Skip to content

Commit 46c02e7

Browse files
committed
C#: Convert tests for cs/dereferenced-value-is-always-null to use inline expectations.
1 parent 5e84c71 commit 46c02e7

File tree

9 files changed

+47
-46
lines changed

9 files changed

+47
-46
lines changed

csharp/ql/test/query-tests/Nullness/A.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class A
55
public void Lock()
66
{
77
object synchronizedAlways = null;
8-
lock (synchronizedAlways) // BAD (always)
8+
lock (synchronizedAlways) // $ Alert[cs/dereferenced-value-is-always-null]
99
{
1010
synchronizedAlways.GetHashCode(); // GOOD
1111
}
@@ -14,7 +14,7 @@ public void Lock()
1414
public void ArrayAssignTest()
1515
{
1616
int[] arrayNull = null;
17-
arrayNull[0] = 10; // BAD (always)
17+
arrayNull[0] = 10; // $ Alert[cs/dereferenced-value-is-always-null]
1818

1919
int[] arrayOk;
2020
arrayOk = new int[10];
@@ -28,10 +28,10 @@ public void Access()
2828
object methodAccess = null;
2929
object methodCall = null;
3030

31-
Console.WriteLine(arrayAccess[1]); // BAD (always)
32-
Console.WriteLine(fieldAccess.Length); // BAD (always)
33-
Func<String> tmp = methodAccess.ToString; // BAD (always)
34-
Console.WriteLine(methodCall.ToString()); // BAD (always)
31+
Console.WriteLine(arrayAccess[1]); // $ Alert[cs/dereferenced-value-is-always-null]
32+
Console.WriteLine(fieldAccess.Length); // $ Alert[cs/dereferenced-value-is-always-null]
33+
Func<String> tmp = methodAccess.ToString; // $ Alert[cs/dereferenced-value-is-always-null]
34+
Console.WriteLine(methodCall.ToString()); // $ Alert[cs/dereferenced-value-is-always-null]
3535

3636
Console.WriteLine(arrayAccess[1]); // GOOD
3737
Console.WriteLine(fieldAccess.Length); // GOOD
@@ -47,7 +47,7 @@ public void OutOrRef()
4747

4848
object varRef = null;
4949
TestMethod2(ref varRef);
50-
varRef.ToString(); // BAD (always)
50+
varRef.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
5151

5252
varRef = null;
5353
TestMethod3(ref varRef);

csharp/ql/test/query-tests/Nullness/Assert.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ void Fn(bool b)
1212

1313
s = b ? null : "";
1414
Assert.IsNull(s);
15-
Console.WriteLine(s.Length); // BAD (always)
15+
Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null]
1616

1717
s = b ? null : "";
1818
Assert.IsNotNull(s);
1919
Console.WriteLine(s.Length); // GOOD
2020

2121
s = b ? null : "";
2222
Assert.IsTrue(s == null);
23-
Console.WriteLine(s.Length); // BAD (always)
23+
Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null]
2424

2525
s = b ? null : "";
2626
Assert.IsTrue(s != null);
2727
Console.WriteLine(s.Length); // GOOD
2828

2929
s = b ? null : "";
3030
Assert.IsFalse(s != null);
31-
Console.WriteLine(s.Length); // BAD (always)
31+
Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null]
3232

3333
s = b ? null : "";
3434
Assert.IsFalse(s == null);
@@ -44,10 +44,10 @@ void Fn(bool b)
4444

4545
s = b ? null : "";
4646
Assert.IsTrue(s == null && b);
47-
Console.WriteLine(s.Length); // BAD (always)
47+
Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null]
4848

4949
s = b ? null : "";
5050
Assert.IsFalse(s != null || !b);
51-
Console.WriteLine(s.Length); // BAD (always)
51+
Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null]
5252
}
5353
}

csharp/ql/test/query-tests/Nullness/B.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void OperatorCall()
1010
B neqCallAlways = null;
1111

1212
if (eqCallAlways == null)
13-
eqCallAlways.ToString(); // BAD (always)
13+
eqCallAlways.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
1414

