-
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
Fix S2589 FP: When condition that evaluates to constant is a constant field. #7708
Fix S2589 FP: When condition that evaluates to constant is a constant field. #7708
Conversation
dbb3f87
to
9f9c44d
Compare
9e26499
to
4e90bc7
Compare
...ts/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.cs
Outdated
Show resolved
Hide resolved
...ts/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.cs
Outdated
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.
It seems like a file got committed by accident.
@@ -43,7 +43,8 @@ public override ProgramState ConditionEvaluated(SymbolicContext context) | |||
{ | |||
var operation = context.Operation.Instance; | |||
if (operation.Kind is not OperationKindEx.Literal | |||
&& operation.Syntax.Ancestors().Any(IsUsing) is false) | |||
&& operation.Syntax.Ancestors().Any(IsUsing) is false | |||
&& !IsConstField(operation)) |
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 wonder if we should just use !operation.ConstantValue.HasValue
instead. Let's keep that option in mind and see if we get other annoying FPs with the field approach.
...SonarAnalyzer.Common/SymbolicExecution/Roslyn/RuleChecks/ConditionEvaluatesToConstantBase.cs
Outdated
Show resolved
Hide resolved
...ts/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/MethodParameterUnused.cs
Outdated
Show resolved
Hide resolved
...ts/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.cs
Outdated
Show resolved
Hide resolved
Do we have tests like |
2009087
to
d3c38b2
Compare
e114c8a
to
6dec1e3
Compare
{ | ||
var x = t || a || b; // Compliant t is const | ||
|
||
if (t == true) // Noncompliant |
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 be indeed noncompliant as we are evaluating the whole condition as a whole t == true
.
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.
Yes, correct!
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!
Related to #7646