Skip to content

Commit d2a431e

Browse files
committed
C#: Add more nullness tests
Port many of the nullness test from Java, as well as add new tests.
1 parent 939db5a commit d2a431e

File tree

15 files changed

+2184
-323
lines changed

15 files changed

+2184
-323
lines changed

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

Lines changed: 39 additions & 269 deletions
Original file line numberDiff line numberDiff line change
@@ -2,222 +2,66 @@
22

33
class A
44
{
5-
public void notTest()
5+
public void Lock()
66
{
7-
object not_ok = null;
8-
if (!(!(!(not_ok == null))))
7+
object synchronizedAlways = null;
8+
lock (synchronizedAlways) // BAD (always)
99
{
10-
not_ok.GetHashCode();
10+
synchronizedAlways.GetHashCode(); // GOOD
1111
}
12-
object not = null;
13-
if (!(not != null))
14-
{
15-
not.GetHashCode();
16-
}
17-
}
18-
public void instanceOf()
19-
{
20-
object instanceof_ok = null;
21-
if (instanceof_ok is string)
22-
{
23-
instanceof_ok.GetHashCode();
24-
}
25-
}
26-
27-
public void locked()
28-
{
29-
object synchronized_always = null;
30-
lock (synchronized_always)
31-
{
32-
synchronized_always.GetHashCode();
33-
}
34-
}
35-
36-
public void assignIf()
37-
{
38-
string xx;
39-
string ok = null;
40-
if ((ok = (xx = null)) == null || ok.Length == 0)
41-
{
42-
}
43-
}
44-
public void assignIf2()
45-
{
46-
string ok2 = null;
47-
if (foo(ok2 = "hello") || ok2.Length == 0)
48-
{
49-
}
50-
}
51-
public void assignIfAnd()
52-
{
53-
string xx;
54-
string ok3 = null;
55-
if ((xx = (ok3 = null)) != null && ok3.Length == 0)
56-
{
57-
}
58-
}
59-
60-
61-
public bool foo(string o) { return false; }
62-
63-
public void dowhile()
64-
{
65-
string do_ok = "";
66-
do
67-
{
68-
Console.WriteLine(do_ok.Length);
69-
do_ok = null;
70-
}
71-
while (do_ok != null);
72-
73-
74-
string do_always = null;
75-
do
76-
{
77-
Console.WriteLine(do_always.Length);
78-
do_always = null;
79-
}
80-
while (do_always != null);
81-
82-
string do_maybe1 = null;
83-
do
84-
{
85-
Console.WriteLine(do_maybe1.Length);
86-
}
87-
while (do_maybe1 != null);
88-
89-
string do_maybe = "";
90-
do
91-
{
92-
Console.WriteLine(do_maybe.Length);
93-
do_maybe = null;
94-
}
95-
while (true);
96-
}
97-
98-
public void while_()
99-
{
100-
string while_ok = "";
101-
while (while_ok != null)
102-
{
103-
Console.WriteLine(while_ok.Length);
104-
while_ok = null;
105-
}
106-
107-
bool TRUE = true;
108-
string while_always = null;
109-
while (TRUE)
110-
{
111-
Console.WriteLine(while_always.Length);
112-
while_always = null;
113-
114-
}
115-
116-
117-
string while_maybe = "";
118-
while (true)
119-
{
120-
Console.WriteLine(while_maybe.Length);
121-
while_maybe = null;
122-
}
123-
124-
}
125-
126-
public void array_assign_test()
127-
{
128-
int[] array_null = null;
129-
array_null[0] = 10;
130-
131-
int[] array_ok;
132-
array_ok = new int[10];
133-
array_ok[0] = 42;
13412
}
13513

136-
public void if_()
14+
public void ArrayAssignTest()
13715
{
138-
string if_ok = "";
139-
if (if_ok != null)
140-
{
141-
Console.WriteLine(if_ok.Length);
142-
if_ok = null;
143-
}
144-
16+
int[] arrayNull = null;
17+
arrayNull[0] = 10; // BAD (always)
14518

146-
string if_always = null;
147-
if (if_always == null)
148-
{
149-
Console.WriteLine(if_always.Length);
150-
if_always = null;
151-
}
152-
153-
string if_maybe = "";
154-
if (if_maybe != null && if_maybe.Length % 2 == 0)
155-
{
156-
if_maybe = null;
157-
}
158-
Console.WriteLine(if_maybe.Length);
19+
int[] arrayOk;
20+
arrayOk = new int[10];
21+
arrayOk[0] = 42; // GOOD
15922
}
16023

161-
public void for_()
24+
public void Access()
16225
{
163-
string for_ok;
164-
for (for_ok = ""; for_ok != null; for_ok = null)
165-
{
166-
Console.WriteLine(for_ok.Length);
167-
}
168-
169-
Console.WriteLine(for_ok.Length);
26+
int[] arrayAccess = null;
27+
string[] fieldAccess = null;
28+
object methodAccess = null;
29+
object methodCall = null;
17030

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)
17135

172-
for (string for_always = null; for_always == null; for_always = null)
173-
{
174-
Console.WriteLine(for_always.Length);
175-
}
176-
177-
178-
for (string for_maybe = ""; ; for_maybe = null)
179-
{
180-
Console.WriteLine(for_maybe.Length);
181-
}
36+
Console.WriteLine(arrayAccess[1]); // GOOD
37+
Console.WriteLine(fieldAccess.Length); // GOOD
38+
tmp = methodAccess.ToString; // GOOD
39+
Console.WriteLine(methodCall.ToString()); // GOOD
18240
}
18341