1515
if (b2 != null)
1616
b2.ToString(); // GOOD
@@ -21,7 +21,7 @@ public void OperatorCall()
2121

2222
if (neqCallAlways != null) { }
2323
else
24-
neqCallAlways.ToString(); // BAD (always)
24+
neqCallAlways.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
2525
}
2626

2727
public static bool operator ==(B b1, B b2)

csharp/ql/test/query-tests/Nullness/C.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void NotTest()
1515

1616
if (!(o != null))
1717
{
18-
o.GetHashCode(); // BAD (always)
18+
o.GetHashCode(); // $ Alert[cs/dereferenced-value-is-always-null]
1919
}
2020
}
2121

@@ -39,7 +39,7 @@ public void AssertTest()
3939
{
4040
var s = Maybe() ? null : "";
4141
Debug.Assert(s == null);
42-
s.ToString(); // BAD (always)
42+
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
4343

4444
s = Maybe() ? null : "";
4545
Debug.Assert(s != null);
@@ -50,11 +50,11 @@ public void AssertNullTest()
5050
{
5151
var o1 = new object();
5252
AssertNull(o1);
53-
o1.ToString(); // BAD (always) (false negative)
53+
o1.ToString(); // $ MISSING: Alert[cs/dereferenced-value-is-always-null]
5454

5555
var o2 = Maybe() ? null : "";
5656
Assert.IsNull(o2);
57-
o2.ToString(); // BAD (always)
57+
o2.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
5858
}
5959

6060
public void AssertNotNullTest()
@@ -159,15 +159,15 @@ public void DoWhile()
159159
s = null;
160160
do
161161
{
162-
s.ToString(); // BAD (always)
162+
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
163163
s = null;
164164
}
165165
while (s != null);
166166

167167
s = null;
168168
do
169169
{
170-
s.ToString(); // BAD (always)
170+
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
171171
}
172172
while (s != null);
173173

@@ -193,7 +193,7 @@ public void While()
193193
s = null;
194194
while (b)
195195
{
196-
s.ToString(); // BAD (always)
196+
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
197197
s = null;
198198
}
199199

@@ -215,7 +215,7 @@ public void If()
215215
}
216216

217217
if (s == null)
218-
s.ToString(); // BAD (always)
218+
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
219219

220220
s = "";
221221
if (s != null && s.Length % 2 == 0)
@@ -230,11 +230,11 @@ public void For()
230230
{
231231
s.ToString(); // GOOD
232232
}
233-
s.ToString(); // BAD (always)
233+
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
234234

235235
for (s = null; s == null; s = null)
236236
{
237-
s.ToString(); // BAD (always)
237+
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
238238
}
239239

240240
for (s = ""; ; s = null)
@@ -246,7 +246,7 @@ public void For()
246246
public void ArrayAssignTest()
247247
{
248248
int[] a = null;
249-
a[0] = 10; // BAD (always)
249+
a[0] = 10; // $ Alert[cs/dereferenced-value-is-always-null]
250250

251251
a = new int[10];
252252
a[0] = 42; // GOOD
@@ -257,8 +257,8 @@ public void Access()
257257
int[] ia = null;
258258
string[] sa = null;
259259

260-
ia[1] = 0; // BAD (always)
261-
var temp = sa.Length; // BAD (always)
260+
ia[1] = 0; // $ Alert[cs/dereferenced-value-is-always-null]
261+
var temp = sa.Length; // $ Alert[cs/dereferenced-value-is-always-null]
262262

263263
ia[1] = 0; // BAD (always), but not first
264264
temp = sa.Length; // BAD (always), but not first

csharp/ql/test/query-tests/Nullness/D.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void F(bool b)
117117
var x = b ? null : "abc";
118118
x = x == null ? "" : x;
119119
if (x == null)
120-
x.ToString(); // BAD (always)
120+
x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
121121
else
122122
x.ToString(); // GOOD
123123
}
@@ -194,7 +194,7 @@ public void ClearNotNull()
194194
{
195195
var o = new Object();
196196
if (o == null)
197-
o.ToString(); // BAD (always)
197+
o.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
198198
o.ToString(); // GOOD
199199

200200
try
@@ -204,7 +204,7 @@ public void ClearNotNull()
204204
catch (Exception e)
205205
{
206206
if (e == null)
207-
e.ToString(); // BAD (always)
207+
e.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
208208
e.ToString(); // GOOD
209209
}
210210

@@ -214,12 +214,12 @@ public void ClearNotNull()
214214

215215
var o3 = "abc";
216216
if (o3 == null)
217-
o3.ToString(); // BAD (always)
217+
o3.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
218218
o3.ToString(); // GOOD
219219

220220
var o4 = "" + null;
221221
if (o4 == null)
222-
o4.ToString(); // BAD (always)
222+
o4.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
223223
o4.ToString(); // GOOD
224224
}
225225

