Skip to content

Commit 389e626

Browse files
authored
Merge pull request #2773 from hvitved/csharp/useless-assignment-to-local-default
C#: Remove false positives for `cs/useless-assignment-to-local`
2 parents 19286bd + 69d9d41 commit 389e626

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

change-notes/1.24/analysis-csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following changes in version 1.24 affect C# analysis in all applications.
2020
| 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. |
2121
| 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. |
2222
| Dereferenced variable may be null (`cs/dereferenced-value-may-be-null`) | More results | Results are reported from parameters with a default value of `null`. |
23+
| 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`. |
2324

2425
## Removal of old queries
2526

csharp/ql/src/Dead Code/DeadStoreOfLocal.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class RelevantDefinition extends AssignableDefinition {
114114
*/
115115
private predicate isDefaultLikeInitializer() {
116116
this.isInitializer() and
117-
exists(Expr e | e = this.getSource() |
117+
exists(Expr e | e = this.getSource().stripCasts() |
118118
exists(string val | val = e.getValue() |
119119
val = "0" or
120120
val = "-1" or

csharp/ql/test/query-tests/Dead Code/DeadStoreOfLocal/DeadStoreOfLocal.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ string M7(bool b)
389389
return s;
390390
return null;
391391
}
392+
393+
string M8()
394+
{
395+
string s = default; // "GOOD"
396+
s = "";
397+
return s;
398+
}
399+
400+
string M9()
401+
{
402+
var s = (string)null; // "GOOD"
403+
s = "";
404+
return s;
405+
}
392406
}
393407

394408
class Anonymous

0 commit comments

Comments
 (0)