From d4a32476da74aaa2d35428d7c7dbd37cf2762e40 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Tue, 28 Apr 2026 10:44:16 +0200 Subject: [PATCH] C#: No need to special-case default arguments in nullness analysis --- .../lib/semmle/code/csharp/dataflow/Nullness.qll | 14 -------------- .../test/query-tests/Nullness/NullMaybe.expected | 2 +- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll index 14d343dedfb..63205e52ae5 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll @@ -181,16 +181,6 @@ private predicate hasMultipleParamsArguments(Call c) { ) } -private predicate isNullDefaultArgument(Ssa::ParameterDefinition def, AlwaysNullExpr arg) { - exists(AssignableDefinitions::ImplicitParameterDefinition pdef, Parameter p | - p = def.getParameter() - | - p = pdef.getParameter().getUnboundDeclaration() and - arg = p.getDefaultValue() and - not arg.getEnclosingCallable().getEnclosingCallable*() instanceof TestMethod - ) -} - /** Holds if `def` is an SSA definition that may be `null`. */ private predicate defMaybeNull(Ssa::Definition def, ControlFlowNode node, string msg, Element reason) { not nonNullDef(def) and @@ -216,10 +206,6 @@ private predicate defMaybeNull(Ssa::Definition def, ControlFlowNode node, string else msg = "because of $@ potential null argument" ) or - isNullDefaultArgument(def, reason) and - node = def.getControlFlowNode() and - msg = "because the parameter has a null default value" - or // If the source of a variable is `null` then the variable may be `null` exists(AssignableDefinition adef | adef = def.(Ssa::ExplicitDefinition).getADefinition() | adef.getSource() = maybeNullExpr(node.asExpr()) and diff --git a/csharp/ql/test/query-tests/Nullness/NullMaybe.expected b/csharp/ql/test/query-tests/Nullness/NullMaybe.expected index 3f5219d8c0c..ba8c7eb9091 100644 --- a/csharp/ql/test/query-tests/Nullness/NullMaybe.expected +++ b/csharp/ql/test/query-tests/Nullness/NullMaybe.expected @@ -52,7 +52,7 @@ | E.cs:302:9:302:9 | access to local variable s | Variable $@ may be null at this access because of $@ assignment. | E.cs:301:13:301:13 | s | s | E.cs:301:13:301:27 | String s = ... | this | | E.cs:343:9:343:9 | access to local variable x | Variable $@ may be null at this access because of $@ assignment. | E.cs:342:13:342:13 | x | x | E.cs:342:13:342:32 | String x = ... | this | | E.cs:349:9:349:9 | access to local variable x | Variable $@ may be null at this access because of $@ assignment. | E.cs:348:17:348:17 | x | x | E.cs:348:17:348:36 | dynamic x = ... | this | -| E.cs:366:41:366:41 | access to parameter s | Variable $@ may be null at this access because the parameter has a null default value. | E.cs:366:28:366:28 | s | s | E.cs:366:32:366:35 | null | this | +| E.cs:366:41:366:41 | access to parameter s | Variable $@ may be null at this access because of $@ assignment. | E.cs:366:28:366:28 | s | s | E.cs:366:32:366:35 | null | this | | E.cs:375:20:375:20 | access to local variable s | Variable $@ may be null at this access because of $@ assignment. | E.cs:374:17:374:17 | s | s | E.cs:374:17:374:31 | String s = ... | this | | E.cs:417:34:417:34 | access to parameter i | Variable $@ may be null at this access because it has a nullable type. | E.cs:415:27:415:27 | i | i | E.cs:415:27:415:27 | i | this | | E.cs:423:38:423:38 | access to parameter i | Variable $@ may be null at this access because it has a nullable type. | E.cs:420:27:420:27 | i | i | E.cs:420:27:420:27 | i | this |