sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

Improve S1125: Flexibility in opting in and out constant patterns

Open antonioaversa opened this issue 1 year ago • 4 comments

The context

After this issue, reported in 2019, was implemented earlier this year, we have got a user request to relax the reporting in presence of constant patterns: https://github.com/SonarSource/sonar-dotnet/issues/8147.

The goal

Provide the user with more flexibility, by allowing to opt in and out:

  • ==-based comparisons: == true and == false
  • is false comparison
  • is true comparison

Possible solutions

From @antonioaversa I think we have four options here:

  1. keep the rule as is, and reject the user request: b is false and b is true remain redundant

Or split the rule into two: take out is cases from 1125 and create a new rule for them: 2. with the new rule treating b is true as redundant and b is false as redundant 3. with the new rule treating b is true as redundant and b is false as valid

  1. Or keep b is true in S1125 and have the new rule only report on b is false.

From @Tim-Pohlmann

There could be three rules:

  1. Use x instead of x == true and x is true.
  2. Use !x instead of x == false and x is false.
  3. Use x is true/false instead of x == true/false.

Allowed patterns based on enabled rules:

  • Only 1: x !x x == false x is false
  • 1 + 2: x !x
  • Only 3: x !x x is true x is false
  • 1 + 3: x !x x is false

In addition to that 3 could also have an effect on != or that could be a separate thing.

antonioaversa avatar Oct 11 '23 08:10 antonioaversa