-
Notifications
You must be signed in to change notification settings - Fork 185
Implement unused variable checker on HIR #4055
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
base: master
Are you sure you want to change the base?
Conversation
c2bb066
to
038d637
Compare
gcc/rust/ChangeLog: * Make-lang.in: New file. * checks/lints/unused-var/rust-unused-var-checker.cc (UnusedVarChecker): Implement unused variable checker. * checks/lints/unused-var/rust-unused-var-checker.h (UnusedVarChecker): Implement unused variable checker. * checks/lints/unused-var/rust-unused-var-collector.cc (UnusedVarCollector): Implement unused variable collector. * checks/lints/unused-var/rust-unused-var-collector.h (UnusedVarCollector): Implement unused variable collector. * checks/lints/unused-var/rust-unused-var-context.cc (UnusedVarContext): Implement unused variable context. * checks/lints/unused-var/rust-unused-var-context.h (UnusedVarContext): Implement unused variable context. Signed-off-by: Ryutaro Okada <[email protected]>
038d637
to
f27c7a7
Compare
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.
I think this is a really good base and the code looks good to me! I think it is important to add testcases for features like these, so it would be good if you could add a flag to enable the lints you've written and then add testcases to the testsuite
UnusedVarContext * | ||
UnusedVarContext::get () | ||
{ | ||
static UnusedVarContext *instance; | ||
if (instance == nullptr) | ||
instance = new UnusedVarContext (); | ||
return instance; | ||
} |
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.
I think this is fine but we should also check if we can do without a global variable
UnusedVarCollector::visit (HIR::LetStmt &stmt) | ||
{ | ||
HIR::Pattern &pattern = stmt.get_pattern (); | ||
collect_variable (pattern); |
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.
can't we visit instead here? mabye I'm missing something but I think all patterns should create variables to check for, so we could just do visit(pattern)
and then create visitors for all our kinds of patterns instead of manually dispatching like in collect_variable
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.
Thank you @sakupan102, this is really good work!!! Just a couple nitpicks and we should merge this. Well done :)
if (auto id = nr_context.lookup (ast_node_id)) | ||
def_id = *id; | ||
else | ||
return; |
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.
so if we fail to lookup the variable we silently return and do nothing - I think it would be worth crashing if we encounter this instead, as it should never happen
if (auto id = nr_context.lookup (ast_node_id)) | |
def_id = *id; | |
else | |
return; | |
NodeId def_id = nr_context.lookup (ast_node_id).value(); |
if (!hir_id) | ||
return; |
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.
likewise
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.
I think we don't need this file, 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.
... You can move this file to the regular gcc/testsuite/rust/compile
folder, and add the following line to the top of the file:
// { dg-additional-options "-frust-unused-check-2.0" }
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.
Likewise here :)
@CohenArthur |
This PR closes #4086, #4085
make check-rust
passes locallyclang-format