184-
public void access()
42+
public void OutOrRef()
18543
{
186-
int[] arrayaccess = null;
187-
string[] fieldaccess = null;
188-
Object methodaccess = null;
189-
Object methodcall = null;
190-
191-
Console.WriteLine(arrayaccess[1]);
192-
Console.WriteLine(fieldaccess.Length);
193-
Func<String> tmp = methodaccess.ToString;
194-
Console.WriteLine(methodcall.ToString());
44+
object varOut = null;
45+
TestMethod1(out varOut);
46+
varOut.ToString(); // GOOD
19547

196-
Console.WriteLine(arrayaccess[1]);
197-
Console.WriteLine(fieldaccess.Length);
198-
tmp = methodaccess.ToString;
199-
Console.WriteLine(methodcall.ToString());
200-
}
201-
202-
public void out_or_ref()
203-
{
204-
object var_out = null;
205-
TestMethod1(out var_out);
206-
Console.WriteLine(var_out.ToString());
48+
object varRef = null;
49+
TestMethod2(ref varRef);
50+
varRef.ToString(); // BAD (always) (false negative)
20751

208-
object var_ref = null;
209-
TestMethod2(ref var_ref);
210-
Console.WriteLine(var_ref.ToString());
52+
varRef = null;
53+
TestMethod3(ref varRef);
54+
varRef.ToString(); // GOOD
21155
}
21256

213-
public void lambda_test()
57+
public void LambdaTest()
21458
{
21559
string actual = null;
21660

21761
MyDelegate fun = e => x => actual = e;
21862

21963
fun("hello")("world");
220-
Console.WriteLine(actual.Length);
64+
Console.WriteLine(actual.Length); // GOOD
22165
}
22266

22367
static void TestMethod1(out object num)
@@ -229,85 +73,11 @@ static void TestMethod2(ref object num)
22973
{
23074
}
23175

232-
static void Main() { }
233-
}
234-
public delegate MyDelegate2 MyDelegate(string e);
235-
public delegate void MyDelegate2(string e);
236-
237-
class B
238-
{
239-
public void operatorCall()
240-
{
241-
B eq_call_always = null;
242-
B b2 = null;
243-
B b3 = null;
244-
B neq_call_always = null;
245-
246-
if (eq_call_always == null)
247-
Console.WriteLine(eq_call_always.ToString());
248-
249-
if (b2 != null)
250-
Console.WriteLine(b2.ToString());
251-
252-
if (b3 == null) { }
253-
else
254-
Console.WriteLine(b3.ToString());
255-
256-
if (neq_call_always != null) { }
257-
else
258-
Console.WriteLine(neq_call_always.ToString());
259-
260-
261-
262-
}
263-
public static bool operator ==(B b1, B b2)
264-
{
265-
return Object.Equals(b1, b2);
266-
}
267-
public static bool operator !=(B b1, B b2)
268-
{
269-
return !(b1 == b2);
270-
}
271-
}
272-
public struct CoOrds
273-
{
274-
public int x, y;
275-
276-
277-
public CoOrds(int p1, int p2)
76+
static void TestMethod3(ref object num)
27877
{
279-
x = p1;
280-
y = p2;
281-
}
282-
}
283-
284-
public class Casts
285-
{
286-
void test()
287-
{
288-
object o = null;
289-
if ((object)o != null)
290-
{
291-
// GOOD
292-
var eq = o.Equals(o);
293-
}
294-
}
295-
}
296-
297-
public class Delegates
298-
{
299-
delegate void Del();
300-
301-
class Foo
302-
{
303-
public static void Run(Del d) { }
304-
public void Bar() { }
78+
num = 42;
30579
}
30680

307-
void F()
308-
{
309-
Foo foo = null;
310-
Foo.Run(delegate { foo = new Foo(); });
311-
foo.Bar();
312-
}
81+
public delegate MyDelegate2 MyDelegate(string e);
82+
public delegate void MyDelegate2(string e);
31383
}

0 commit comments

Comments
 (0)