Skip to content

Commit 4da7a17

Browse files
committed
C#: Add more tests for cs/constant-condition
1 parent fad37bd commit 4da7a17

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// semmle-extractor-options: /r:System.Threading.Thread.dll /r:System.Diagnostics.Debug.dll
1+
// semmle-extractor-options: /r:System.Threading.Thread.dll /r:System.Diagnostics.Debug.dll /langversion:preview
22

33
using System;
44
using System.Collections;
@@ -89,6 +89,23 @@ void M3(object o)
8989
break;
9090
}
9191
}
92+
93+
string M4(object o)
94+
{
95+
return o switch
96+
{
97+
_ => o.ToString() // BAD
98+
};
99+
}
100+
101+
string M5(object o)
102+
{
103+
return o switch
104+
{
105+
"" => " ",
106+
_ => o.ToString() // GOOD
107+
};
108+
}
92109
}
93110

94111
class Assertions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
| ConstantCondition.cs:64:18:64:18 | 2 | Pattern never matches. |
88
| ConstantCondition.cs:66:18:66:18 | 3 | Pattern always matches. |
99
| ConstantCondition.cs:77:18:77:20 | access to type Int32 | Pattern never matches. |
10+
| ConstantCondition.cs:97:13:97:13 | _ | Pattern always matches. |
11+
| ConstantCondition.cs:106:13:106:13 | _ | Pattern always matches. |
1012
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
1113
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
1214
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |

0 commit comments

Comments
 (0)