Skip to content

Commit 1a511c2

Browse files
committed
C#: Update the queries that provide LINQ recommendation.
1 parent e14e0cd commit 1a511c2

File tree

6 files changed

+49
-21
lines changed

6 files changed

+49
-21
lines changed

csharp/ql/lib/Linq/Helpers.qll

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* Provides helper classes and methods related to LINQ.
44
*/
55

6-
import csharp
6+
private import csharp
7+
private import semmle.code.csharp.frameworks.system.collections.Generic as GenericCollections
8+
private import semmle.code.csharp.frameworks.system.Collections as Collections
79

810
//#################### PREDICATES ####################
911
private Stmt firstStmt(ForeachStmt fes) {
@@ -29,13 +31,40 @@ predicate isIEnumerableType(ValueOrRefType t) {
2931
)
3032
}
3133

34+
/**
35+
* A class of foreach statements where the iterable expression
36+
* supports the use of the LINQ extension methods on `IEnumerable<T>`.
37+
*/
38+
class ForeachStmtGenericEnumerable extends ForeachStmt {
39+
ForeachStmtGenericEnumerable() {
40+
exists(ValueOrRefType t | t = this.getIterableExpr().getType() |
41+
t.getABaseType*().getUnboundDeclaration() instanceof
42+
GenericCollections::SystemCollectionsGenericIEnumerableTInterface or
43+
t.(ArrayType).getRank() = 1
44+
)
45+
}
46+
}
47+
48+
/**
49+
* A class of foreach statements where the iterable expression
50+
* supports the use of the LINQ extension methods on `IEnumerable`.
51+
*/
52+
class ForeachStmtEnumerable extends ForeachStmt {
53+
ForeachStmtEnumerable() {
54+
exists(ValueOrRefType t | t = this.getIterableExpr().getType() |
55+
t.getABaseType*() instanceof Collections::SystemCollectionsIEnumerableInterface or
56+
t.(ArrayType).getRank() = 1
57+
)
58+
}
59+
}
60+
3261
/**
3362
* Holds if `foreach` statement `fes` could be converted to a `.All()` call.
3463
* That is, the `ForeachStmt` contains a single `if` with a condition that
3564
* accesses the loop variable and with a body that assigns `false` to a variable
3665
* and `break`s out of the `foreach`.
3766
*/
38-
predicate missedAllOpportunity(ForeachStmt fes) {
67+
predicate missedAllOpportunity(ForeachStmtGenericEnumerable fes) {
3968
exists(IfStmt is |
4069
// The loop contains an if statement with no else case, and nothing else.
4170
is = firstStmt(fes) and
@@ -54,12 +83,12 @@ predicate missedAllOpportunity(ForeachStmt fes) {
5483
}
5584

5685
/**
57-
* Holds if `foreach` statement `fes` could be converted to a `.Cast()` call.
86+
* Holds if the `foreach` statement `fes` can be converted to a `.Cast()` call.
5887
* That is, the loop variable is accessed only in the first statement of the
59-
* block, and the access is a cast. The first statement needs to be a
60-
* `LocalVariableDeclStmt`.
88+
* block, the access is a cast, and the first statement is a
89+
* local variable declaration statement `s`.
6190
*/
62-
predicate missedCastOpportunity(ForeachStmt fes, LocalVariableDeclStmt s) {
91+
predicate missedCastOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclStmt s) {
6392
s = firstStmt(fes) and
6493
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
6594
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -71,12 +100,12 @@ predicate missedCastOpportunity(ForeachStmt fes, LocalVariableDeclStmt s) {
71100
}
72101

73102
/**
74-
* Holds if `foreach` statement `fes` could be converted to an `.OfType()` call.
103+
* Holds if `foreach` statement `fes` can be converted to an `.OfType()` call.
75104
* That is, the loop variable is accessed only in the first statement of the
76-
* block, and the access is a cast with the `as` operator. The first statement
77-
* needs to be a `LocalVariableDeclStmt`.
105+
* block, the access is a cast with the `as` operator, and the first statement
106+
* is a local variable declaration statement `s`.
78107
*/
79-
predicate missedOfTypeOpportunity(ForeachStmt fes, LocalVariableDeclStmt s) {
108+
predicate missedOfTypeOpportunity(ForeachStmtEnumerable fes, LocalVariableDeclStmt s) {
80109
s = firstStmt(fes) and
81110
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
82111
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -88,12 +117,12 @@ predicate missedOfTypeOpportunity(ForeachStmt fes, LocalVariableDeclStmt s) {
88117
}
89118

90119
/**
91-
* Holds if `foreach` statement `fes` could be converted to a `.Select()` call.
120+
* Holds if `foreach` statement `fes` can be converted to a `.Select()` call.
92121
* That is, the loop variable is accessed only in the first statement of the
93-
* block, and the access is not a cast. The first statement needs to be a
94-
* `LocalVariableDeclStmt`.
122+
* block, the access is not a cast, and the first statement is a
123+
* local variable declaration statement `s`.
95124
*/
96-
predicate missedSelectOpportunity(ForeachStmt fes, LocalVariableDeclStmt s) {
125+
predicate missedSelectOpportunity(ForeachStmtGenericEnumerable fes, LocalVariableDeclStmt s) {
97126
s = firstStmt(fes) and
98127
forex(VariableAccess va | va = fes.getVariable().getAnAccess() |
99128
va = s.getAVariableDeclExpr().getAChildExpr*()
@@ -107,7 +136,7 @@ predicate missedSelectOpportunity(ForeachStmt fes, LocalVariableDeclStmt s) {
107136
* variable, and the body of the `if` is either a `continue` or there's nothing
108137
* else in the loop than the `if`.
109138
*/
110-
predicate missedWhereOpportunity(ForeachStmt fes, IfStmt is) {
139+
predicate missedWhereOpportunity(ForeachStmtGenericEnumerable fes, IfStmt is) {
111140
// The very first thing the foreach loop does is test its iteration variable.
112141
is = firstStmt(fes) and
113142
exists(VariableAccess va |

csharp/ql/src/Linq/MissedAllOpportunity.ql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* language-features
1111
*/
1212

13-
import csharp
1413
import Linq.Helpers
1514

1615
/*
@@ -31,7 +30,7 @@ import Linq.Helpers
3130
* bool allEven = lst.All(i => i % 2 == 0);
3231
*/
3332

34-
from ForeachStmt fes
33+
from ForeachStmtGenericEnumerable fes
3534
where missedAllOpportunity(fes)
3635
select fes,
3736
"This foreach loop looks as if it might be testing whether every sequence element satisfies a predicate - consider using '.All(...)'."

csharp/ql/src/Linq/MissedCastOpportunity.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import csharp
1414
import Linq.Helpers
1515

16-
from ForeachStmt fes, LocalVariableDeclStmt s
16+
from ForeachStmtEnumerable fes, LocalVariableDeclStmt s
1717
where missedCastOpportunity(fes, s)
1818
select fes,
1919
"This foreach loop immediately $@ - consider casting the sequence explicitly using '.Cast(...)'.",

csharp/ql/src/Linq/MissedOfTypeOpportunity.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import csharp
1414
import Linq.Helpers
1515

16-
from ForeachStmt fes, LocalVariableDeclStmt s
16+
from ForeachStmtEnumerable fes, LocalVariableDeclStmt s
1717
where missedOfTypeOpportunity(fes, s)
1818
select fes,
1919
"This foreach loop immediately uses 'as' to $@ - consider using '.OfType(...)' instead.", s,

csharp/ql/src/Linq/MissedSelectOpportunity.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ predicate oversized(LocalVariableDeclStmt s) {
2020
)
2121
}
2222

23-
from ForeachStmt fes, LocalVariableDeclStmt s
23+
from ForeachStmtGenericEnumerable fes, LocalVariableDeclStmt s
2424
where
2525
missedSelectOpportunity(fes, s) and
2626
not oversized(s)

csharp/ql/src/Linq/MissedWhereOpportunity.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import csharp
1313
import Linq.Helpers
1414

15-
from ForeachStmt fes, IfStmt is
15+
from ForeachStmtGenericEnumerable fes, IfStmt is
1616
where
1717
missedWhereOpportunity(fes, is) and
1818
not missedAllOpportunity(fes)

0 commit comments

Comments
 (0)