Skip to content

Commit e122f94

Browse files
committed
Move to isBooleanConstant
1 parent 74ee101 commit e122f94

File tree

3 files changed

+15
-28
lines changed

3 files changed

+15
-28
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ private predicate isBooleanConstant(Expr e, boolean value) {
199199
value = false
200200
or
201201
isConstantComparison(e, value)
202+
or
203+
exists(Method m, Call c, Expr expr |
204+
m = any(SystemStringClass s).getIsNullOrEmptyMethod() and
205+
c.getTarget() = m and
206+
e = c and
207+
expr = c.getArgument(0) and
208+
expr.hasValue() and
209+
if expr.getValue().length() > 0 and not expr instanceof NullLiteral
210+
then value = false
211+
else value = true
212+
)
202213
)
203214
}
204215

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,6 @@ class ConstantIfCondition extends ConstantBooleanCondition {
7373
}
7474
}
7575

76-
/** A constant return value from a function with constant input expression. */
77-
class ConstantReturnValueCondition extends ConstantCondition {
78-
boolean b;
79-
80-
ConstantReturnValueCondition() {
81-
exists(Method m, Call c, Expr expr |
82-
m = any(SystemStringClass s).getIsNullOrEmptyMethod() and
83-
c.getTarget() = m and
84-
this = c and
85-
expr = c.getArgument(0) and
86-
expr.hasValue() and
87-
if expr.getValue().length() > 0 and not expr instanceof NullLiteral
88-
then b = false
89-
else b = true
90-
)
91-
}
92-
93-
override string getMessage() {
94-
if b = true
95-
then result = "Expression is always 'true'."
96-
else result = "Expression is always 'false'."
97-
}
98-
}
99-
10076
/** A constant loop condition. */
10177
class ConstantLoopCondition extends ConstantBooleanCondition {
10278
ConstantLoopCondition() { this = any(LoopStmt ls).getCondition() }

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
| ConstantIfCondition.cs:11:17:11:29 | ... == ... | Condition always evaluates to 'true'. |
2020
| ConstantIfCondition.cs:14:17:14:21 | false | Condition always evaluates to 'false'. |
2121
| ConstantIfCondition.cs:17:17:17:26 | ... == ... | Condition always evaluates to 'true'. |
22-
| ConstantIsNullOrEmpty.cs:10:21:10:54 | call to method IsNullOrEmpty | Expression is always 'false'. |
23-
| ConstantIsNullOrEmpty.cs:46:21:46:46 | call to method IsNullOrEmpty | Expression is always 'true'. |
24-
| ConstantIsNullOrEmpty.cs:50:21:50:44 | call to method IsNullOrEmpty | Expression is always 'true'. |
25-
| ConstantIsNullOrEmpty.cs:54:21:54:45 | call to method IsNullOrEmpty | Expression is always 'false'. |
22+
| ConstantIsNullOrEmpty.cs:10:21:10:54 | call to method IsNullOrEmpty | Condition always evaluates to 'false'. |
23+
| ConstantIsNullOrEmpty.cs:46:21:46:46 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. |
24+
| ConstantIsNullOrEmpty.cs:50:21:50:44 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. |
25+
| ConstantIsNullOrEmpty.cs:54:21:54:45 | call to method IsNullOrEmpty | Condition always evaluates to 'false'. |
2626
| ConstantNullCoalescingLeftHandOperand.cs:11:24:11:34 | access to constant NULL_OBJECT | Expression is never 'null'. |
2727
| ConstantNullCoalescingLeftHandOperand.cs:12:24:12:27 | null | Expression is always 'null'. |
2828
| ConstantWhileCondition.cs:12:20:12:32 | ... == ... | Condition always evaluates to 'true'. |

0 commit comments

Comments
 (0)