-
Notifications
You must be signed in to change notification settings - Fork 226
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
S3655: Honor null forgiving operator #7015
Conversation
e33e560
to
b1a45dc
Compare
1578ed4
to
15f0db1
Compare
15f0db1
to
54c3b8c
Compare
{ | ||
ReportIssue(conversion.Operand, conversion.Operand.Syntax.ToString()); | ||
} | ||
|
||
return context.State; | ||
|
||
bool NotNullFlowState(IOperation reference) => |
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.
@zsolt-kolbay-sonarsource
As discussed offline, the local function name has been changed to NotNullFlowState
from NoNullForgiving
, since Roslyn flow analysis can deduce a non-null nullability state even in absence of the null forgiving operator.
On the other hand, when we reach this condition of the if statement, we are in a very narrow scenario, involving a nullable value type, and it's not clear to me how that would happen.
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
87cdd6c
to
aa75910
Compare
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 with one refactoring to do before merging
{ | ||
ReportIssue(conversion.Operand, conversion.Operand.Syntax.ToString()); | ||
} | ||
|
||
return context.State; | ||
|
||
bool NotNullFlowState(IOperation reference) => |
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.
The name seems wrong to me from looking at the implementation.
Implementation says (equivalent evolution on each line)
FlowState != NotNull
FlowState Not NotNull
Not NotNull FlowState
while the method is named
NotNull FlowState
so it's the opposite.
It's should be NotNotNullFlowState
and that's bad.
I don't have better idea for a good name. So we can avoid it like this:
&& FlowState(reference.Instance) != NullableFlowState.NotNull
// ...
NullableFlowState FlowState(IOperation reference) =>
SemanticModel.GetTypeInfo(reference.Syntax).Nullability().FlowState;
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
Improves #6794
Follows discussion had here.
When the user specifies the null forgiving operator on a value access of a
Nullable<T>
(e.g.nullable!.Value
), no issue is raised by S3655, even when a path is found, in whichnullable
isnull
.It also removes false positives encountered during peach validation for #6794, such as
source:
dotnet-runtime:src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/LambdaCompiler.Statements.cs