Skip to content

Handle nullability attributes in param-nullchecking#59393

Merged
RikkiGibson merged 5 commits intodotnet:mainfrom
RikkiGibson:pnc-notnull
Feb 16, 2022
Merged

Handle nullability attributes in param-nullchecking#59393
RikkiGibson merged 5 commits intodotnet:mainfrom
RikkiGibson:pnc-notnull

Conversation

@RikkiGibson
Copy link
Copy Markdown
Member

@RikkiGibson RikkiGibson commented Feb 8, 2022

Closes #59226
Relates to test plan #36024

@RikkiGibson RikkiGibson requested a review from a team as a code owner February 8, 2022 22:23
@ghost ghost added the Area-Compilers label Feb 8, 2022
}
else if (parameter.Type.IsValueType && !parameter.Type.IsPointerOrFunctionPointer())

if (parameter.Type.IsNonNullableValueType() && !parameter.Type.IsPointerOrFunctionPointer())
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tend to dislike if (...) { warn } else if (...) { error } conditions anyway--can be a bit brittle.

var annotations = parameter.FlowAnalysisAnnotations;
if ((annotations & FlowAnalysisAnnotations.NotNull) == 0
&& !NullableWalker.GetParameterState(parameter.TypeWithAnnotations, annotations, applyParameterNullCheck: false).IsNotNull
&& (!parameter.Type.IsTypeParameter() || parameter.Type.IsNullableTypeOrTypeParameter() || parameter.TypeWithAnnotations.NullableAnnotation.IsAnnotated() || (annotations & FlowAnalysisAnnotations.AllowNull) != 0))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Generally, if it's possible for a parameter of a type parameter type to be non-nullable after substitution, we want to avoid giving the warning.

@RikkiGibson

This comment was marked as outdated.

@RikkiGibson
Copy link
Copy Markdown
Member Author

ping @dotnet/roslyn-compiler for second review


var annotations = parameter.FlowAnalysisAnnotations;
if ((annotations & FlowAnalysisAnnotations.NotNull) == 0
&& !NullableWalker.GetParameterState(parameter.TypeWithAnnotations, annotations, applyParameterNullCheck: false).IsNotNull
Copy link
Copy Markdown
Member

@jcouv jcouv Feb 12, 2022

Choose a reason for hiding this comment

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

Consider simplifying the double-negation: && NullableWalker.GetParameterState(....).MayBeNull
Correction: should use the extension method MayBeNull(). Somehow, MaybeNull property and MayBeNull() extension method diverged for the maybe-default case :-/

var annotations = parameter.FlowAnalysisAnnotations;
if ((annotations & FlowAnalysisAnnotations.NotNull) == 0
&& !NullableWalker.GetParameterState(parameter.TypeWithAnnotations, annotations, applyParameterNullCheck: false).IsNotNull
&& (!parameter.Type.IsTypeParameter() || parameter.Type.IsNullableTypeOrTypeParameter() || parameter.TypeWithAnnotations.NullableAnnotation.IsAnnotated() || (annotations & FlowAnalysisAnnotations.AllowNull) != 0))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

From offline chat:
There's an open question on type parameter constrained to C?.
Is there any existing helper/abstraction we might use to simplify this?
Consider extracting this line to a local function with comments.

@RikkiGibson RikkiGibson requested a review from jcouv February 14, 2022 22:50
@jcouv jcouv self-assigned this Feb 15, 2022
}

// For type parameters, we only want to give the warning if no type argument would result in a non-nullable type.
static bool isTypeParameterWithPossiblyNonNullableType(TypeWithAnnotations typeWithAnnotations, FlowAnalysisAnnotations annotations)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, that was much easier to follow :-)

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 5)

@RikkiGibson RikkiGibson merged commit 20cb688 into dotnet:main Feb 16, 2022
@ghost ghost added this to the Next milestone Feb 16, 2022
@RikkiGibson RikkiGibson deleted the pnc-notnull branch February 16, 2022 06:32
@RikkiGibson RikkiGibson modified the milestones: Next, 17.2.P2 Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Null-checking nullable param warning should be relaxed

3 participants