Skip to content

Commit db565c5

Browse files
committed
C#: Remove false positives in cs/constant-condition
1 parent 4da7a17 commit db565c5

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-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 |
106+
se.getACase().getPattern() = this.(DiscardExpr) and
107+
strictcount(se.getACase()) > 1
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.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
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. |
1010
| ConstantCondition.cs:97:13:97:13 | _ | Pattern always matches. |
11-
| ConstantCondition.cs:106:13:106:13 | _ | Pattern always matches. |
1211
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
1312
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
1413
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |

0 commit comments

Comments
 (0)