Skip to content

Commit 61c043f

Browse files
authored
Merge pull request github#18935 from michaelnebel/csharp/useless-if-statement
C#: Fewer alerts in `cs/useless-if-statement`.
2 parents a9ab39d + c73eeec commit 61c043f

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

csharp/ql/src/Useless code/FutileConditional.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ predicate emptyStmt(Stmt s) {
1616
or
1717
s =
1818
any(BlockStmt bs |
19-
bs.getNumberOfStmts() = 0
19+
bs.getNumberOfStmts() = 0 and
20+
not any(CommentBlock cb).getParent() = bs
2021
or
2122
bs.getNumberOfStmts() = 1 and
2223
emptyStmt(bs.getStmt(0))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Don't consider an if-statement to be *useless* in `cs/useless-if-statement` if there is at least a comment.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
class FutileConditionalTest
4+
{
5+
6+
public void M(string s)
7+
{
8+
if (s.Length > 0) ; // $ Alert
9+
10+
if (s.Length > 1)
11+
{
12+
} // $ Alert
13+
14+
if (s.Length > 2) // GOOD: because of else-branch
15+
{
16+
}
17+
else
18+
{
19+
Console.WriteLine("hello");
20+
}
21+
22+
if (s.Length > 3)
23+
{
24+
}
25+
else
26+
{
27+
} // $ Alert
28+
29+
if (s.Length > 4)
30+
{
31+
// GOOD: Because of the comment.
32+
}
33+
}
34+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| FutileConditional.cs:8:9:8:27 | if (...) ... | If-statement with an empty then-branch and no else-branch. |
2+
| FutileConditional.cs:10:9:12:9 | if (...) ... | If-statement with an empty then-branch and no else-branch. |
3+
| FutileConditional.cs:22:9:27:9 | if (...) ... | If-statement with an empty then-branch and no else-branch. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Useless code/FutileConditional.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj

0 commit comments

Comments
 (0)