Also run cs/constant-condition on test for cs/comparison-of-identical-expressions

This commit is contained in:
Owen Mansel-Chan
2026-07-08 13:18:37 +01:00
parent 85d800f317
commit 7b96f66d34
3 changed files with 38 additions and 30 deletions

View File

@@ -11,54 +11,54 @@ class CompareIdenticalValues : Super
{
public void M()
{
if (this.Foo == Foo) ; // $ Alert
if (base.Foo == Foo) ; // $ Alert
if (this.Foo == Foo) ; // $ Alert[cs/comparison-of-identical-expressions]
if (base.Foo == Foo) ; // $ Alert[cs/comparison-of-identical-expressions]
if (Foo == new CompareIdenticalValues().Foo) ;
var x = "Abc";
if (x == "Abc") ;
var temp = x == x; // BAD: but flagged by cs/constant-comparison
var temp = x == x; // $ Alert[cs/constant-condition] // BAD: but flagged by cs/constant-condition
double d = double.NaN;
if (d == d) ; // $ Alert // !double.IsNan(d)
if (d <= d) ; // $ Alert // !double.IsNan(d), but unlikely to be intentional
if (d >= d) ; // $ Alert // !double.IsNan(d), but unlikely to be intentional
if (d != d) ; // $ Alert // double.IsNan(d)
if (d > d) ; // $ Alert // always false
if (d < d) ; // $ Alert // always false
if (d == d) ; // $ Alert[cs/comparison-of-identical-expressions] // !double.IsNan(d)
if (d <= d) ; // $ Alert[cs/comparison-of-identical-expressions] // !double.IsNan(d), but unlikely to be intentional
if (d >= d) ; // $ Alert[cs/comparison-of-identical-expressions] // !double.IsNan(d), but unlikely to be intentional
if (d != d) ; // $ Alert[cs/comparison-of-identical-expressions] // double.IsNan(d)
if (d > d) ; // $ Alert[cs/comparison-of-identical-expressions] // always false
if (d < d) ; // $ Alert[cs/comparison-of-identical-expressions] // always false
float f = float.NaN;
if (f == f) ; // $ Alert // !float.IsNan(f)
if (f <= f) ; // $ Alert // !float.IsNan(f), but unlikely to be intentional
if (f >= f) ; // $ Alert // !float.IsNan(f), but unlikely to be intentional
if (f != f) ; // $ Alert // float.IsNan(f)
if (f > f) ; // $ Alert // always false
if (f < f) ; // $ Alert // always false
if (f == f) ; // $ Alert[cs/comparison-of-identical-expressions] // !float.IsNan(f)
if (f <= f) ; // $ Alert[cs/comparison-of-identical-expressions] // !float.IsNan(f), but unlikely to be intentional
if (f >= f) ; // $ Alert[cs/comparison-of-identical-expressions] // !float.IsNan(f), but unlikely to be intentional
if (f != f) ; // $ Alert[cs/comparison-of-identical-expressions] // float.IsNan(f)
if (f > f) ; // $ Alert[cs/comparison-of-identical-expressions] // always false
if (f < f) ; // $ Alert[cs/comparison-of-identical-expressions] // always false
int i = 0;
if (i == i) ; // BAD: but flagged by cs/constant-condition
if (i != i) ; // BAD: but flagged by cs/constant-condition
if (i == i) ; // $ Alert[cs/constant-condition] // BAD: but flagged by cs/constant-condition
if (i != i) ; // $ Alert[cs/constant-condition] // BAD: but flagged by cs/constant-condition
CompareIdenticalValues c = null;
c.Prop.Equals(c.Prop); // $ Alert
Equals(c.Prop.Prop.Prop.Foo + 2, c.Prop.Prop.Prop.Foo + 2); // $ Alert
c.Prop.Equals(c.Prop); // $ Alert[cs/comparison-of-identical-expressions]
Equals(c.Prop.Prop.Prop.Foo + 2, c.Prop.Prop.Prop.Foo + 2); // $ Alert[cs/comparison-of-identical-expressions]
Equals(c.Prop.Prop.Prop.Foo, c.Prop.Prop.Foo);
if (base.Bar == Bar) ;
if (Bar == this.Bar) ; // $ Alert
Equals(this); // $ Alert
if (Bar == this.Bar) ; // $ Alert[cs/comparison-of-identical-expressions]
Equals(this); // $ Alert[cs/comparison-of-identical-expressions]
if (1 + 1 == 2) ; // BAD: but flagged by cs/constant-condition
if (1 + 1 == 3) ;
if (0 == 1) ;
if (1 + 1 == 2) ; // $ Alert[cs/constant-condition] // BAD: but flagged by cs/constant-condition
if (1 + 1 == 3) ; // $ Alert[cs/constant-condition] // BAD: but flagged by cs/constant-condition
if (0 == 1) ; // $ Alert[cs/constant-condition] // BAD: but flagged by cs/constant-condition
var a = new int[0];
if (a[0] == a[0]) ; // $ Alert
if (a[0] == a[0]) ; // $ Alert[cs/comparison-of-identical-expressions]
if (this.Bar[0] == Bar[1 - 1]) ; // $ Alert
if (this.Bar[0] == Bar[1 - 1]) ; // $ Alert[cs/comparison-of-identical-expressions]
if (this.Bar[0] == Bar[1]) ;
if (this.Prop[Foo] == Prop[this.Foo]) ; // $ Alert
if (this.Prop[Foo] == Prop[this.Foo]) ; // $ Alert[cs/comparison-of-identical-expressions]
if (this.Prop[0] == Prop[1]) ;
}
@@ -73,17 +73,17 @@ class CompareIdenticalValues : Super
public void IsBoxedWrong1<T>(T x) where T : struct
{
ReferenceEquals(x, x); // $ Alert
ReferenceEquals(x, x); // $ Alert[cs/comparison-of-identical-expressions]
}
public void IsBoxedWrong2<T>(T x) where T : class
{
ReferenceEquals(x, x); // $ Alert
ReferenceEquals(x, x); // $ Alert[cs/comparison-of-identical-expressions]
}
public void IsBoxedWrong3<T>(T x) where T : Super
{
ReferenceEquals(x, x); // $ Alert
ReferenceEquals(x, x); // $ Alert[cs/comparison-of-identical-expressions]
}
public int this[int i] { get { return 0; } }

View File

@@ -0,0 +1,6 @@
| CompareIdenticalValues.cs:20:20:20:25 | ... == ... | Condition always evaluates to 'true'. | CompareIdenticalValues.cs:20:20:20:25 | ... == ... | dummy |
| CompareIdenticalValues.cs:39:13:39:18 | ... == ... | Condition always evaluates to 'true'. | CompareIdenticalValues.cs:39:13:39:18 | ... == ... | dummy |
| CompareIdenticalValues.cs:40:13:40:18 | ... != ... | Condition always evaluates to 'false'. | CompareIdenticalValues.cs:40:13:40:18 | ... != ... | dummy |
| CompareIdenticalValues.cs:51:13:51:22 | ... == ... | Condition always evaluates to 'true'. | CompareIdenticalValues.cs:51:13:51:22 | ... == ... | dummy |
| CompareIdenticalValues.cs:52:13:52:22 | ... == ... | Condition always evaluates to 'false'. | CompareIdenticalValues.cs:52:13:52:22 | ... == ... | dummy |
| CompareIdenticalValues.cs:53:13:53:18 | ... == ... | Condition always evaluates to 'false'. | CompareIdenticalValues.cs:53:13:53:18 | ... == ... | dummy |

View File

@@ -0,0 +1,2 @@
query: Bad Practices/Control-Flow/ConstantCondition.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql