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 S2589 FP: When condition that evaluates to constant is a constant field. #7708

Merged
merged 5 commits into from
Aug 2, 2023

Conversation

mary-georgiou-sonarsource
Copy link
Contributor

Related to #7646

@mary-georgiou-sonarsource mary-georgiou-sonarsource changed the title S2589: Exclude constants Fix S2589 FP: When condition that evaluates to constant is a constant field. Jul 31, 2023
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.

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

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.

@Tim-Pohlmann
Copy link
Contributor

Do we have tests like ConstTrue == true?

{
var x = t || a || b; // Compliant t is const

if (t == true) // Noncompliant
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 should be indeed noncompliant as we are evaluating the whole condition as a whole t == true.
Am I missing something?

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann Aug 2, 2023

Choose a reason for hiding this comment

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

Yes, correct!

@mary-georgiou-sonarsource mary-georgiou-sonarsource marked this pull request as ready for review August 2, 2023 12:16
@sonarqubecloud
Copy link

sonarqubecloud bot commented Aug 2, 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

@sonarqubecloud
Copy link

sonarqubecloud bot commented Aug 2, 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
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!

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