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

Fix S2259 FP: Recognize NotNull results of String methods #7838

Merged
merged 9 commits into from
Aug 24, 2023

Conversation

mary-georgiou-sonarsource
Copy link
Contributor

Fixes #6158

@@ -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),
Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah... yes!

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

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

Round 1

@@ -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),
Copy link
Contributor

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.

@@ -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",
Copy link
Contributor Author

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).

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a 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.

@@ -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",
Copy link
Contributor

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))
Copy link
Contributor

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.

@sonarcloud
Copy link

sonarcloud bot commented Aug 24, 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 Aug 24, 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

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

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

LGTM!

@mary-georgiou-sonarsource mary-georgiou-sonarsource merged commit 49afee5 into master Aug 24, 2023
29 checks passed
@mary-georgiou-sonarsource mary-georgiou-sonarsource deleted the mary/2259-fp branch August 24, 2023 12:46
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.

Fix S2259 FP: Recognize NotNull results of String methods
3 participants