diff --git a/csharp/ql/src/Useless code/FutileConditional.ql b/csharp/ql/src/Useless code/FutileConditional.ql index 1000113185d4..d77fd55a434a 100644 --- a/csharp/ql/src/Useless code/FutileConditional.ql +++ b/csharp/ql/src/Useless code/FutileConditional.ql @@ -16,7 +16,8 @@ predicate emptyStmt(Stmt s) { or s = any(BlockStmt bs | - bs.getNumberOfStmts() = 0 + bs.getNumberOfStmts() = 0 and + not any(CommentBlock cb).getParent() = bs or bs.getNumberOfStmts() = 1 and emptyStmt(bs.getStmt(0)) diff --git a/csharp/ql/src/change-notes/2025-03-05-useless-if-statement.md b/csharp/ql/src/change-notes/2025-03-05-useless-if-statement.md new file mode 100644 index 000000000000..3d62fe373e1e --- /dev/null +++ b/csharp/ql/src/change-notes/2025-03-05-useless-if-statement.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Don't consider an if-statement to be *useless* in `cs/useless-if-statement` if there is at least a comment. diff --git a/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.cs b/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.cs new file mode 100644 index 000000000000..fffdaf9de743 --- /dev/null +++ b/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.cs @@ -0,0 +1,34 @@ +using System; + +class FutileConditionalTest +{ + + public void M(string s) + { + if (s.Length > 0) ; // $ Alert + + if (s.Length > 1) + { + } // $ Alert + + if (s.Length > 2) // GOOD: because of else-branch + { + } + else + { + Console.WriteLine("hello"); + } + + if (s.Length > 3) + { + } + else + { + } // $ Alert + + if (s.Length > 4) + { + // GOOD: Because of the comment. + } + } +} diff --git a/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.expected b/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.expected new file mode 100644 index 000000000000..db9b75043cba --- /dev/null +++ b/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.expected @@ -0,0 +1,3 @@ +| FutileConditional.cs:8:9:8:27 | if (...) ... | If-statement with an empty then-branch and no else-branch. | +| FutileConditional.cs:10:9:12:9 | if (...) ... | If-statement with an empty then-branch and no else-branch. | +| FutileConditional.cs:22:9:27:9 | if (...) ... | If-statement with an empty then-branch and no else-branch. | diff --git a/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.qlref b/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.qlref new file mode 100644 index 000000000000..6f3548889789 --- /dev/null +++ b/csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.qlref @@ -0,0 +1,2 @@ +query: Useless code/FutileConditional.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/csharp/ql/test/query-tests/Useless Code/FutileConditional/options b/csharp/ql/test/query-tests/Useless Code/FutileConditional/options new file mode 100644 index 000000000000..75c39b4541ba --- /dev/null +++ b/csharp/ql/test/query-tests/Useless Code/FutileConditional/options @@ -0,0 +1,2 @@ +semmle-extractor-options: /nostdlib /noconfig +semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj