Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 631b424

Browse files
committed
C#: Add a nullness test
1 parent 742bd1c commit 631b424

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ public void Ex13(bool b)
198198
var o = b ? null : "";
199199
o.M1(); // GOOD
200200
if (b)
201-
o.M2(); // BAD (maybe)
201+
o.M2(); // BAD (maybe)
202202
else
203-
o.Select(x => x); // BAD (maybe)
203+
o.Select(x => x); // BAD (maybe)
204204
}
205205

206206
public int Ex14(string s)
207207
{
208208
if (s is string)
209-
return s.Length;
209+
return s.Length;
210210
return s.GetHashCode(); // BAD (always)
211211
}
212212

@@ -362,6 +362,8 @@ static void Ex33(string s, object o)
362362
if (x != (string)null)
363363
x.ToString(); // GOOD
364364
}
365+
366+
static int Ex34(string s = null) => s.Length; // BAD (maybe) False negative
365367
}
366368

367369
public static class Extensions

csharp/ql/test/query-tests/Nullness/NullMaybe.expected

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ nodes
321321
| E.cs:192:17:192:17 | access to parameter o |
322322
| E.cs:198:13:198:29 | [b (line 196): false] SSA def(o) |
323323
| E.cs:198:13:198:29 | [b (line 196): true] SSA def(o) |
324-
| E.cs:201:11:201:11 | access to local variable o |
325-
| E.cs:203:11:203:11 | access to local variable o |
324+
| E.cs:201:13:201:13 | access to local variable o |
325+
| E.cs:203:13:203:13 | access to local variable o |
326326
| E.cs:206:28:206:28 | SSA param(s) |
327327
| E.cs:210:16:210:16 | access to parameter s |
328328
| E.cs:217:13:217:20 | [b (line 213): true] SSA def(x) |
@@ -664,8 +664,8 @@ edges
664664
| E.cs:181:9:183:9 | {...} | E.cs:184:9:187:9 | if (...) ... |
665665
| E.cs:184:9:187:9 | if (...) ... | E.cs:186:13:186:15 | access to parameter obj |
666666
| E.cs:190:29:190:29 | SSA param(o) | E.cs:192:17:192:17 | access to parameter o |
667-
| E.cs:198:13:198:29 | [b (line 196): false] SSA def(o) | E.cs:203:11:203:11 | access to local variable o |
668-
| E.cs:198:13:198:29 | [b (line 196): true] SSA def(o) | E.cs:201:11:201:11 | access to local variable o |
667+
| E.cs:198:13:198:29 | [b (line 196): false] SSA def(o) | E.cs:203:13:203:13 | access to local variable o |
668+
| E.cs:198:13:198:29 | [b (line 196): true] SSA def(o) | E.cs:201:13:201:13 | access to local variable o |
669669
| E.cs:206:28:206:28 | SSA param(s) | E.cs:210:16:210:16 | access to parameter s |
670670
| E.cs:217:13:217:20 | [b (line 213): true] SSA def(x) | E.cs:218:9:218:9 | access to local variable x |
671671
| E.cs:217:13:217:20 | [b (line 213): true] SSA def(x) | E.cs:220:13:220:13 | access to local variable x |
@@ -771,8 +771,8 @@ edges
771771
| E.cs:186:13:186:15 | access to parameter obj | E.cs:173:29:173:31 | SSA param(obj) | E.cs:186:13:186:15 | access to parameter obj | Variable $@ may be null here as suggested by $@ null check. | E.cs:173:29:173:31 | obj | obj | E.cs:175:19:175:29 | ... == ... | this |
772772
| E.cs:186:13:186:15 | access to parameter obj | E.cs:173:29:173:31 | SSA param(obj) | E.cs:186:13:186:15 | access to parameter obj | Variable $@ may be null here as suggested by $@ null check. | E.cs:173:29:173:31 | obj | obj | E.cs:180:13:180:23 | ... == ... | this |
773773
| E.cs:192:17:192:17 | access to parameter o | E.cs:190:29:190:29 | SSA param(o) | E.cs:192:17:192:17 | access to parameter o | Variable $@ may be null here as suggested by $@ null check. | E.cs:190:29:190:29 | o | o | E.cs:193:17:193:17 | access to parameter o | this |
774-
| E.cs:201:11:201:11 | access to local variable o | E.cs:198:13:198:29 | [b (line 196): true] SSA def(o) | E.cs:201:11:201:11 | access to local variable o | Variable $@ may be null here because of $@ assignment. | E.cs:198:13:198:13 | o | o | E.cs:198:13:198:29 | String o = ... | this |
775-
| E.cs:203:11:203:11 | access to local variable o | E.cs:198:13:198:29 | [b (line 196): false] SSA def(o) | E.cs:203:11:203:11 | access to local variable o | Variable $@ may be null here because of $@ assignment. | E.cs:198:13:198:13 | o | o | E.cs:198:13:198:29 | String o = ... | this |
774+
| E.cs:201:13:201:13 | access to local variable o | E.cs:198:13:198:29 | [b (line 196): true] SSA def(o) | E.cs:201:13:201:13 | access to local variable o | Variable $@ may be null here because of $@ assignment. | E.cs:198:13:198:13 | o | o | E.cs:198:13:198:29 | String o = ... | this |
775+
| E.cs:203:13:203:13 | access to local variable o | E.cs:198:13:198:29 | [b (line 196): false] SSA def(o) | E.cs:203:13:203:13 | access to local variable o | Variable $@ may be null here because of $@ assignment. | E.cs:198:13:198:13 | o | o | E.cs:198:13:198:29 | String o = ... | this |
776776
| E.cs:218:9:218:9 | access to local variable x | E.cs:217:13:217:20 | [b (line 213): true] SSA def(x) | E.cs:218:9:218:9 | access to local variable x | Variable $@ may be null here because of $@ assignment. | E.cs:215:13:215:13 | x | x | E.cs:217:13:217:20 | ... = ... | this |
777777
| E.cs:230:9:230:9 | access to local variable x | E.cs:227:13:227:20 | [b (line 223): true] SSA def(x) | E.cs:230:9:230:9 | access to local variable x | Variable $@ may be null here because of $@ assignment. | E.cs:225:13:225:13 | x | x | E.cs:227:13:227:20 | ... = ... | this |
778778
| E.cs:235:16:235:16 | access to parameter i | E.cs:233:26:233:26 | SSA param(i) | E.cs:235:16:235:16 | access to parameter i | Variable $@ may be null here because it has a nullable type. | E.cs:233:26:233:26 | i | i | E.cs:233:26:233:26 | i | this |

0 commit comments

Comments
 (0)