Skip to content

Commit 4f3cbe0

Browse files
authored
Merge pull request #1521 from hvitved/csharp/constant-condition-fp
Approved by calumgrant
2 parents ae3a48d + e6e6062 commit 4f3cbe0

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

change-notes/1.22/analysis-csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
| **Query** | **Expected impact** | **Change** |
66
|------------------------------|------------------------|-----------------------------------|
7+
| Constant condition (`cs/constant-condition`) | Fewer false positive results | Results have been removed for default cases (`_`) in switch expressions. |
78
| Dispose may not be called if an exception is thrown during execution (`cs/dispose-not-called-on-throw`) | Fewer false positive results | Results have been removed where an object is disposed both by a `using` statement and a `Dispose` call. |
89

910
## Changes to code extraction

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ class ConstantMatchingCondition extends ConstantCondition {
101101
)
102102
}
103103

104+
override predicate isWhiteListed() {
105+
exists(SwitchExpr se, int i |
106+
se.getCase(i).getPattern() = this.(DiscardExpr) and
107+
i > 0
108+
)
109+
}
110+
104111
override string getMessage() {
105112
if b = true then result = "Pattern always matches." else result = "Pattern never matches."
106113
}

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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. |
1011
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
1112
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
1213
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |

0 commit comments

Comments
 (0)