Skip to content

Commit 57f1145

Browse files
committed
C#: Add some synthetic library extensions methods and tests for cs/dereferenced-value-is-always-null.
1 parent 3037df7 commit 57f1145

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Library;
2+
3+
public class F
4+
{
5+
public void M1()
6+
{
7+
object o = null;
8+
o.Accept(); // $ Alert[cs/dereferenced-value-is-always-null]
9+
}
10+
11+
public void M2()
12+
{
13+
object? o = null;
14+
o.AcceptNullable();
15+
}
16+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#select
12
| A.cs:8:15:8:32 | access to local variable synchronizedAlways | Variable $@ is always null at this dereference. | A.cs:7:16:7:33 | synchronizedAlways | synchronizedAlways |
23
| A.cs:17:9:17:17 | access to local variable arrayNull | Variable $@ is always null at this dereference. | A.cs:16:15:16:23 | arrayNull | arrayNull |
34
| A.cs:31:27:31:37 | access to local variable arrayAccess | Variable $@ is always null at this dereference. | A.cs:26:15:26:25 | arrayAccess | arrayAccess |
@@ -38,6 +39,10 @@
3839
| E.cs:331:9:331:9 | access to local variable x | Variable $@ is always null at this dereference. | E.cs:330:13:330:13 | x | x |
3940
| E.cs:405:16:405:16 | access to local variable i | Variable $@ is always null at this dereference. | E.cs:403:14:403:14 | i | i |
4041
| E.cs:439:13:439:13 | access to parameter s | Variable $@ is always null at this dereference. | E.cs:435:29:435:29 | s | s |
42+
| F.cs:8:9:8:9 | access to local variable o | Variable $@ is always null at this dereference. | F.cs:7:16:7:16 | o | o |
43+
| F.cs:14:9:14:9 | access to local variable o | Variable $@ is always null at this dereference. | F.cs:13:17:13:17 | o | o |
4144
| Forwarding.cs:36:31:36:31 | access to local variable s | Variable $@ is always null at this dereference. | Forwarding.cs:7:16:7:16 | s | s |
4245
| Forwarding.cs:40:27:40:27 | access to local variable s | Variable $@ is always null at this dereference. | Forwarding.cs:7:16:7:16 | s | s |
4346
| NullAlwaysBad.cs:9:30:9:30 | access to parameter s | Variable $@ is always null at this dereference. | NullAlwaysBad.cs:7:29:7:29 | s | s |
47+
testFailures
48+
| F.cs:14:9:14:9 | Variable $@ is always null at this dereference. | Unexpected result: Alert |
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
semmle-extractor-options: /nostdlib /noconfig
22
semmle-extractor-options: --load-sources-from-project:${testdir}/../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj
3-
semmle-extractor-options: ${testdir}/../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs
3+
semmle-extractor-options: ${testdir}/../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs
4+
semmle-extractor-options: ${testdir}/../../resources/stubs/Library.cs
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Library;
2+
3+
/*
4+
* This file is for making stubs for library methods used for testing purposes.
5+
* The file is located in the stubs folder, because then the code is not
6+
* recognized as being from source.
7+
*/
8+
public static class MyExtensions
9+
{
10+
public static void Accept(this object o) { }
11+
12+
public static void AcceptNullable(this object? o) { }
13+
}

0 commit comments

Comments
 (0)