Skip to content

Commit 612e515

Browse files
committed
C#: Add some tests for cs/linq/missed-all.
1 parent 3736516 commit 612e515

4 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Linq;
3+
using System.Collections.Generic;
4+
5+
class MissedAllOpportunity
6+
{
7+
public void M1(List<int> lst)
8+
{
9+
// BAD: Can be replaced with lst.All(e => e % 2 == 0)
10+
var allEven = true;
11+
foreach (int i in lst)
12+
{
13+
if (i % 2 != 0)
14+
{
15+
allEven = false;
16+
break;
17+
}
18+
} // $ Alert
19+
}
20+
21+
public void M2(NonEnumerableClass nec)
22+
{
23+
// GOOD: Linq can't be used here.
24+
var allEven = true;
25+
foreach (int i in nec)
26+
{
27+
if (i % 2 != 0)
28+
{
29+
allEven = false;
30+
break;
31+
}
32+
}
33+
}
34+
35+
public class NonEnumerableClass
36+
{
37+
public IEnumerator<int> GetEnumerator() => throw null;
38+
}
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| MissedAllOpportunity.cs:11:9:18:9 | foreach (... ... in ...) ... | This foreach loop looks as if it might be testing whether every sequence element satisfies a predicate - consider using '.All(...)'. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Linq/MissedAllOpportunity.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.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)