-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle nullability attributes in param-nullchecking #59393
Conversation
{ | ||
diagnostics.Add(ErrorCode.WRN_NullCheckingOnNullableType, location, parameter); | ||
} | ||
else if (parameter.Type.IsValueType && !parameter.Type.IsPointerOrFunctionPointer()) | ||
|
||
if (parameter.Type.IsNonNullableValueType() && !parameter.Type.IsPointerOrFunctionPointer()) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
src/Compilers/CSharp/Test/Semantic/Semantics/NullCheckedParameterTests.cs
Show resolved
Hide resolved
This comment was marked as outdated.
This comment was marked as outdated.
ping @dotnet/roslyn-compiler for second review |
|
||
var annotations = parameter.FlowAnalysisAnnotations; | ||
if ((annotations & FlowAnalysisAnnotations.NotNull) == 0 | ||
&& !NullableWalker.GetParameterState(parameter.TypeWithAnnotations, annotations, applyParameterNullCheck: false).IsNotNull |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
{ | ||
diagnostics.Add(ErrorCode.ERR_NonNullableValueTypeIsNullChecked, location, parameter); | ||
} | ||
|
||
// 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) |
There was a problem hiding this comment.
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 :-)
There was a problem hiding this 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)
Closes #59226
Relates to test plan #36024