-
Notifications
You must be signed in to change notification settings - Fork 231
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
SE: Do not learn Null constraint from NotNullWhenAttribute #6159
SE: Do not learn Null constraint from NotNullWhenAttribute #6159
Conversation
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
I think the root cause of the issues is not typeofShould be fixed by: 6157 Multiple attributes
public static bool TryCreate(
[System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true), System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Uri")] string? uriString,
in System.UriCreationOptions creationOptions,
[System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out System.Uri? result) { throw null; } IsKind extension method
We could suppress these kinds of FPs with this PR, but please add issues for each root cause, so we can tackle them the next time we come back to SE. |
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.
This kills some FPs and some TPs. I'm not sure if we want to do this, because most FPs have a different root cause.
@@ -58,7 +58,7 @@ protected override ProgramState[] Process(SymbolicContext context, IInvocationOp | |||
_ when invocation.TargetMethod.ContainingType.IsAny(KnownType.System_Linq_Enumerable, KnownType.System_Linq_Queryable) => ProcessLinqEnumerableAndQueryable(context, invocation), | |||
_ when invocation.TargetMethod.Name == nameof(object.Equals) => ProcessEquals(context, invocation), | |||
_ when invocation.TargetMethod.IsAny(KnownType.System_String, nameof(string.IsNullOrEmpty), nameof(string.IsNullOrWhiteSpace)) => | |||
ProcessIsNotNullWhen(state, invocation.WrappedOperation, invocation.Arguments[0].ToArgument(), false), | |||
ProcessIsNotNullWhen(state, invocation.WrappedOperation, invocation.Arguments[0].ToArgument(), false, true), |
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.
ProcessIsNotNullWhen(state, invocation.WrappedOperation, invocation.Arguments[0].ToArgument(), false, true), | |
ProcessIsNotNullWhen(state, invocation.WrappedOperation, invocation.Arguments[0].ToArgument(), when: false, learnNull: true), |
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'd consider that in case there are 3 of them
@@ -70,13 +70,13 @@ private static ProgramState[] ProcessIsNotNullWhen(ProgramState state, IInvocati | |||
if (argument.Parameter?.GetAttributes().FirstOrDefault(x => x.HasName("NotNullWhenAttribute")) is { } attribute | |||
&& attribute.TryGetAttributeValue<bool>("returnValue", out var returnValue)) | |||
{ | |||
return ProcessIsNotNullWhen(state, invocation.WrappedOperation, argument, returnValue); | |||
return ProcessIsNotNullWhen(state, invocation.WrappedOperation, argument, returnValue, false); |
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.
named argument for "false"?
We strongly prefer FNs over FPs. |
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
grammarName doesn't have NotNull, I've created #6158 yesterday to build it in the future.
Ah, I missed the re-assignment part. It makes sense to add support for string methods.
The IsKind was a motivation for this PR. It just creates too many FPs.
That makes sense. I think we can re-add the constraint when we also take the "NullableFlowState" into account. If Roslyn thinks, that the value "MayBeNull" we can also add the "Null" constraint case. In the case of "None" (aka no nullable analysis performed by Roslyn) or "NotNull" we should not add the null constraint case.
Fixes some Peach issues:
Issue
Line 67
Issue
Line 423
Issue
Line 61
..and few other similar cases