sonar-dotnet
sonar-dotnet copied to clipboard
Improve S1125: Flexibility in opting in and out constant patterns
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:
- 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
- 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:
- Use
x
instead ofx == true
andx is true
. - Use
!x
instead ofx == false
andx is false
. - Use
x is true/false
instead ofx == 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.