@@ -382,7 +382,7 @@ void Test(Exception e, bool b)
382382
if (ioe != null)
383383
ioe = e;
384384
else
385-
ioe.ToString(); // BAD (always)
385+
ioe.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
386386
}
387387

388388
public void LengthGuard2(int[] a, int[] b)

csharp/ql/test/query-tests/Nullness/E.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public int Ex14(string s)
207207
{
208208
if (s is string)
209209
return s.Length;
210-
return s.GetHashCode(); // BAD (always)
210+
return s.GetHashCode(); // $ Alert[cs/dereferenced-value-is-always-null]
211211
}
212212

213213
public void Ex15(bool b)
@@ -217,7 +217,7 @@ public void Ex15(bool b)
217217
x = null;
218218
x.ToString(); // BAD (maybe)
219219
if (b)
220-
x.ToString(); // BAD (always)
220+
x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
221221
}
222222

223223
public void Ex16(bool b)
@@ -226,7 +226,7 @@ public void Ex16(bool b)
226226
if (b)
227227
x = null;
228228
if (b)
229-
x.ToString(); // BAD (always)
229+
x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
230230
x.ToString(); // BAD (maybe)
231231
}
232232

@@ -320,15 +320,15 @@ static void Ex27(string s1, string s2)
320320
{
321321
if ((s1 ?? s2) is null)
322322
{
323-
s1.ToString(); // BAD (always)
324-
s2.ToString(); // BAD (always)
323+
s1.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
324+
s2.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
325325
}
326326
}
327327

328328
static void Ex28()
329329
{
330330
var x = (string)null ?? null;
331-
x.ToString(); // BAD (always)
331+
x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
332332
}
333333

334334
static void Ex29(string s)
@@ -402,7 +402,7 @@ int Ex40()
402402
{
403403
int? i = null;
404404
i ??= null;
405-
return i.Value; // BAD (always)
405+
return i.Value; // $ Alert[cs/dereferenced-value-is-always-null]
406406
}
407407

408408
int Ex41()
@@ -436,12 +436,12 @@ static void Ex45(string s)
436436
{
437437
if (s is null)
438438
{
439-
s.ToString(); // BAD (always)
439+
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
440440
}
441441

442442
if (s is not not null)
443443
{
444-
s.ToString(); // BAD (always) (FALSE NEGATIVE)
444+
s.ToString(); // $ MISSING: Alert[cs/dereferenced-value-is-always-null]
445445
}
446446

447447
if (s is not null)

csharp/ql/test/query-tests/Nullness/Forwarding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ void Fn()
3333

3434
if (IsNotNullWrong(s))
3535
{
36-
Console.WriteLine(s.Length); // BAD (always)
36+
Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-is-always-null]
3737
}
3838

3939
AssertIsNotNull(s);
40-
Console.WriteLine(s.Length); // GOOD (false positive)
40+
Console.WriteLine(s.Length); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-is-always-null]
4141
}
4242

4343
bool IsNotNull(object o)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
CSI/NullAlways.ql
1+
query: CSI/NullAlways.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

csharp/ql/test/query-tests/Nullness/NullAlwaysBad.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Bad
66
{
77
void DoPrint(string s)
88
{
9-
if (s != null || s.Length > 0)
9+
if (s != null || s.Length > 0) // $ Alert[cs/dereferenced-value-is-always-null]
1010
Console.WriteLine(s);
1111
}
1212
}

0 commit comments

Comments
 (0)