Skip to content

Commit b11dce7

Browse files
committed
C#: Fix FP in expressions of the form (a?b:null)??d, where the null has a single successor, but the d is a join node.
1 parent 3e563f7 commit b11dce7

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ class ConstantNullnessCondition extends ConstantCondition {
7171

7272
ConstantNullnessCondition() {
7373
forex(ControlFlow::Node cfn | cfn = this.getAControlFlowNode() |
74-
exists(ControlFlow::SuccessorTypes::NullnessSuccessor t | exists(cfn.getASuccessorByType(t)) |
75-
b = t.getValue()
74+
exists(ControlFlow::SuccessorTypes::NullnessSuccessor t, ControlFlow::Node s |
75+
s = cfn.getASuccessorByType(t) |
76+
b = t.getValue() and
77+
not s.isJoin()
7678
) and
7779
strictcount(ControlFlow::SuccessorType t | exists(cfn.getASuccessorByType(t))) = 1
7880
)

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void M2(int i)
5050
j = (int?)i ?? 1; // BAD
5151
s = ""?.CommaJoinWith(s); // BAD
5252
s = s ?? ""; // GOOD
53-
s = (i==0 ? s : null) ?? s; // BAD (False positive)
53+
s = (i==0 ? s : null) ?? s;
5454
}
5555
}
5656

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
@@ -4,7 +4,6 @@
44
| ConstantCondition.cs:49:17:49:18 | "" | Expression is never 'null'. |
55
| ConstantCondition.cs:50:13:50:19 | (...) ... | Expression is never 'null'. |
66
| ConstantCondition.cs:51:13:51:14 | "" | Expression is never 'null'. |
7-
| ConstantCondition.cs:53:25:53:28 | null | Expression is always 'null'. |
87
| ConstantCondition.cs:63:18:63:18 | 2 | Pattern never matches. |
98
| ConstantCondition.cs:65:18:65:18 | 3 | Pattern always matches. |
109
| ConstantCondition.cs:76:18:76:20 | access to type Int32 | Pattern never matches. |

0 commit comments

Comments
 (0)