diff --git a/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/CompareIdenticalValues.cs b/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/CompareIdenticalValues.cs index 540ce6bb814..68e95de15dc 100644 --- a/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/CompareIdenticalValues.cs +++ b/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/CompareIdenticalValues.cs @@ -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 x) where T : struct { - ReferenceEquals(x, x); // $ Alert + ReferenceEquals(x, x); // $ Alert[cs/comparison-of-identical-expressions] } public void IsBoxedWrong2(T x) where T : class { - ReferenceEquals(x, x); // $ Alert + ReferenceEquals(x, x); // $ Alert[cs/comparison-of-identical-expressions] } public void IsBoxedWrong3(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; } } diff --git a/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/ConstantCondition.expected b/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/ConstantCondition.expected new file mode 100644 index 00000000000..e54ba7f25d7 --- /dev/null +++ b/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/ConstantCondition.expected @@ -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 | diff --git a/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/ConstantCondition.qlref b/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/ConstantCondition.qlref new file mode 100644 index 00000000000..6692217230e --- /dev/null +++ b/csharp/ql/test/query-tests/CSI/CompareIdenticalValues/ConstantCondition.qlref @@ -0,0 +1,2 @@ +query: Bad Practices/Control-Flow/ConstantCondition.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql