Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create test.ql #144

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions csharp/ql/src/Likely Bugs/Statements/test.ql
Original file line number Diff line number Diff line change
@@ -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."
Loading