Open antonioaversa opened 11 months ago
It would also be nice if the rule is relaxed when the comparison involves checking the run-time type of a boxed value.
A slimmed down example from a WPF IValueConverter implementation where the is true
pattern is reported as a code smell, but returning value
is not equivalent.
public object Convert(object value)
{
return value is true;
}
hello, any news when this will be done ? we are also struggling to remove the !
and use is false
whenever possible in our code but this rule kinda hinders us at the moment
Having is false
as a valid option sounds good to me.
is true
is redundant and should IMHO not be valid
I increased the priority of this ticket. Maybe we can pick it up in a future hardening sprint.
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
comparisonis true
comparisonPossible solutions
From @antonioaversa I think we have four options here:
Or split the rule into two: take out is cases from 1125 and create a new rule for them:
with the new rule treating b is true as redundant and b is false as redundant
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:
x
instead ofx == true
andx is true
.!x
instead ofx == false
andx is false
.x is true/false
instead ofx == true/false
.Allowed patterns based on enabled rules:
x
!x
x == false
x is false
x
!x
x
!x
x is true
x is false
x
!x
x is false
In addition to that 3 could also have an effect on != or that could be a separate thing.