diff --git a/change-notes/1.24/analysis-csharp.md b/change-notes/1.24/analysis-csharp.md index bf2ae180eb15..e76890135061 100644 --- a/change-notes/1.24/analysis-csharp.md +++ b/change-notes/1.24/analysis-csharp.md @@ -20,6 +20,7 @@ The following changes in version 1.24 affect C# analysis in all applications. | Useless assignment to local variable (`cs/useless-assignment-to-local`) | Fewer false positive results | Results have been removed when the variable is named `_` in a `foreach` statement. | | Potentially dangerous use of non-short-circuit logic (`cs/non-short-circuit`) | Fewer false positive results | Results have been removed when the expression contains an `out` parameter. | | Dereferenced variable may be null (`cs/dereferenced-value-may-be-null`) | More results | Results are reported from parameters with a default value of `null`. | +| Useless assignment to local variable (`cs/useless-assignment-to-local`) | Fewer false positive results | Results have been removed when the value assigned is an (implicitly or explicitly) cast default-like value. For example, `var s = (string)null` and `string s = default`. | ## Removal of old queries diff --git a/csharp/ql/src/Dead Code/DeadStoreOfLocal.ql b/csharp/ql/src/Dead Code/DeadStoreOfLocal.ql index 7181879ef8c2..2e8d1b92e02a 100644 --- a/csharp/ql/src/Dead Code/DeadStoreOfLocal.ql +++ b/csharp/ql/src/Dead Code/DeadStoreOfLocal.ql @@ -114,7 +114,7 @@ class RelevantDefinition extends AssignableDefinition { */ private predicate isDefaultLikeInitializer() { this.isInitializer() and - exists(Expr e | e = this.getSource() | + exists(Expr e | e = this.getSource().stripCasts() | exists(string val | val = e.getValue() | val = "0" or val = "-1" or diff --git a/csharp/ql/test/query-tests/Dead Code/DeadStoreOfLocal/DeadStoreOfLocal.cs b/csharp/ql/test/query-tests/Dead Code/DeadStoreOfLocal/DeadStoreOfLocal.cs index 0c6a4f44bbcd..b787401f9411 100644 --- a/csharp/ql/test/query-tests/Dead Code/DeadStoreOfLocal/DeadStoreOfLocal.cs +++ b/csharp/ql/test/query-tests/Dead Code/DeadStoreOfLocal/DeadStoreOfLocal.cs @@ -389,6 +389,20 @@ string M7(bool b) return s; return null; } + + string M8() + { + string s = default; // "GOOD" + s = ""; + return s; + } + + string M9() + { + var s = (string)null; // "GOOD" + s = ""; + return s; + } } class Anonymous