Skip to content

[NET10.0] Annotate converters for nullability 4/n#29549

Merged
rmarinho merged 3 commits intodotnet:net10.0from
MartyIX:feature/2025-05-16-Convert-nullability-4-n-NET10
May 28, 2025
Merged

[NET10.0] Annotate converters for nullability 4/n#29549
rmarinho merged 3 commits intodotnet:net10.0from
MartyIX:feature/2025-05-16-Convert-nullability-4-n-NET10

Conversation

@MartyIX
Copy link
Contributor

@MartyIX MartyIX commented May 16, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

Follow-up to #29314. See #28244 (comment) for details on motivation.

Issues Fixed

Contributes to #28860

@MartyIX MartyIX requested a review from a team as a code owner May 16, 2025 14:04
@MartyIX MartyIX requested review from jfversluis and rmarinho May 16, 2025 14:04
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label May 16, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@MartyIX! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

_parts = css!.Replace("\r\n", "")
#else
_parts = css.Replace("\r\n", "", StringComparison.Ordinal)
_parts = css!.Replace("\r\n", "", StringComparison.Ordinal)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason string.IsNullOrWhiteSpace(css) is not enough for the analyzer to realize that css is not null at this point. (if (css is null || css = "") would be an alternative to avoid the ! operator.

if (strValue.StartsWith(Rgb, StringComparison.InvariantCulture)
|| strValue.StartsWith(Rgba, StringComparison.InvariantCulture)
|| strValue.StartsWith(Hsl, StringComparison.InvariantCulture)
|| strValue.StartsWith(Hsla, StringComparison.InvariantCulture))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringComparison.InvariantCulture was missing for Hsla, so I added it.

@rmarinho
Copy link
Member

@MartyIX net10 android needs Android 35 api? we also recommend jdk 21.

@MartyIX
Copy link
Contributor Author

MartyIX commented May 19, 2025

I do have JDK 21 installed on my machine.

net10 android needs Android 35 api?

I don't know. I just ran dotnet build ./microsoft.Maui.BuildTasks.slnf.

@bhavanesh2001
Copy link
Contributor

@MartyIX I ran into the same build issue.

In my case, I had updated my System Environment Variables to set ANDROID_SDK_ROOT and ANDROID_HOME to:

C:\Users\user1\AppData\Local\Android\Sdk

— primarily to get Appium working. After that, I started encountering build errors when trying to build MAUI code.

It turns out that the MAUI/Gradle build process checks for the SDK at:

C:\Program Files (x86)\Android\android-sdk

which is where Visual Studio installs the SDK by default via the Android SDK Manager.

To resolve the issue, I simply removed the manually set environment variables, and the build worked again without errors.

Hope this helps.

@MartyIX
Copy link
Contributor Author

MartyIX commented May 19, 2025

Much appreciated! My setup seems to be a bit different. I'm trying to repair my Visual Studio at the moment, if it is not related to it. It's a wild guess.

Otherwise, my setup looks pretty normal. Not sure what might be wrong. But I'll figure it out... sooner or later.

@MartyIX MartyIX force-pushed the feature/2025-05-16-Convert-nullability-4-n-NET10 branch from 199468b to e694190 Compare May 19, 2025 21:17
@rmarinho
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@rmarinho
Copy link
Member

I do have JDK 21 installed on my machine.

net10 android needs Android 35 api?

I don't know. I just ran dotnet build ./microsoft.Maui.BuildTasks.slnf.

Sorry it wasn't a question, you need to install api 35 of android

@MartyIX
Copy link
Contributor Author

MartyIX commented May 20, 2025

@bhavanesh2001 I copied C:\Program Files (x86)\Android\android-sdk\platforms\android-Baklava -> C:\Program Files (x86)\Android\android-sdk\platforms\android-36 (effectively renaming it) and it did the trick for me. :)

@PureWeen PureWeen moved this from Todo to Changes Requested in MAUI SDK Ongoing May 20, 2025
@PureWeen PureWeen added this to the .NET 10.0-preview6 milestone May 20, 2025
}

pathFigure.Segments.Add(new LineSegment
pathFigure!.Segments.Add(new LineSegment
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just return if it s null ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is my understanding that it would be better to:

if (pathFigure is null)
{
	ThrowBadToken();
}
else
{
	pathFigure.Segments.Add(new LineSegment
	{
		Point = lastPoint
	});
}

because one can get here presumably only if the parsed path string is somehow invalid (there is no 'm' / 'M' specified.

Would it make sense to you as an alternative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it this way.

Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test passing null to these converters to make sure it actually works and doesn t break ?

@MartyIX MartyIX force-pushed the feature/2025-05-16-Convert-nullability-4-n-NET10 branch from e694190 to af9d132 Compare May 27, 2025 13:49
@MartyIX
Copy link
Contributor Author

MartyIX commented May 27, 2025

Can we add a test passing null to these converters to make sure it actually works and doesn t break ?

I added tests which show that converters behave differently but the tests hopefully make sure that they behave the same as before.

public class PathFigureCollectionConverterTests
{
private readonly PathFigureCollectionConverter _pathFigureCollectionConverter = new();
private readonly PathFigureCollectionConverter _converter = new();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed so it's unified with other tests.

}

void ThrowBadToken()
Exception GetBadTokenException()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified to return the exception as throw GetBadTokenException() behaves much better with respect to .NET analyzers (detection of dead code, etc.)

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
var strValue = value?.ToString();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world, we would go with

Suggested change
var strValue = value?.ToString();
var strValue = value.ToString();

but that would change behavior. This approach is used even in other converters.

@rmarinho
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

public class BrushTypeConverterUnitTests : BaseTestFixture
{
BrushTypeConverter _converter;
private readonly BrushTypeConverter _converter = new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don t use private

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

IMO there should be an analyzer checking it. So much time of humans and machines is lost on this.

@github-project-automation github-project-automation bot moved this from Changes Requested to Approved in MAUI SDK Ongoing May 28, 2025
@rmarinho rmarinho merged commit 9568b9c into dotnet:net10.0 May 28, 2025
119 of 135 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in MAUI SDK Ongoing May 28, 2025
@MartyIX MartyIX deleted the feature/2025-05-16-Convert-nullability-4-n-NET10 branch May 28, 2025 12:02
@github-actions github-actions bot locked and limited conversation to collaborators Jun 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

community ✨ Community Contribution

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants