C#: Add a tests for MaybeNullExpr.

This commit is contained in:
Michael Nebel
2026-01-14 14:27:46 +01:00
parent 4ba89230db
commit f0135e93c5
4 changed files with 35 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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 ... |

View File

@@ -0,0 +1,6 @@
import csharp
import semmle.code.csharp.dataflow.Nullness
from MaybeNullExpr e
where e.fromSource()
select e

View File

@@ -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