Skip to content

Commit 5bcb9b2

Browse files
authored
Merge pull request github#11142 from JarLob/const
C#: Extend `Constant Condition` query with `String.IsNullOrEmpty`.
2 parents d731308 + d865f2e commit 5bcb9b2

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
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
@@ -198,6 +198,17 @@ private predicate isBooleanConstant(Expr e, boolean value) {
198198
value = false
199199
or
200200
isConstantComparison(e, value)
201+
or
202+
exists(Method m, Call c, Expr expr |
203+
m = any(SystemStringClass s).getIsNullOrEmptyMethod() and
204+
c.getTarget() = m and
205+
e = c and
206+
expr = c.getArgument(0) and
207+
expr.hasValue() and
208+
if expr.getValue().length() > 0 and not expr instanceof NullLiteral
209+
then value = false
210+
else value = true
211+
)
201212
)
202213
}
203214

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `Constant Condition` query was extended to catch cases when `String.IsNullOrEmpty` returns a constant value.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +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 | 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'. |
2226
| ConstantNullCoalescingLeftHandOperand.cs:11:24:11:34 | access to constant NULL_OBJECT | Expression is never 'null'. |
2327
| ConstantNullCoalescingLeftHandOperand.cs:12:24:12:27 | null | Expression is always 'null'. |
2428
| ConstantWhileCondition.cs:12:20:12:32 | ... == ... | Condition always evaluates to 'true'. |
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Threading.Tasks;
2+
3+
namespace ConstantIsNullOrEmpty
4+
{
5+
internal class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
{
10+
if (string.IsNullOrEmpty(nameof(args))) // bad: always false
11+
{
12+
}
13+
14+
string? x = null;
15+
if (string.IsNullOrEmpty(x)) // would be nice... bad: always true
16+
{
17+
}
18+
19+
string y = "";
20+
if (string.IsNullOrEmpty(y)) // would be nice... bad: always true
21+
{
22+
}
23+
24+
if (args[1] != null)
25+
y = "b";
26+
if (string.IsNullOrEmpty(y)) // good: non-constant
27+
{
28+
}
29+
30+
string z = " ";
31+
if (string.IsNullOrEmpty(z)) // would be nice... bad: always false
32+
{
33+
}
34+
35+
string a = "a";
36+
if (string.IsNullOrEmpty(a)) // would be nice... bad: always false
37+
{
38+
}
39+
40+
if (args[1] != null)
41+
a = "";
42+
if (string.IsNullOrEmpty(a)) // good: non-constant
43+
{
44+
}
45+
46+
if (string.IsNullOrEmpty(null)) // bad: always true
47+
{
48+
}
49+
50+
if (string.IsNullOrEmpty("")) // bad: always true
51+
{
52+
}
53+
54+
if (string.IsNullOrEmpty(" ")) // bad: always false
55+
{
56+
}
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)