diff --git a/csharp/ql/src/Likely Bugs/Statements/test.ql b/csharp/ql/src/Likely Bugs/Statements/test.ql new file mode 100644 index 000000000000..f03b720b8ee0 --- /dev/null +++ b/csharp/ql/src/Likely Bugs/Statements/test.ql @@ -0,0 +1,45 @@ +/** + * @name Empty branch of conditional, or empty loop body + * @description Empty blocks that occur as a branch of a conditional or as a loop body may indicate + * badly-maintained code or a bug due to an unhandled case. + * @kind problem + * @problem.severity warning + * @precision high + * @id cs/empty-block-test + * @tags reliability + * readability + */ + +import csharp + +predicate loopStmtWithEptyBlock(BlockStmt child) { + exists(LoopStmt stmt, SourceLocation l | + stmt.getAChild() = child and + child.getNumberOfStmts() = 0 and + child.getLocation() = l and + l.getStartLine() != l.getEndLine() + ) +} + +predicate conditionalWithEmptyBlock(BlockStmt child) { + exists(IfStmt stmt | + stmt.getThen() = child and child.getNumberOfStmts() = 0 and not exists(stmt.getElse()) + ) + or + exists(IfStmt stmt, SourceLocation l | + stmt.getThen() = child and + child.getNumberOfStmts() = 0 and + exists(stmt.getElse()) and + child.getLocation() = l and + l.getStartLine() != l.getEndLine() + ) + or + exists(IfStmt stmt | stmt.getElse() = child and child.getNumberOfStmts() = 0) +} + +from BlockStmt s +where + (loopStmtWithEmptyBlock(s) or conditionalWithEmptyBlock(s)) and + not exists(CommentBlock c | c.getParent() = s) and + not exists(ForStmt fs | fs.getBody() = s and exists(fs.getAnUpdate())) +select s, "Empty block without comment."