Skip to content

Commit bf20e95

Browse files
authored
Merge pull request #22492 from aschackmull/csharp/foreach-rename
C#: Rename ForeachStmt to ForEachStmt.
2 parents ac14085 + a5e733c commit bf20e95

28 files changed

Lines changed: 65 additions & 56 deletions

csharp/ql/consistency-queries/SsaConsistency.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ query predicate localDeclWithSsaDef(LocalVariableDeclExpr d) {
1010
exists(SsaExplicitWrite def |
1111
d = def.getDefinition().(AssignableDefinitions::LocalVariableDefinition).getDeclaration()
1212
|
13-
not d = any(ForeachStmt fs).getVariableDeclExpr() and
13+
not d = any(ForEachStmt fs).getVariableDeclExpr() and
1414
not d = any(SpecificCatchClause scc).getVariableDeclExpr() and
1515
not d.getVariable().getType() instanceof Struct and
1616
not d instanceof PatternExpr and

csharp/ql/lib/Linq/Helpers.qll

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ private import semmle.code.csharp.frameworks.system.collections.Generic as Gener
88
private import semmle.code.csharp.frameworks.system.Collections as Collections
99

1010
//#################### PREDICATES ####################
11-
private Stmt firstStmt(ForeachStmt fes) {
11+
private Stmt firstStmt(ForEachStmt fes) {
1212
if fes.getBody() instanceof BlockStmt
1313
then result = fes.getBody().(BlockStmt).getStmt(0)
1414
else result = fes.getBody()
1515
}
1616

17-
private int numStmts(ForeachStmt fes) {
17+
private int numStmts(ForEachStmt fes) {
1818
if fes.getBody() instanceof BlockStmt
1919
then result = count(fes.getBody().(BlockStmt).getAStmt())
2020
else result = 1
@@ -53,12 +53,15 @@ predicate isIEnumerableType(ValueOrRefType t) {
5353
)
5454
}
5555

56+
/** DEPRECATED: Use `ForEachStmtGenericEnumerable` instead. */
57+
deprecated class ForeachStmtGenericEnumerable = ForEachStmtGenericEnumerable;
58+
5659
/**
5760
* A class of foreach statements where the iterable expression
5861
* supports the use of the LINQ extension methods on `IEnumerable<T>`.
5962
*/
60-
class ForeachStmtGenericEnumerable extends ForeachStmt {
61-
ForeachStmtGenericEnumerable() {
63+
class ForEachStmtGenericEnumerable extends ForEachStmt {
64+
ForEachStmtGenericEnumerable() {
6265
exists(ValueOrRefType t | t = this.getIterableExpr().getType() |
6366
t.getABaseType*().getUnboundDeclaration() instanceof
6467
GenericCollections::SystemCollectionsGenericIEnumerableTInterface or
@@ -67,12 +70,15 @@ class ForeachStmtGenericEnumerable extends ForeachStmt {
6770
}
6871
}
6972

73+
/** DEPRECATED: Use `ForEachStmtEnumerable` instead. */
74+
deprecated class ForeachStmtEnumerable = ForEachStmtEnumerable;
75+
7076
/**
7177
* A class of foreach statements where the iterable expression
7278
* supports the use of the LINQ extension methods on `IEnumerable`.
7379
*/
74-
class ForeachStmtEnumerable extends ForeachStmt {
75-
ForeachStmtEnumerable() {
80+
class ForEachStmtEnumerable extends ForEachStmt {
81+
ForEachStmtEnumerable() {
7682
exists(ValueOrRefType t | t = this.getIterableExpr().getType() |
7783
t.getABaseType*() instanceof Collections::SystemCollectionsIEnumerableInterface or
7884
t.(ArrayType).getRank() = 1
@@ -82,11 +88,11 @@ class ForeachStmtEnumerable extends ForeachStmt {
8288

8389
/**
8490
* Holds if `foreach` statement `fes` could be converted to a `.All()` call.
85-
* That is, the `ForeachStmt` contains a single `if` with a condition that
91+
* That is, the `ForEachStmt` contains a single `if` with a condition that
8692
* accesses the loop variable and with a body that assigns `false` to a variable
8793
* and `break`s out of the `foreach`.
8894
*/
89-
predicate missedAllOpportunity(ForeachStmtGenericEnumerable fes) {
95+
predicate missedAllOpportunity(ForEachStmtGenericEnumerable fes) {
9096
exists(IfStmt is |
9197
// The loop contains an if statement with no else case, and nothing else.
9298
is = firstStmt(fes) and
@@ -110,7 +116,7 @@ predicate missedAllOpportunity(ForeachStmtGenericEnumerable fes) {
110116
* block, the access is a cast, and the first statement is a
111117
* local variable declaration statement `s`.
112118
*/
113-
predicate missedCastOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclStmt s) {
119+
predicate missedCastOpportunity(ForEachStmtEnumerable fes, LocalVariableDeclStmt s) {
114120
s = firstStmt(fes) and
115121
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
116122
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -127,7 +133,7 @@ predicate missedCastOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclStmt
127133
* block, the access is a cast with the `as` operator, and the first statement
128134
* is a local variable declaration statement `s`.
129135
*/
130-
predicate missedOfTypeOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclStmt s) {
136+
predicate missedOfTypeOpportunity(ForEachStmtEnumerable fes, LocalVariableDeclStmt s) {
131137
s = firstStmt(fes) and
132138
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
133139
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -145,7 +151,7 @@ predicate missedOfTypeOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclSt
145151
* local variable declaration statement `s`, and the initializer does not
146152
* contain an `await` expression (since `Select` does not support async lambdas).
147153
*/
148-
predicate missedSelectOpportunity(ForeachStmtGenericEnumerable fes, LocalVariableDeclStmt s) {
154+
predicate missedSelectOpportunity(ForEachStmtGenericEnumerable fes, LocalVariableDeclStmt s) {
149155
s = firstStmt(fes) and
150156
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
151157
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -160,7 +166,7 @@ predicate missedSelectOpportunity(ForeachStmtGenericEnumerable fes, LocalVariabl
160166
* variable, and the body of the `if` is either a `continue` or there's nothing
161167
* else in the loop than the `if`.
162168
*/
163-
predicate missedWhereOpportunity(ForeachStmtGenericEnumerable fes, IfStmt is) {
169+
predicate missedWhereOpportunity(ForEachStmtGenericEnumerable fes, IfStmt is) {
164170
// The very first thing the foreach loop does is test its iteration variable.
165171
is = firstStmt(fes) and
166172
exists(VariableAccess va |

csharp/ql/lib/semmle/code/csharp/Stmt.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class DefaultCase extends CaseStmt, LabeledStmt {
305305
*
306306
* Either a `while` statement (`WhileStmt`), a `do`-`while` statement
307307
* (`DoStmt`), a `for` statement (`ForStmt`), or a `foreach` statement
308-
* (`ForeachStmt`).
308+
* (`ForEachStmt`).
309309
*/
310310
class LoopStmt extends Stmt, @loop_stmt {
311311
/** Gets the body of this loop statement. */
@@ -422,6 +422,9 @@ class ForStmt extends LoopStmt, @for_stmt {
422422
override string getAPrimaryQlClass() { result = "ForStmt" }
423423
}
424424

425+
/** DEPRECATED: Use `ForEachStmt` instead. */
426+
deprecated class ForeachStmt = ForEachStmt;
427+
425428
/**
426429
* A `foreach` loop, for example
427430
*
@@ -431,7 +434,7 @@ class ForStmt extends LoopStmt, @for_stmt {
431434
* }
432435
* ```
433436
*/
434-
class ForeachStmt extends LoopStmt, @foreach_stmt {
437+
class ForEachStmt extends LoopStmt, @foreach_stmt {
435438
/**
436439
* Gets the local variable of this `foreach` loop, if any.
437440
*
@@ -564,7 +567,7 @@ class ForeachStmt extends LoopStmt, @foreach_stmt {
564567

565568
override string toString() { result = "foreach (... ... in ...) ..." }
566569

567-
override string getAPrimaryQlClass() { result = "ForeachStmt" }
570+
override string getAPrimaryQlClass() { result = "ForEachStmt" }
568571
}
569572

570573
/**

csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ module Ast implements AstSig<Location> {
188188
AstNode getUpdate(int index) { result = super.getUpdate(index) }
189189
}
190190

191-
final private class FinalForeachStmt = CS::ForeachStmt;
191+
final private class FinalForEachStmt = CS::ForEachStmt;
192192

193-
class ForEachStmt extends FinalForeachStmt {
193+
class ForEachStmt extends FinalForEachStmt {
194194
Expr getVariable() {
195195
result = this.getVariableDeclExpr() or result = this.getVariableDeclTuple()
196196
}

csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private predicate nonNullDef(SsaExplicitWrite def) {
116116
any(AssignableDefinitions::LocalVariableDefinition d |
117117
d.getExpr() = any(SpecificCatchClause scc).getVariableDeclExpr()
118118
or
119-
d.getExpr() = any(ForeachStmt fs).getAVariableDeclExpr()
119+
d.getExpr() = any(ForEachStmt fs).getAVariableDeclExpr()
120120
)
121121
)
122122
}
@@ -306,7 +306,7 @@ class Dereference extends G::DereferenceableExpr {
306306
or
307307
this = any(LockStmt stmt).getExpr()
308308
or
309-
this = any(ForeachStmt stmt).getIterableExpr()
309+
this = any(ForEachStmt stmt).getIterableExpr()
310310
or
311311
exists(ExtensionMethodCall emc, Parameter p |
312312
this = emc.getArgumentForParameter(p) and

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ private predicate readContentStep(Node node1, Content c, Node node2) {
22072207
c instanceof ElementContent
22082208
or
22092209
exists(
2210-
ForeachStmt fs, SsaExplicitWrite def, AssignableDefinitions::LocalVariableDefinition defTo
2210+
ForEachStmt fs, SsaExplicitWrite def, AssignableDefinitions::LocalVariableDefinition defTo
22112211
|
22122212
node1.asExpr() = fs.getIterableExpr() and
22132213
defTo.getDeclaration() = fs.getVariableDeclExpr() and

csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ class QualifiableExpr extends Expr, @qualifiable_expr {
11071107
private Expr getAnAssignOrForeachChild() {
11081108
result = any(AssignExpr e).getLeftOperand()
11091109
or
1110-
result = any(ForeachStmt fs).getVariableDeclTuple()
1110+
result = any(ForEachStmt fs).getVariableDeclTuple()
11111111
or
11121112
result = getAnAssignOrForeachChild().getAChildExpr()
11131113
}

csharp/ql/src/API Abuse/NoDisposeCallOnLocalIDisposable.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module DisposeCallOnLocalIDisposableConfig implements DataFlow::ConfigSig {
5959
exists(UsingStmt us | us.getAnExpr() = e)
6060
or
6161
// Foreach calls Dispose
62-
exists(ForeachStmt fs | fs.getIterableExpr() = e)
62+
exists(ForEachStmt fs | fs.getIterableExpr() = e)
6363
or
6464
// As are disposables on which the Dispose method is called explicitly
6565
exists(MethodCall mc |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class RelevantDefinition extends AssignableDefinition {
3131
any(LocalVariableDeclExpr lvde |
3232
lvde = any(SpecificCatchClause scc).getVariableDeclExpr()
3333
or
34-
lvde = any(ForeachStmt fs).getVariableDeclExpr() and
34+
lvde = any(ForEachStmt fs).getVariableDeclExpr() and
3535
not lvde.getName() = "_"
3636
)
3737
or

csharp/ql/src/Language Abuse/ForeachCapture.ql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ predicate lambdaCaptures(AnonymousFunctionExpr lambda, Variable v) {
2323
exists(VariableAccess va | va.getEnclosingCallable() = lambda | va.getTarget() = v)
2424
}
2525

26-
predicate lambdaCapturesLoopVariable(AnonymousFunctionExpr lambda, ForeachStmt loop, Variable v) {
26+
predicate lambdaCapturesLoopVariable(AnonymousFunctionExpr lambda, ForEachStmt loop, Variable v) {
2727
lambdaCaptures(lambda, v) and
28-
inForeachStmtBody(loop, lambda) and
28+
inForEachStmtBody(loop, lambda) and
2929
loop.getVariable() = v
3030
}
3131

32-
predicate inForeachStmtBody(ForeachStmt loop, Element e) {
32+
predicate inForEachStmtBody(ForEachStmt loop, Element e) {
3333
e = loop.getBody()
3434
or
3535
exists(Element mid |
36-
inForeachStmtBody(loop, mid) and
36+
inForEachStmtBody(loop, mid) and
3737
e = mid.getAChild()
3838
)
3939
}
@@ -53,7 +53,7 @@ module LambdaDataFlow {
5353
exists(DataFlow::Node sink | flow(DataFlow::exprNode(lambda), sink) |
5454
storage = getAssignmentTarget(sink.asExpr())
5555
) and
56-
exists(ForeachStmt loop | lambdaCapturesLoopVariable(lambda, loop, loopVar) |
56+
exists(ForEachStmt loop | lambdaCapturesLoopVariable(lambda, loop, loopVar) |
5757
not declaredInsideLoop(loop, storage)
5858
)
5959
}
@@ -103,9 +103,9 @@ Element getCollectionAssignmentTarget(Expr e) {
103103
}
104104

105105
// Variable v is declared inside the loop body
106-
predicate declaredInsideLoop(ForeachStmt loop, LocalVariable v) {
106+
predicate declaredInsideLoop(ForEachStmt loop, LocalVariable v) {
107107
exists(LocalVariableDeclStmt decl | decl.getVariableDeclExpr(_).getVariable() = v |
108-
inForeachStmtBody(loop, decl)
108+
inForEachStmtBody(loop, decl)
109109
)
110110
}
111111

0 commit comments

Comments
 (0)