зеркало из https://github.com/github/codeql.git
Merge pull request #1521 from hvitved/csharp/constant-condition-fp
Approved by calumgrant
This commit is contained in:
Коммит
4f3cbe0029
|
@ -4,6 +4,7 @@
|
|||
|
||||
| **Query** | **Expected impact** | **Change** |
|
||||
|------------------------------|------------------------|-----------------------------------|
|
||||
| Constant condition (`cs/constant-condition`) | Fewer false positive results | Results have been removed for default cases (`_`) in switch expressions. |
|
||||
| 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. |
|
||||
|
||||
## Changes to code extraction
|
||||
|
|
|
@ -101,6 +101,13 @@ class ConstantMatchingCondition extends ConstantCondition {
|
|||
)
|
||||
}
|
||||
|
||||
override predicate isWhiteListed() {
|
||||
exists(SwitchExpr se, int i |
|
||||
se.getCase(i).getPattern() = this.(DiscardExpr) and
|
||||
i > 0
|
||||
)
|
||||
}
|
||||
|
||||
override string getMessage() {
|
||||
if b = true then result = "Pattern always matches." else result = "Pattern never matches."
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// semmle-extractor-options: /r:System.Threading.Thread.dll /r:System.Diagnostics.Debug.dll
|
||||
// semmle-extractor-options: /r:System.Threading.Thread.dll /r:System.Diagnostics.Debug.dll /langversion:preview
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
@ -89,6 +89,23 @@ class ConstantMatching
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
string M4(object o)
|
||||
{
|
||||
return o switch
|
||||
{
|
||||
_ => o.ToString() // BAD
|
||||
};
|
||||
}
|
||||
|
||||
string M5(object o)
|
||||
{
|
||||
return o switch
|
||||
{
|
||||
"" => " ",
|
||||
_ => o.ToString() // GOOD
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class Assertions
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
| ConstantCondition.cs:64:18:64:18 | 2 | Pattern never matches. |
|
||||
| ConstantCondition.cs:66:18:66:18 | 3 | Pattern always matches. |
|
||||
| ConstantCondition.cs:77:18:77:20 | access to type Int32 | Pattern never matches. |
|
||||
| ConstantCondition.cs:97:13:97:13 | _ | Pattern always matches. |
|
||||
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
|
||||
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
|
||||
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |
|
||||
|
|
Загрузка…
Ссылка в новой задаче