Skip to content
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

Merged
merged 3 commits into from
Apr 14, 2023

Conversation

antonioaversa
Copy link
Contributor

@antonioaversa antonioaversa commented Mar 31, 2023

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 which nullable is null.

It also removes false positives encountered during peach validation for #6794, such as

Label? after = null;
if (info.Is64BitSwitch)
{
    after = _ilg.DefineLabel();
    // ...
}
// ...
if (info.Is64BitSwitch)
{
    _ilg.MarkLabel(after!.Value); // <- This should be compliant
}

source: dotnet-runtime:src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/LambdaCompiler.Statements.cs

@antonioaversa antonioaversa mentioned this pull request Mar 31, 2023
@antonioaversa antonioaversa changed the base branch from master to feature/SE March 31, 2023 11:42
@antonioaversa antonioaversa changed the title CS3655: Honor null forgiving operator S3655: Honor null forgiving operator Mar 31, 2023
@antonioaversa antonioaversa changed the title S3655: Honor null forgiving operator [DRAFT] S3655: Honor null forgiving operator Mar 31, 2023
@antonioaversa antonioaversa changed the title [DRAFT] S3655: Honor null forgiving operator S3655: Honor null forgiving operator [DRAFT] Mar 31, 2023
@antonioaversa antonioaversa force-pushed the Antonio/S3655-cs9-cs10-syntax-bang branch from e33e560 to b1a45dc Compare April 3, 2023 15:03
Base automatically changed from feature/SE to master April 4, 2023 13:05
@antonioaversa antonioaversa force-pushed the Antonio/S3655-cs9-cs10-syntax-bang branch 5 times, most recently from 1578ed4 to 15f0db1 Compare April 5, 2023 13:03
@antonioaversa antonioaversa changed the title S3655: Honor null forgiving operator [DRAFT] S3655: Honor null forgiving operator Apr 5, 2023
@antonioaversa antonioaversa marked this pull request as ready for review April 5, 2023 13:26
@antonioaversa antonioaversa force-pushed the Antonio/S3655-cs9-cs10-syntax-bang branch from 15f0db1 to 54c3b8c Compare April 6, 2023 06:53
{
ReportIssue(conversion.Operand, conversion.Operand.Syntax.ToString());
}

return context.State;

bool NotNullFlowState(IOperation reference) =>
Copy link
Contributor Author

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.

Copy link
Contributor

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM

@antonioaversa antonioaversa force-pushed the Antonio/S3655-cs9-cs10-syntax-bang branch from 87cdd6c to aa75910 Compare April 14, 2023 12:34
Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a 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) =>
Copy link
Contributor

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;

@sonarcloud
Copy link

sonarcloud bot commented Apr 14, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Apr 14, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@antonioaversa antonioaversa merged commit 61c0a19 into master Apr 14, 2023
@antonioaversa antonioaversa deleted the Antonio/S3655-cs9-cs10-syntax-bang branch April 14, 2023 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants