diff --git a/csharp/ql/test/query-tests/Nullness/A.cs b/csharp/ql/test/query-tests/Nullness/A.cs index 51bbc280e3c..12f5d74d5a2 100644 --- a/csharp/ql/test/query-tests/Nullness/A.cs +++ b/csharp/ql/test/query-tests/Nullness/A.cs @@ -5,7 +5,7 @@ class A public void Lock() { object synchronizedAlways = null; - lock (synchronizedAlways) // BAD (always) + lock (synchronizedAlways) // $ Alert[cs/dereferenced-value-is-always-null] { synchronizedAlways.GetHashCode(); // GOOD } @@ -14,7 +14,7 @@ class A public void ArrayAssignTest() { int[] arrayNull = null; - arrayNull[0] = 10; // BAD (always) + arrayNull[0] = 10; // $ Alert[cs/dereferenced-value-is-always-null] int[] arrayOk; arrayOk = new int[10]; @@ -28,10 +28,10 @@ class A object methodAccess = null; object methodCall = null; - Console.WriteLine(arrayAccess[1]); // BAD (always) - Console.WriteLine(fieldAccess.Length); // BAD (always) - Func tmp = methodAccess.ToString; // BAD (always) - Console.WriteLine(methodCall.ToString()); // BAD (always) + Console.WriteLine(arrayAccess[1]); // $ Alert[cs/dereferenced-value-is-always-null] + Console.WriteLine(fieldAccess.Length); // $ Alert[cs/dereferenced-value-is-always-null] + Func tmp = methodAccess.ToString; // $ Alert[cs/dereferenced-value-is-always-null] + Console.WriteLine(methodCall.ToString()); // $ Alert[cs/dereferenced-value-is-always-null] Console.WriteLine(arrayAccess[1]); // GOOD Console.WriteLine(fieldAccess.Length); // GOOD @@ -47,7 +47,7 @@ class A object varRef = null; TestMethod2(ref varRef); - varRef.ToString(); // BAD (always) + varRef.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] varRef = null; TestMethod3(ref varRef); diff --git a/csharp/ql/test/query-tests/Nullness/Assert.cs b/csharp/ql/test/query-tests/Nullness/Assert.cs index 0236977aa39..86a99708a1f 100644 --- a/csharp/ql/test/query-tests/Nullness/Assert.cs +++ b/csharp/ql/test/query-tests/Nullness/Assert.cs @@ -12,7 +12,7 @@ class AssertTests s = b ? null : ""; Assert.IsNull(s); - Console.WriteLine(s.Length); // BAD (always) + Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null] s = b ? null : ""; Assert.IsNotNull(s); @@ -20,7 +20,7 @@ class AssertTests s = b ? null : ""; Assert.IsTrue(s == null); - Console.WriteLine(s.Length); // BAD (always) + Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null] s = b ? null : ""; Assert.IsTrue(s != null); @@ -28,7 +28,7 @@ class AssertTests s = b ? null : ""; Assert.IsFalse(s != null); - Console.WriteLine(s.Length); // BAD (always) + Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null] s = b ? null : ""; Assert.IsFalse(s == null); @@ -44,10 +44,10 @@ class AssertTests s = b ? null : ""; Assert.IsTrue(s == null && b); - Console.WriteLine(s.Length); // BAD (always) + Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null] s = b ? null : ""; Assert.IsFalse(s != null || !b); - Console.WriteLine(s.Length); // BAD (always) + Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null] } } diff --git a/csharp/ql/test/query-tests/Nullness/B.cs b/csharp/ql/test/query-tests/Nullness/B.cs index 76ebb6ffd8e..946bacecefb 100644 --- a/csharp/ql/test/query-tests/Nullness/B.cs +++ b/csharp/ql/test/query-tests/Nullness/B.cs @@ -10,7 +10,7 @@ class B B neqCallAlways = null; if (eqCallAlways == null) - eqCallAlways.ToString(); // BAD (always) + eqCallAlways.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] if (b2 != null) b2.ToString(); // GOOD @@ -21,7 +21,7 @@ class B if (neqCallAlways != null) { } else - neqCallAlways.ToString(); // BAD (always) + neqCallAlways.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } public static bool operator ==(B b1, B b2) diff --git a/csharp/ql/test/query-tests/Nullness/C.cs b/csharp/ql/test/query-tests/Nullness/C.cs index 805d9e2cae4..1375120ed5d 100644 --- a/csharp/ql/test/query-tests/Nullness/C.cs +++ b/csharp/ql/test/query-tests/Nullness/C.cs @@ -15,7 +15,7 @@ public class C if (!(o != null)) { - o.GetHashCode(); // BAD (always) + o.GetHashCode(); // $ Alert[cs/dereferenced-value-is-always-null] } } @@ -39,7 +39,7 @@ public class C { var s = Maybe() ? null : ""; Debug.Assert(s == null); - s.ToString(); // BAD (always) + s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] s = Maybe() ? null : ""; Debug.Assert(s != null); @@ -50,11 +50,11 @@ public class C { var o1 = new object(); AssertNull(o1); - o1.ToString(); // BAD (always) (false negative) + o1.ToString(); // $ MISSING: Alert[cs/dereferenced-value-is-always-null] var o2 = Maybe() ? null : ""; Assert.IsNull(o2); - o2.ToString(); // BAD (always) + o2.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } public void AssertNotNullTest() @@ -159,7 +159,7 @@ public class C s = null; do { - s.ToString(); // BAD (always) + s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] s = null; } while (s != null); @@ -167,7 +167,7 @@ public class C s = null; do { - s.ToString(); // BAD (always) + s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } while (s != null); @@ -193,7 +193,7 @@ public class C s = null; while (b) { - s.ToString(); // BAD (always) + s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] s = null; } @@ -215,7 +215,7 @@ public class C } if (s == null) - s.ToString(); // BAD (always) + s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] s = ""; if (s != null && s.Length % 2 == 0) @@ -230,11 +230,11 @@ public class C { s.ToString(); // GOOD } - s.ToString(); // BAD (always) + s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] for (s = null; s == null; s = null) { - s.ToString(); // BAD (always) + s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } for (s = ""; ; s = null) @@ -246,7 +246,7 @@ public class C public void ArrayAssignTest() { int[] a = null; - a[0] = 10; // BAD (always) + a[0] = 10; // $ Alert[cs/dereferenced-value-is-always-null] a = new int[10]; a[0] = 42; // GOOD @@ -257,8 +257,8 @@ public class C int[] ia = null; string[] sa = null; - ia[1] = 0; // BAD (always) - var temp = sa.Length; // BAD (always) + ia[1] = 0; // $ Alert[cs/dereferenced-value-is-always-null] + var temp = sa.Length; // $ Alert[cs/dereferenced-value-is-always-null] ia[1] = 0; // BAD (always), but not first temp = sa.Length; // BAD (always), but not first diff --git a/csharp/ql/test/query-tests/Nullness/D.cs b/csharp/ql/test/query-tests/Nullness/D.cs index 40419b7f577..cb3df3b3bf2 100644 --- a/csharp/ql/test/query-tests/Nullness/D.cs +++ b/csharp/ql/test/query-tests/Nullness/D.cs @@ -117,7 +117,7 @@ public class D var x = b ? null : "abc"; x = x == null ? "" : x; if (x == null) - x.ToString(); // BAD (always) + x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] else x.ToString(); // GOOD } @@ -194,7 +194,7 @@ public class D { var o = new Object(); if (o == null) - o.ToString(); // BAD (always) + o.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] o.ToString(); // GOOD try @@ -204,7 +204,7 @@ public class D catch (Exception e) { if (e == null) - e.ToString(); // BAD (always) + e.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] e.ToString(); // GOOD } @@ -214,12 +214,12 @@ public class D var o3 = "abc"; if (o3 == null) - o3.ToString(); // BAD (always) + o3.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] o3.ToString(); // GOOD var o4 = "" + null; if (o4 == null) - o4.ToString(); // BAD (always) + o4.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] o4.ToString(); // GOOD } @@ -382,7 +382,7 @@ public class D if (ioe != null) ioe = e; else - ioe.ToString(); // BAD (always) + ioe.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } public void LengthGuard2(int[] a, int[] b) diff --git a/csharp/ql/test/query-tests/Nullness/E.cs b/csharp/ql/test/query-tests/Nullness/E.cs index ec1fa161392..3dde8d97fba 100644 --- a/csharp/ql/test/query-tests/Nullness/E.cs +++ b/csharp/ql/test/query-tests/Nullness/E.cs @@ -207,7 +207,7 @@ public class E { if (s is string) return s.Length; - return s.GetHashCode(); // BAD (always) + return s.GetHashCode(); // $ Alert[cs/dereferenced-value-is-always-null] } public void Ex15(bool b) @@ -217,7 +217,7 @@ public class E x = null; x.ToString(); // BAD (maybe) if (b) - x.ToString(); // BAD (always) + x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } public void Ex16(bool b) @@ -226,7 +226,7 @@ public class E if (b) x = null; if (b) - x.ToString(); // BAD (always) + x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] x.ToString(); // BAD (maybe) } @@ -320,15 +320,15 @@ public class E { if ((s1 ?? s2) is null) { - s1.ToString(); // BAD (always) - s2.ToString(); // BAD (always) + s1.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] + s2.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } } static void Ex28() { var x = (string)null ?? null; - x.ToString(); // BAD (always) + x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } static void Ex29(string s) @@ -402,7 +402,7 @@ public class E { int? i = null; i ??= null; - return i.Value; // BAD (always) + return i.Value; // $ Alert[cs/dereferenced-value-is-always-null] } int Ex41() @@ -436,12 +436,12 @@ public class E { if (s is null) { - s.ToString(); // BAD (always) + s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null] } if (s is not not null) { - s.ToString(); // BAD (always) (FALSE NEGATIVE) + s.ToString(); // $ MISSING: Alert[cs/dereferenced-value-is-always-null] } if (s is not null) diff --git a/csharp/ql/test/query-tests/Nullness/Forwarding.cs b/csharp/ql/test/query-tests/Nullness/Forwarding.cs index fc7b69da490..122c5036567 100644 --- a/csharp/ql/test/query-tests/Nullness/Forwarding.cs +++ b/csharp/ql/test/query-tests/Nullness/Forwarding.cs @@ -33,11 +33,11 @@ class ForwardingTests if (IsNotNullWrong(s)) { - Console.WriteLine(s.Length); // BAD (always) + Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null] } AssertIsNotNull(s); - Console.WriteLine(s.Length); // GOOD (false positive) + Console.WriteLine(s.Length); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-is-always-null] } bool IsNotNull(object o) diff --git a/csharp/ql/test/query-tests/Nullness/NullAlways.qlref b/csharp/ql/test/query-tests/Nullness/NullAlways.qlref index 16785ed3e7a..9f937e60952 100644 --- a/csharp/ql/test/query-tests/Nullness/NullAlways.qlref +++ b/csharp/ql/test/query-tests/Nullness/NullAlways.qlref @@ -1 +1,2 @@ -CSI/NullAlways.ql +query: CSI/NullAlways.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/csharp/ql/test/query-tests/Nullness/NullAlwaysBad.cs b/csharp/ql/test/query-tests/Nullness/NullAlwaysBad.cs index 6f0d486f1c7..107a4f3381e 100644 --- a/csharp/ql/test/query-tests/Nullness/NullAlwaysBad.cs +++ b/csharp/ql/test/query-tests/Nullness/NullAlwaysBad.cs @@ -6,7 +6,7 @@ namespace NullAlways { void DoPrint(string s) { - if (s != null || s.Length > 0) + if (s != null || s.Length > 0) // $ Alert[cs/dereferenced-value-is-always-null] Console.WriteLine(s); } }