Skip to content

Commit 46f8cf6

Browse files
committed
C#: Rename ForeachStmt to ForEachStmt.
1 parent 738c91f commit 46f8cf6

22 files changed

Lines changed: 53 additions & 44 deletions

File tree

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
@@ -33,12 +33,15 @@ predicate isIEnumerableType(ValueOrRefType t) {
3333
)
3434
}
3535

36+
/** DEPRECATED: Use `ForEachStmtGenericEnumerable` instead. */
37+
deprecated class ForeachStmtGenericEnumerable = ForEachStmtGenericEnumerable;
38+
3639
/**
3740
* A class of foreach statements where the iterable expression
3841
* supports the use of the LINQ extension methods on `IEnumerable<T>`.
3942
*/
40-
class ForeachStmtGenericEnumerable extends ForeachStmt {
41-
ForeachStmtGenericEnumerable() {
43+
class ForEachStmtGenericEnumerable extends ForEachStmt {
44+
ForEachStmtGenericEnumerable() {
4245
exists(ValueOrRefType t | t = this.getIterableExpr().getType() |
4346
t.getABaseType*().getUnboundDeclaration() instanceof
4447
GenericCollections::SystemCollectionsGenericIEnumerableTInterface or
@@ -47,12 +50,15 @@ class ForeachStmtGenericEnumerable extends ForeachStmt {
4750
}
4851
}
4952

53+
/** DEPRECATED: Use `ForEachStmtEnumerable` instead. */
54+
deprecated class ForeachStmtEnumerable = ForEachStmtEnumerable;
55+
5056
/**
5157
* A class of foreach statements where the iterable expression
5258
* supports the use of the LINQ extension methods on `IEnumerable`.
5359
*/
54-
class ForeachStmtEnumerable extends ForeachStmt {
55-
ForeachStmtEnumerable() {
60+
class ForEachStmtEnumerable extends ForEachStmt {
61+
ForEachStmtEnumerable() {
5662
exists(ValueOrRefType t | t = this.getIterableExpr().getType() |
5763
t.getABaseType*() instanceof Collections::SystemCollectionsIEnumerableInterface or
5864
t.(ArrayType).getRank() = 1
@@ -62,11 +68,11 @@ class ForeachStmtEnumerable extends ForeachStmt {
6268

6369
/**
6470
* Holds if `foreach` statement `fes` could be converted to a `.All()` call.
65-
* That is, the `ForeachStmt` contains a single `if` with a condition that
71+
* That is, the `ForEachStmt` contains a single `if` with a condition that
6672
* accesses the loop variable and with a body that assigns `false` to a variable
6773
* and `break`s out of the `foreach`.
6874
*/
69-
predicate missedAllOpportunity(ForeachStmtGenericEnumerable fes) {
75+
predicate missedAllOpportunity(ForEachStmtGenericEnumerable fes) {
7076
exists(IfStmt is |
7177
// The loop contains an if statement with no else case, and nothing else.
7278
is = firstStmt(fes) and
@@ -90,7 +96,7 @@ predicate missedAllOpportunity(ForeachStmtGenericEnumerable fes) {
9096
* block, the access is a cast, and the first statement is a
9197
* local variable declaration statement `s`.
9298
*/
93-
predicate missedCastOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclStmt s) {
99+
predicate missedCastOpportunity(ForEachStmtEnumerable fes, LocalVariableDeclStmt s) {
94100
s = firstStmt(fes) and
95101
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
96102
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -107,7 +113,7 @@ predicate missedCastOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclStmt
107113
* block, the access is a cast with the `as` operator, and the first statement
108114
* is a local variable declaration statement `s`.
109115
*/
110-
predicate missedOfTypeOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclStmt s) {
116+
predicate missedOfTypeOpportunity(ForEachStmtEnumerable fes, LocalVariableDeclStmt s) {
111117
s = firstStmt(fes) and
112118
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
113119
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -125,7 +131,7 @@ predicate missedOfTypeOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclSt
125131
* local variable declaration statement `s`, and the initializer does not
126132
* contain an `await` expression (since `Select` does not support async lambdas).
127133
*/
128-
predicate missedSelectOpportunity(ForeachStmtGenericEnumerable fes, LocalVariableDeclStmt s) {
134+
predicate missedSelectOpportunity(ForEachStmtGenericEnumerable fes, LocalVariableDeclStmt s) {
129135
s = firstStmt(fes) and
130136
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
131137
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -140,7 +146,7 @@ predicate missedSelectOpportunity(ForeachStmtGenericEnumerable fes, LocalVariabl
140146
* variable, and the body of the `if` is either a `continue` or there's nothing
141147
* else in the loop than the `if`.
142148
*/
143-
predicate missedWhereOpportunity(ForeachStmtGenericEnumerable fes, IfStmt is) {
149+
predicate missedWhereOpportunity(ForEachStmtGenericEnumerable fes, IfStmt is) {
144150
// The very first thing the foreach loop does is test its iteration variable.
145151
is = firstStmt(fes) and
146152
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)