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

Improve S2589: Detect comparison between constant values #6745

Closed
gregory-paidis-sonarsource opened this issue Feb 10, 2023 · 2 comments · Fixed by #7750
Closed

Improve S2589: Detect comparison between constant values #6745

gregory-paidis-sonarsource opened this issue Feb 10, 2023 · 2 comments · Fixed by #7750
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues.
Milestone

Comments

@gregory-paidis-sonarsource
Copy link
Contributor

Comparing a constant value with another constant value does not make much sense.
The evaluation yields exclusively true or false, meaning the "conditional" code is always/never executed.

            const int constant1 = 42;
            const int constant2 = 42;
            const int constant3 = 43;

            if (constant1 == constant2) // Noncompliant, always true
            {
                DoWork(); // Always executed
            }

            if (constant1 == constant3) // Noncompliant, always false
            {
                DoWork(); // Never executed
            }
@gregory-paidis-sonarsource gregory-paidis-sonarsource added Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. Type: Rule Idea Idea for a rule that has NOT been specified. labels Feb 10, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Remove this comparison between constant values Improve S2589: Detect comparison between constant values Jul 18, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: False Negative Rule is NOT triggered when it should be. Area: CFG/SE CFG and SE related issues. Type: Improvement and removed Type: Rule Idea Idea for a rule that has NOT been specified. Type: False Negative Rule is NOT triggered when it should be. labels Jul 18, 2023
@pavel-mikula-sonarsource
Copy link
Contributor

This should just start working out of the box when migrating to the new engine, as we already compute BoolConstraint from NumberConstraint comparisons.

@mary-georgiou-sonarsource
Copy link
Contributor

Fixed by #7750

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants