File tree Expand file tree Collapse file tree 4 files changed +27
-1
lines changed
src/Bad Practices/Control-Flow
test/query-tests/Bad Practices/Control-Flow/ConstantCondition Expand file tree Collapse file tree 4 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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
33using System ;
44using 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
94111class Assertions
Original file line number Diff line number Diff line change 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'. |
You can’t perform that action at this time.
0 commit comments