Skip to content

Commit ca553bf

Browse files
authored
Merge pull request #18932 from michaelnebel/csharp/ismatchingconstantunknowtype
C#: Special handling of unknown types in `isMatchingConstant`.
2 parents d84368e + 21aa62c commit ca553bf

File tree

14 files changed

+38
-0
lines changed

14 files changed

+38
-0
lines changed

csharp/ql/lib/semmle/code/csharp/controlflow/internal/Completion.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ private predicate isMatchingConstant(PatternExpr pe, boolean value) {
293293
value = true
294294
or
295295
exists(Type t, Type strippedType |
296+
not t instanceof UnknownType and
297+
not strippedType instanceof UnknownType and
296298
typePatternMustHaveMatchingCompletion(pe, t, strippedType) and
297299
not typePatternCommonSubType(t, strippedType) and
298300
value = false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Increase query precision for `cs/useless-assignment-to-local` and `cs/constant-condition` when *unknown* types are involved (mostly relevant for `build-mode: none` databases).
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
partial class C1
4+
{
5+
public C2 Prop { get; set; }
6+
}
7+
8+
class C2 { }
9+
10+
class ConstantMatching
11+
{
12+
void M1()
13+
{
14+
var c1 = new C1();
15+
if (c1.Prop is int) // $ Alert
16+
{
17+
}
18+
19+
// Should not be considered a constant condition as
20+
// we don't know anything about D.
21+
var d = new D();
22+
if (d.Prop is C2)
23+
{
24+
}
25+
}
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| ConstantCondition.cs:15:13:15:26 | ... is ... | Condition always evaluates to 'false'. |
2+
| ConstantCondition.cs:15:24:15:26 | access to type Int32 | Pattern never matches. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Bad Practices/Control-Flow/ConstantCondition.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --standalone

0 commit comments

Comments
 (0)