-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissedAllOpportunity.ql
More file actions
38 lines (35 loc) · 975 Bytes
/
MissedAllOpportunity.ql
File metadata and controls
38 lines (35 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @name Missed opportunity to use All
* @description The intent of a foreach loop that checks whether every element of its target sequence satisfies some predicate can be expressed
* more directly using LINQ's 'All' method.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cs/linq/missed-all
* @tags quality
* maintainability
* readability
* language-features
*/
import Linq.Helpers
/*
* The purpose of this query is to find loops of the following form:
*
* bool allEven = true;
* foreach(int i in lst)
* {
* if(i % 2 != 0)
* {
* allEven = false;
* break;
* }
* }
*
* This could be written more cleanly as:
*
* bool allEven = lst.All(i => i % 2 == 0);
*/
from ForeachStmtGenericEnumerable fes
where missedAllOpportunity(fes)
select fes,
"This foreach loop looks as if it might be testing whether every sequence element satisfies a predicate - consider using '.All(...)'."