-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Rust: Unreachable code query #17525
Rust: Unreachable code query #17525
Conversation
QHelp previews: rust/ql/src/queries/unusedentities/UnreachableCode.qhelpUnreachable codeThis rule finds code that is never reached. Unused code should be removed to increase readability and avoid confusion. RecommendationRemove any unreachable code. ExampleIn the following example, the final
The problem can be fixed simply by removing the unreachable code:
References
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine to me. However, it may be worth fixing some of the missing CFG steps instead of marking a lot of false results in the expected output. By the looks of it they are mainly caused by the lack of special handling of boolean literals. I updated the branch to pull in the latest CFG improvements, with a bit of luck some of the issues have already been fixed.
@hvitved Could you have a quick look at this too? |
* Holds if `n` is an AST node that's unreachable. | ||
*/ | ||
private predicate unreachable(AstNode n) { | ||
not n = any(CfgNode cfn).getAstNode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to restrict this to nodes that can possibly be part of a CFG, for example ParenExpr
s cannot. I think the best heuristic is simply n instanceof ControlFlowTree
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
n instanceof ControlFlowGraphImpl::ControlFlowTree
doesn't work ... unless this fix requires more of your work-in-progress to apply?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Try with
exists(ControlFlowGraphImpl::ControlFlowTree cft |
cft.succ(n, _, _)
or
cft.succ(_, n, _)
)
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that works perfectly and I can see what the intent is now.
I've updated the PR.
Unreachable code query for Rust.
@paldepind please would you review, particularly whether the logic in
firstUnreachable
andskipNode
is reasonable and written as cleanly as possible right now; and whether there are obvious explanations for the false positive results in the test?