Skip to content

Commit e2ebb0d

Browse files
committed
C#: Add test cases that uses in/out or ref parameters.
1 parent 612e515 commit e2ebb0d

8 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎csharp/ql/test/query-tests/Linq/MissedAllOpportunity/MissedAllOpportunity.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public void M2(NonEnumerableClass nec)
3232
}
3333
}
3434

35+
public void M3(List<int> lst, ref int x)
36+
{
37+
// GOOD: Linq can't be used here because the condition uses a ref parameter.
38+
var allEven = true;
39+
foreach (int i in lst)
40+
{
41+
if (i % 2 != x)
42+
{
43+
allEven = false;
44+
break;
45+
}
46+
}
47+
}
48+
3549
public class NonEnumerableClass
3650
{
3751
public IEnumerator<int> GetEnumerator() => throw null;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
#select
12
| 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(...)'. |
3+
| MissedAllOpportunity.cs:39:9:46:9 | foreach (... ... in ...) ... | This foreach loop looks as if it might be testing whether every sequence element satisfies a predicate - consider using '.All(...)'. |
4+
testFailures
5+
| MissedAllOpportunity.cs:39:9:46:9 | This foreach loop looks as if it might be testing whether every sequence element satisfies a predicate - consider using '.All(...)'. | Unexpected result: Alert |

‎csharp/ql/test/query-tests/Linq/MissedFirstOrDefaultOpportunity/MissedFirstOrDefaultOpportunity.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ public Operation M14(IEnumerable<Operation> operations, Func<string, bool>[] pre
179179
return null;
180180
}
181181

182+
public int M15(IEnumerable<int> values, ref readonly int x)
183+
{
184+
// GOOD: FirstOrDefault does not support a predicate that captures a ref parameter.
185+
foreach (var value in values)
186+
{
187+
if (value > x)
188+
{
189+
return value;
190+
}
191+
}
192+
193+
return default;
194+
}
195+
182196
private static Task<bool> IsMatch(Operation operation, string operationId) =>
183197
Task.FromResult(string.Equals(operation.OperationId, operationId, StringComparison.Ordinal));
184198
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#select
12
| MissedFirstOrDefaultOpportunity.cs:10:9:14:9 | foreach (... ... in ...) ... | This foreach loop returns the first sequence element satisfying a $@ - consider finding the element explicitly using '.FirstOrDefault(...)'. | MissedFirstOrDefaultOpportunity.cs:12:17:12:91 | call to method Equals | predicate |
23
| MissedFirstOrDefaultOpportunity.cs:22:9:28:9 | foreach (... ... in ...) ... | This foreach loop returns the first sequence element satisfying a $@ - consider finding the element explicitly using '.FirstOrDefault(...)'. | MissedFirstOrDefaultOpportunity.cs:24:17:24:25 | ... > ... | predicate |
34
| MissedFirstOrDefaultOpportunity.cs:36:9:40:9 | foreach (... ... in ...) ... | This foreach loop returns the first sequence element satisfying a $@ - consider finding the element explicitly using '.FirstOrDefault(...)'. | MissedFirstOrDefaultOpportunity.cs:38:17:38:25 | ... > ... | predicate |
45
| MissedFirstOrDefaultOpportunity.cs:149:9:153:9 | foreach (... ... in ...) ... | This foreach loop returns the first sequence element satisfying a $@ - consider finding the element explicitly using '.FirstOrDefault(...)'. | MissedFirstOrDefaultOpportunity.cs:151:17:151:32 | ... > ... | predicate |
56
| MissedFirstOrDefaultOpportunity.cs:161:9:165:9 | foreach (... ... in ...) ... | This foreach loop returns the first sequence element satisfying a $@ - consider finding the element explicitly using '.FirstOrDefault(...)'. | MissedFirstOrDefaultOpportunity.cs:163:17:163:25 | ... > ... | predicate |
7+
| MissedFirstOrDefaultOpportunity.cs:185:9:191:9 | foreach (... ... in ...) ... | This foreach loop returns the first sequence element satisfying a $@ - consider finding the element explicitly using '.FirstOrDefault(...)'. | MissedFirstOrDefaultOpportunity.cs:187:17:187:25 | ... > ... | predicate |
8+
testFailures
9+
| MissedFirstOrDefaultOpportunity.cs:185:9:191:9 | This foreach loop returns the first sequence element satisfying a $@ - consider finding the element explicitly using '.FirstOrDefault(...)'. | Unexpected result: Alert |

‎csharp/ql/test/query-tests/Linq/MissedSelectOpportunity/MissedSelectOpportunity.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ public async Task M2(IEnumerable<ICounter> counters)
2525
}
2626
}
2727

