Merge pull request #1521 from hvitved/csharp/constant-condition-fp

Approved by calumgrant
This commit is contained in:
semmle-qlci 2019-07-01 10:52:14 +01:00 коммит произвёл GitHub
Родитель ae3a48db58 e6e606232d
Коммит 4f3cbe0029
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 27 добавлений и 1 удалений

Просмотреть файл

@ -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'. |