Skip to content

Commit ed21259

Browse files
authored
Merge pull request #2568 from calumgrant/cs/assignment-to-_
C#: Remove FP in useless assignment to _
2 parents 9160fbf + 68f42a6 commit ed21259

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

change-notes/1.24/analysis-csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The following changes in version 1.24 affect C# analysis in all applications.
1313

1414
| **Query** | **Expected impact** | **Change** |
1515
|------------------------------|------------------------|-----------------------------------|
16+
| 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. |
1617

1718
## Removal of old queries
1819

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ class RelevantDefinition extends AssignableDefinition {
8383
//or
8484
//this.(AssignableDefinitions::OutRefDefinition).getTargetAccess().isOutArgument()
8585
this.(AssignableDefinitions::LocalVariableDefinition).getDeclaration() = any(LocalVariableDeclExpr lvde |
86-
lvde = any(SpecificCatchClause scc).getVariableDeclExpr() or
87-
lvde = any(ForeachStmt fs).getVariableDeclExpr()
86+
lvde = any(SpecificCatchClause scc).getVariableDeclExpr()
87+
or
88+
lvde = any(ForeachStmt fs).getVariableDeclExpr() and
89+
not lvde.getName() = "_"
8890
)
8991
or
9092
this instanceof AssignableDefinitions::PatternDefinition

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void M7()
259259
fn(() =>
260260
{
261261
var x = y; // BAD: Dead store in lambda
262-
return 0;
262+
return 0;
263263
});
264264
}
265265

@@ -425,3 +425,14 @@ int M(bool b)
425425
return i;
426426
}
427427
}
428+
429+
public static class AnonymousVariable
430+
{
431+
public static int Count<T>(this IEnumerable<T> items)
432+
{
433+
int count = 0;
434+
foreach (var _ in items) // GOOD
435+
count++;
436+
return count;
437+
}
438+
}

0 commit comments

Comments
 (0)