File tree Expand file tree Collapse file tree
csharp/ql/test/query-tests/Linq/MissedWhereOpportunity Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -126,6 +126,42 @@ public IEnumerable<int> M9(IEnumerable<int> elements)
126126 } // $ Alert
127127 }
128128
129+ public int M10 ( IEnumerable < int > elements )
130+ {
131+ // GOOD: The filtered case ends with a return from the method instead of continuing the loop.
132+ foreach ( var element in elements )
133+ {
134+ if ( element . GetHashCode ( ) % 2 == 0 )
135+ {
136+ Console . WriteLine ( element ) ;
137+ return element ;
138+ }
139+ }
140+
141+ return 0 ;
142+ }
143+
144+ public int M11 ( IEnumerable < int > elements )
145+ {
146+ // GOOD: Both nested filtered cases return from the method instead of continuing the loop.
147+ foreach ( var element in elements )
148+ {
149+ if ( element . GetHashCode ( ) % 2 == 0 )
150+ {
151+ if ( element > 10 )
152+ {
153+ return element ;
154+ }
155+ else
156+ {
157+ return 10 ;
158+ }
159+ }
160+ }
161+
162+ return 0 ;
163+ }
164+
129165 public class NonEnumerableClass
130166 {
131167 public IEnumerator < int > GetEnumerator ( ) => throw null ;
You can’t perform that action at this time.
0 commit comments