Skip to content

Commit f6aca58

Browse files
authored
Merge pull request github#13885 from michaelnebel/csharp/linqforeach
C#: LINQ recommendation queries.
2 parents f9fc79b + f67d5e1 commit f6aca58

14 files changed

+224
-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)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
class MissedCastOpportunity
6+
{
7+
public void M1(List<Animal> animals)
8+
{
9+
// BAD: Can be replaced with animals.Cast<Dog>().
10+
foreach (Animal a in animals)
11+
{
12+
Dog d = (Dog)a;
13+
d.Woof();
14+
}
15+
}
16+
17+
public void M2(NonEnumerableClass nec)
18+
{
19+
// GOOD: Not possible to use Linq here.
20+
foreach (Animal a in nec)
21+
{
22+
Dog d = (Dog)a;
23+
d.Woof();
24+
}
25+
}
26+
27+
public void M3(Animal[] animals)
28+
{
29+
// BAD: Can be replaced with animals.Cast<Dog>().
30+
foreach (Animal animal in animals)
31+
{
32+
Dog d = (Dog)animal;
33+
d.Woof();
34+
}
35+
}
36+
37+
public void M4(Array animals)
38+
{
39+
// BAD: Can be replaced with animals.Cast<Dog>().
40+
foreach (Animal animal in animals)
41+
{
42+
Dog d = (Dog)animal;
43+
d.Woof();
44+
}
45+
}
46+
47+
public void M5(IEnumerable animals)
48+
{
49+
// BAD: Can be replaced with animals.Cast<Dog>().
50+
foreach (object animal in animals)
51+
{
52+
Dog d = (Dog)animal;
53+
d.Woof();
54+
}
55+
}
56+
57+
public class NonEnumerableClass
58+
{
59+
public IEnumerator<Animal> GetEnumerator() => throw null;
60+
}
61+
62+
public class Animal { }
63+
64+
class Dog : Animal
65+
{
66+
private string name;
67+
68+
public Dog(string name)
69+
{
70+
this.name = name;
71+
}
72+
73+
public void Woof()
74+
{
75+
Console.WriteLine("Woof! My name is " + name + ".");
76+
}
77+
}
78+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| MissedCastOpportunity.cs:10:9:14:9 | foreach (... ... in ...) ... | This foreach loop immediately $@ - consider casting the sequence explicitly using '.Cast(...)'. | MissedCastOpportunity.cs:12:13:12:27 | ... ...; | casts its iteration variable to another type |
2+
| MissedCastOpportunity.cs:30:9:34:9 | foreach (... ... in ...) ... | This foreach loop immediately $@ - consider casting the sequence explicitly using '.Cast(...)'. | MissedCastOpportunity.cs:32:13:32:32 | ... ...; | casts its iteration variable to another type |
3+
| MissedCastOpportunity.cs:40:9:44:9 | foreach (... ... in ...) ... | This foreach loop immediately $@ - consider casting the sequence explicitly using '.Cast(...)'. | MissedCastOpportunity.cs:42:13:42:32 | ... ...; | casts its iteration variable to another type |
4+
| MissedCastOpportunity.cs:50:9:54:9 | foreach (... ... in ...) ... | This foreach loop immediately $@ - consider casting the sequence explicitly using '.Cast(...)'. | MissedCastOpportunity.cs:52:13:52:32 | ... ...; | casts its iteration variable to another type |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Linq/MissedCastOpportunity.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj

0 commit comments

Comments
 (0)