28+
public void M3(List<int> lst, out int x)
29+
{
30+
// GOOD: Linq can't be used here as the Select would capture an out parameter.
31+
x = 2;
32+
foreach (int i in lst)
33+
{
34+
int j = i * x;
35+
Console.WriteLine(j);
36+
}
37+
}
38+
2839
public interface ICounter
2940
{
3041
Task<int> CountAsync();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
#select
12
| MissedSelectOpportunity.cs:11:9:15:9 | foreach (... ... in ...) ... | This foreach loop immediately $@ - consider mapping the sequence explicitly using '.Select(...)'. | MissedSelectOpportunity.cs:13:13:13:26 | ... ...; | maps its iteration variable to another variable |
3+
| MissedSelectOpportunity.cs:32:9:36:9 | foreach (... ... in ...) ... | This foreach loop immediately $@ - consider mapping the sequence explicitly using '.Select(...)'. | MissedSelectOpportunity.cs:34:13:34:26 | ... ...; | maps its iteration variable to another variable |
4+
testFailures
5+
| MissedSelectOpportunity.cs:32:9:36:9 | This foreach loop immediately $@ - consider mapping the sequence explicitly using '.Select(...)'. | Unexpected result: Alert |

‎csharp/ql/test/query-tests/Linq/MissedWhereOpportunity/MissedWhereOpportunity.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ public void M12(IEnumerable<int> elements)
174174
}
175175
}
176176

177+
public void M13(List<int> lst, in int x)
178+
{
179+
// GOOD: Linq can't be used here because the condition uses an in parameter.
180+
foreach (int i in lst)
181+
{
182+
if (i % 2 != x)
183+
continue;
184+
Console.WriteLine(i);
185+
Console.WriteLine((i / 2));
186+
}
187+
}
188+
177189
public class NonEnumerableClass
178190
{
179191
public IEnumerator<int> GetEnumerator() => throw null;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#select
12
| MissedWhereOpportunity.cs:10:9:16:9 | foreach (... ... in ...) ... | This foreach loop $@ - consider filtering the sequence explicitly using '.Where(...)'. | MissedWhereOpportunity.cs:12:17:12:26 | ... != ... | implicitly filters its target sequence |
23
| MissedWhereOpportunity.cs:19:9:26:9 | foreach (... ... in ...) ... | This foreach loop $@ - consider filtering the sequence explicitly using '.Where(...)'. | MissedWhereOpportunity.cs:21:17:21:26 | ... == ... | implicitly filters its target sequence |
34
| MissedWhereOpportunity.cs:45:9:52:9 | foreach (... ... in ...) ... | This foreach loop $@ - consider filtering the sequence explicitly using '.Where(...)'. | MissedWhereOpportunity.cs:47:17:47:26 | ... == ... | implicitly filters its target sequence |
45
| MissedWhereOpportunity.cs:70:9:76:9 | foreach (... ... in ...) ... | This foreach loop $@ - consider filtering the sequence explicitly using '.Where(...)'. | MissedWhereOpportunity.cs:72:17:72:46 | ... == ... | implicitly filters its target sequence |
56
| MissedWhereOpportunity.cs:120:9:126:9 | foreach (... ... in ...) ... | This foreach loop $@ - consider filtering the sequence explicitly using '.Where(...)'. | MissedWhereOpportunity.cs:122:17:122:46 | ... == ... | implicitly filters its target sequence |
7+
| MissedWhereOpportunity.cs:180:9:186:9 | foreach (... ... in ...) ... | This foreach loop $@ - consider filtering the sequence explicitly using '.Where(...)'. | MissedWhereOpportunity.cs:182:17:182:26 | ... != ... | implicitly filters its target sequence |
8+
testFailures
9+
| MissedWhereOpportunity.cs:180:9:186:9 | This foreach loop $@ - consider filtering the sequence explicitly using '.Where(...)'. | Unexpected result: Alert |

0 commit comments

Comments
 (0)