Skip to content

Commit 70e41b1

Browse files
authored
Merge pull request #6800 from hvitved/csharp/constant-cond-tuple-discard
C#: Filter discards in tuples in `ConstantCondition.ql`
2 parents 9762ce7 + f06632a commit 70e41b1

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Discards in tuple patterns, for example `(_, string s)`, are no longer flagged by the query "Constant condition".

csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class ConstantMatchingCondition extends ConstantCondition {
123123
se.getCase(i).getPattern() = this.(DiscardExpr) and
124124
i > 0
125125
)
126+
or
127+
this = any(PositionalPatternExpr ppe).getPattern(_)
126128
}
127129

128130
override string getMessage() {

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ void M2(int i)
4848
j = (int?)i ?? 1; // BAD
4949
s = ""?.CommaJoinWith(s); // BAD
5050
s = s ?? ""; // GOOD
51-
s = (i==0 ? s : null) ?? s; // GOOD
52-
var k = (i==0 ? s : null)?.Length; // GOOD
51+
s = (i == 0 ? s : null) ?? s; // GOOD
52+
var k = (i == 0 ? s : null)?.Length; // GOOD
5353
}
5454
}
5555

@@ -59,32 +59,32 @@ void M1()
5959
{
6060
switch (1 + 2)
6161
{
62-
case 2 : // BAD
63-
break;
64-
case 3 : // BAD
65-
break;
66-
case int _ : // GOOD
67-
break;
62+
case 2: // BAD
63+
break;
64+
case 3: // BAD
65+
break;
66+
case int _: // GOOD
67+
break;
6868
}
6969
}
7070

7171
void M2(string s)
7272
{
7373
switch ((object)s)
7474
{
75-
case int _ : // BAD
76-
break;
77-
case "" : // GOOD
78-
break;
75+
case int _: // BAD
76+
break;
77+
case "": // GOOD
78+
break;
7979
}
8080
}
8181

8282
void M3(object o)
8383
{
8484
switch (o)
8585
{
86-
case IList _ : // GOOD
87-
break;
86+
case IList _: // GOOD
87+
break;
8888
}
8989
}
9090

@@ -105,14 +105,25 @@ string M5(object o)
105105
};
106106
}
107107

108-
void M6(bool b1, bool b2) {
108+
void M6(bool b1, bool b2)
109+
{
109110
if (!b1)
110111
return;
111112
if (!b2)
112113
return;
113114
if (b1 && b2) // BAD
114115
return;
115116
}
117+
118+
string M7(object o)
119+
{
120+
return o switch
121+
{
122+
(string s, _) => s, // GOOD
123+
(_, string s) => s, // GOOD
124+
_ => "" // GOOD
125+
};
126+
}
116127
}
117128

118129
class Assertions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
| ConstantCondition.cs:64:18:64:18 | 3 | Pattern always matches. |
99
| ConstantCondition.cs:75:18:75:20 | access to type Int32 | Pattern never matches. |
1010
| ConstantCondition.cs:95:13:95:13 | _ | Pattern always matches. |
11-
| ConstantCondition.cs:113:13:113:14 | access to parameter b1 | Condition always evaluates to 'true'. |
12-
| ConstantCondition.cs:113:19:113:20 | access to parameter b2 | Condition always evaluates to 'true'. |
11+
| ConstantCondition.cs:114:13:114:14 | access to parameter b1 | Condition always evaluates to 'true'. |
12+
| ConstantCondition.cs:114:19:114:20 | access to parameter b2 | Condition always evaluates to 'true'. |
1313
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
1414
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
1515
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |

0 commit comments

Comments
 (0)