diff --git a/csharp/ql/test/library-tests/dataflow/nullness/MaybeNullExpr.cs b/csharp/ql/test/library-tests/dataflow/nullness/MaybeNullExpr.cs new file mode 100644 index 00000000000..34c59df8bbd --- /dev/null +++ b/csharp/ql/test/library-tests/dataflow/nullness/MaybeNullExpr.cs @@ -0,0 +1,21 @@ +using System; + +public class C +{ + string Prop { get; set; } + + void M(object o, bool b) + { + // Conditional expr might be null. + var conditionalExpr = b ? new object() : null; + + // Null-coalescing expr might be null as the right operand is null. + var nullCoalescing = o ?? null; + + // Cast might be null. + var c = o as C; + + // Conditional access might be null as the qualifier might be null. + var s1 = (o as C)?.Prop; + } +} diff --git a/csharp/ql/test/library-tests/dataflow/nullness/maybeNullExpr.expected b/csharp/ql/test/library-tests/dataflow/nullness/maybeNullExpr.expected new file mode 100644 index 00000000000..4d8b00dd90c --- /dev/null +++ b/csharp/ql/test/library-tests/dataflow/nullness/maybeNullExpr.expected @@ -0,0 +1,6 @@ +| MaybeNullExpr.cs:10:31:10:53 | ... ? ... : ... | +| MaybeNullExpr.cs:10:50:10:53 | null | +| MaybeNullExpr.cs:13:30:13:38 | ... ?? ... | +| MaybeNullExpr.cs:13:35:13:38 | null | +| MaybeNullExpr.cs:16:17:16:22 | ... as ... | +| MaybeNullExpr.cs:19:19:19:24 | ... as ... | diff --git a/csharp/ql/test/library-tests/dataflow/nullness/maybeNullExpr.ql b/csharp/ql/test/library-tests/dataflow/nullness/maybeNullExpr.ql new file mode 100644 index 00000000000..3b5531ccb3a --- /dev/null +++ b/csharp/ql/test/library-tests/dataflow/nullness/maybeNullExpr.ql @@ -0,0 +1,6 @@ +import csharp +import semmle.code.csharp.dataflow.Nullness + +from MaybeNullExpr e +where e.fromSource() +select e diff --git a/csharp/ql/test/library-tests/dataflow/nullness/options b/csharp/ql/test/library-tests/dataflow/nullness/options new file mode 100644 index 00000000000..75c39b4541b --- /dev/null +++ b/csharp/ql/test/library-tests/dataflow/nullness/options @@ -0,0 +1,2 @@ +semmle-extractor-options: /nostdlib /noconfig +semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj