-
Notifications
You must be signed in to change notification settings - Fork 222
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
Fix S2259 FP: Recognize NotNull results of String methods #7838
Conversation
839c81a
to
9d0930f
Compare
@@ -67,6 +67,7 @@ protected override ProgramState[] Process(SymbolicContext context, IInvocationOp | |||
_ when invocation.TargetMethod.Name == nameof(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, true), | |||
_ when invocation.TargetMethod.ContainingType.Is(KnownType.System_String) => ProcessSystemStringInvocation(state, invocation), |
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.
@Tim-Pohlmann in line 66 for ProcessLinqEnumerableAndQueryable
see that the current state is being passed (context.State
) - ignoring any constraints that might have been reset/added from previous lines.
I refactored the method there, in my machine locally, to accept the state
as parameter and the UTs did not fail :/
If I'm not wrong we should pass also there the state
and not the context.State
- and figure out a UT that breaks in case of change.
Do I miss something here?
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.
You miss that this is a switch expression. There is no fall-through. Only one line will get executed.
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.
@Tim-Pohlmann I was not clear enough sorry - when I said previous lines I meant the ones before the switch -33- 58
.
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.
Ah... yes!
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.
So you agree in passing the state and not the context.State?
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.
It seems the correct thing to do. But we have to confirm it with a suitable UT.
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 added a task for this to my calendar and I'll open an issue if it makes sense.
For now I will leave this PR as is.
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.
Round 1
...s/src/SonarAnalyzer.Common/SymbolicExecution/Roslyn/OperationProcessors/Invocation.String.cs
Outdated
Show resolved
Hide resolved
...s/src/SonarAnalyzer.Common/SymbolicExecution/Roslyn/OperationProcessors/Invocation.String.cs
Outdated
Show resolved
Hide resolved
...s/src/SonarAnalyzer.Common/SymbolicExecution/Roslyn/OperationProcessors/Invocation.String.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/SymbolicExecution/Roslyn/OperationProcessors/Invocation.cs
Show resolved
Hide resolved
@@ -67,6 +67,7 @@ protected override ProgramState[] Process(SymbolicContext context, IInvocationOp | |||
_ when invocation.TargetMethod.Name == nameof(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, true), | |||
_ when invocation.TargetMethod.ContainingType.Is(KnownType.System_String) => ProcessSystemStringInvocation(state, invocation), |
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.
You miss that this is a switch expression. There is no fall-through. Only one line will get executed.
dd0d937
to
43f040e
Compare
...ts/expected/Ember-MM/generic.EmberCore.NMT-{84B2143A-D04F-4262-923D-21AEDF86E2B7}-S2259.json
Show resolved
Hide resolved
@@ -158,6 +145,19 @@ | |||
}, | |||
{ | |||
"id": "S2259", | |||
"message": "'fiAV' is Nothing on at least one execution path.", | |||
"location": { | |||
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Ember-MM/Addons/generic.EmberCore.NMT/dlgNMTMovies.vb#L620", |
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.
Not sure how this PR helped but this seems to be a TP due to line 601 (downcasting).
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.
601 is no downcast, this is just VB's way to declare variables. The reason for 2259 here is probably the IsNothing
in line 607.
Then again, I don't think this PR should raise any new issues or am I missing something?
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.
well we learn now that certain methods do not return null - so from my side some issues would not be surprising.
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 is a TP.
It's raising normally in the UTs before and after this change (thus is should not be related to this PR at all).
It should be related to the way the engine visits and cuts branches (this is my assumption).
I would not spend more time on it for now.
analyzers/its/expected/Ember-MM/EmberAPI-{208AA35E-C6AE-4D2D-A9DD-B6EFD19A4279}-S2589.json
Show resolved
Hide resolved
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.
We need more dedicated UTs. And something is fishy with the ITs. We definitely need more investigation.
...ts/SonarAnalyzer.UnitTest/SymbolicExecution/Roslyn/RoslynSymbolicExecutionTest.Invocation.cs
Outdated
Show resolved
Hide resolved
analyzers/its/expected/Ember-MM/EmberAPI-{208AA35E-C6AE-4D2D-A9DD-B6EFD19A4279}-S2589.json
Show resolved
Hide resolved
...ts/expected/Ember-MM/generic.EmberCore.NMT-{84B2143A-D04F-4262-923D-21AEDF86E2B7}-S2259.json
Show resolved
Hide resolved
@@ -158,6 +145,19 @@ | |||
}, | |||
{ | |||
"id": "S2259", | |||
"message": "'fiAV' is Nothing on at least one execution path.", | |||
"location": { | |||
"uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Ember-MM/Addons/generic.EmberCore.NMT/dlgNMTMovies.vb#L620", |
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.
601 is no downcast, this is just VB's way to declare variables. The reason for 2259 here is probably the IsNothing
in line 607.
Then again, I don't think this PR should raise any new issues or am I missing something?
{ | ||
return ProcessIsNotNullWhen(state, invocation.WrappedOperation, invocation.Arguments[0].ToArgument(), false, true); | ||
} | ||
else if (StringMethodReturningNotNull.Contains(invocation.TargetMethod.Name)) |
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 should either use methodName
, or methodName
should be removed.
Following "2 is a group, 3 is a crowd", I'd remove methodName
variable.
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
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!
Fixes #6158