Skip to content

Commit 4451e55

Browse files
committed
C#: Convert cs/constant-condition tests to inline expectation tests.
1 parent a4f2264 commit 4451e55

File tree

9 files changed

+53
-53
lines changed

9 files changed

+53
-53
lines changed

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ class ConstantNullness
3535
{
3636
void M1(int i)
3737
{
38-
var j = ((string)null)?.Length; // BAD
39-
var s = ((int?)i)?.ToString(); // BAD
38+
var j = ((string)null)?.Length; // $ Alert
39+
var s = ((int?)i)?.ToString(); // $ Alert
4040
var k = s?.Length; // GOOD
4141
k = s?.ToLower()?.Length; // GOOD
4242
}
4343

4444
void M2(int i)
4545
{
46-
var j = (int?)null ?? 0; // BAD
47-
var s = "" ?? "a"; // BAD
48-
j = (int?)i ?? 1; // BAD
49-
s = ""?.CommaJoinWith(s); // BAD
46+
var j = (int?)null ?? 0; // $ Alert
47+
var s = "" ?? "a"; // $ Alert
48+
j = (int?)i ?? 1; // $ Alert
49+
s = ""?.CommaJoinWith(s); // $ Alert
5050
s = s ?? ""; // GOOD
5151
s = (i == 0 ? s : null) ?? s; // GOOD
5252
var k = (i == 0 ? s : null)?.Length; // GOOD
@@ -59,9 +59,9 @@ void M1()
5959
{
6060
switch (1 + 2)
6161
{
62-
case 2: // BAD
62+
case 2: // $ Alert
6363
break;
64-
case 3: // BAD
64+
case 3: // $ Alert
6565
break;
6666
case int _: // GOOD
6767
break;
@@ -72,7 +72,7 @@ void M2(string s)
7272
{
7373
switch ((object)s)
7474
{
75-
case int _: // BAD
75+
case int _: // $ Alert
7676
break;
7777
case "": // GOOD
7878
break;
@@ -92,7 +92,7 @@ string M4(object o)
9292
{
9393
return o switch
9494
{
95-
_ => o.ToString() // BAD
95+
_ => o.ToString() // $ Alert
9696
};
9797
}
9898

@@ -111,7 +111,7 @@ void M6(bool b1, bool b2)
111111
return;
112112
if (!b2)
113113
return;
114-
if (b1 && b2) // BAD
114+
if (b1 && b2) // $ Alert
115115
return;
116116
}
117117

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Bad Practices/Control-Flow/ConstantCondition.ql
1+
query: Bad Practices/Control-Flow/ConstantCondition.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ class Bad
22
{
33
public int Max(int a, int b)
44
{
5-
return a > a ? a : b;
5+
return a > a ? a : b; // $ Alert
66
}
77
}

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionalExpressionCondition.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class Main
88

99
public void Foo()
1010
{
11-
int i = (ZERO == 1 - 1) ? 0 : 1; // BAD
12-
int j = false ? 0 : 1; // BAD
13-
int k = " " == " " ? 0 : 1; // BAD
14-
int l = (" "[0] == ' ') ? 0 : 1; // BAD: but not flagged
11+
int i = (ZERO == 1 - 1) ? 0 : 1; // $ Alert
12+
int j = false ? 0 : 1; // $ Alert
13+
int k = " " == " " ? 0 : 1; // $ Alert
14+
int l = (" "[0] == ' ') ? 0 : 1; // Missing Alert
1515
int m = Bar() == 0 ? 0 : 1; // GOOD
1616
}
1717

@@ -21,5 +21,4 @@ public int Bar()
2121
}
2222

2323
}
24-
2524
}

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantForCondition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class Main
66
{
77
public void M()
88
{
9-
for (int i = 0; false; i++) // GOOD
9+
for (int i = 0; false; i++) // $ Alert
1010
;
11-
for (int i = 0; 0 == 1; i++) // BAD
11+
for (int i = 0; 0 == 1; i++) // $ Alert
1212
;
1313
for (; ; ) // GOOD
1414
;

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ class Main
88

99
public void Foo()
1010
{
11-
if (ZERO == 1 - 1)
12-
{ // BAD
11+
if (ZERO == 1 - 1) // $ Alert
12+
{
1313
}
14-
if (false)
15-
{ // BAD
14+
if (false) // $ Alert
15+
{
1616
}
17-
if (" " == " ")
18-
{ // BAD
17+
if (" " == " ") // $ Alert
18+
{
1919
}
20-
if (" "[0] == ' ')
21-
{ // BAD: but not flagged
20+
if (" "[0] == ' ') // Missing Alert
21+
{
2222
}
23-
if (Bar() == 0)
24-
{ // GOOD
23+
if (Bar() == 0) // GOOD
24+
{
2525
}
2626
}
2727

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIsNullOrEmpty.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ internal class Program
77
static void Main(string[] args)
88
{
99
{
10-
if (string.IsNullOrEmpty(nameof(args))) // bad: always false
10+
if (string.IsNullOrEmpty(nameof(args))) // $ Alert
1111
{
1212
}
1313

1414
string? x = null;
15-
if (string.IsNullOrEmpty(x)) // would be nice... bad: always true
15+
if (string.IsNullOrEmpty(x)) // Missing Alert (always true)
1616
{
1717
}
1818

1919
string y = "";
20-
if (string.IsNullOrEmpty(y)) // would be nice... bad: always true
20+
if (string.IsNullOrEmpty(y)) // Missing Alert (always true)
2121
{
2222
}
2323

@@ -28,12 +28,12 @@ static void Main(string[] args)
2828
}
2929

3030
string z = " ";
31-
if (string.IsNullOrEmpty(z)) // would be nice... bad: always false
31+
if (string.IsNullOrEmpty(z)) // Missing Alert (always false)
3232
{
3333
}
3434

3535
string a = "a";
36-
if (string.IsNullOrEmpty(a)) // would be nice... bad: always false
36+
if (string.IsNullOrEmpty(a)) // Missing Alert (always false)
3737
{
3838
}
3939

@@ -43,18 +43,18 @@ static void Main(string[] args)
4343
{
4444
}
4545

46-
if (string.IsNullOrEmpty(null)) // bad: always true
46+
if (string.IsNullOrEmpty(null)) // $ Alert
4747
{
4848
}
4949

50-
if (string.IsNullOrEmpty("")) // bad: always true
50+
if (string.IsNullOrEmpty("")) // $ Alert
5151
{
5252
}
5353

54-
if (string.IsNullOrEmpty(" ")) // bad: always false
54+
if (string.IsNullOrEmpty(" ")) // $ Alert
5555
{
5656
}
5757
}
5858
}
5959
}
60-
}
60+
}

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantNullCoalescingLeftHandOperand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class Main
88

99
public void Foo()
1010
{
11-
object i = NULL_OBJECT ?? ""; // BAD
12-
object j = null ?? ""; // BAD
11+
object i = NULL_OBJECT ?? ""; // $ Alert
12+
object j = null ?? ""; // $ Alert
1313
object k = Bar() ?? ""; // GOOD
1414
}
1515

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantWhileCondition.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ class Main
99

1010
public void Foo()
1111
{
12-
while (ZERO == 1 - 1)
13-
{ // BAD
12+
while (ZERO == 1 - 1) // $ Alert
13+
{
1414
break;
1515
}
16-
while (false)
17-
{ // GOOD
16+
while (false) // $ Alert
17+
{
1818
break;
1919
}
20-
while (true)
21-
{ // GOOD
20+
while (true) // GOOD
21+
{
2222
break;
2323
}
24-
while (" " == " ")
25-
{ // BAD
24+
while (" " == " ") // $ Alert
25+
{
2626
break;
2727
}
28-
while (" "[0] == ' ')
29-
{ // BAD: but not flagged
28+
while (" "[0] == ' ') // Missing Alert
29+
{
3030
break;
3131
}
32-
while (Bar() == 0)
33-
{ // GOOD
32+
while (Bar() == 0) // GOOD
33+
{
3434
break;
3535
}
3636
}

0 commit comments

Comments
 (0)