-
Notifications
You must be signed in to change notification settings - Fork 195
Implement unused assignments lints on HIR #4285
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
Open
lucasly-ba
wants to merge
4
commits into
Rust-GCC:master
Choose a base branch
from
lucasly-ba:4260-unused-assignments
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+443
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4712032 to
344b6eb
Compare
CohenArthur
reviewed
Nov 14, 2025
Member
CohenArthur
left a comment
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 is really good work, well done!!!
189fcd2 to
95fc4c2
Compare
This change moves the unused variable checker from the type resolver to HIR. We can now use the HIR Default Visitor, and it will be much more easier to implement other unused lints with this change. gcc/rust/ChangeLog: * Make-lang.in: Add new files rules in Makefile. * lang.opt: Add new flag. * rust-session-manager.cc (Session::compile_crate): Execute new variable checker. * 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. gcc/testsuite/ChangeLog: * rust/compile/static_item_0.rs: New test. * rust/compile/template_function_0.rs: New test. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
gcc/rust/ChangeLog: * checks/lints/unused-var/rust-unused-var-checker.cc (UnusedVarChecker): Implement unused assignments warning. (UnusedVarChecker::go): Remove unique pointer unused var context. (UnusedVarChecker::visit): Visit AssignExpr in HIR default visitor. * checks/lints/unused-var/rust-unused-var-checker.h: Add visit method. * checks/lints/unused-var/rust-unused-var-collector.cc (UnusedVarCollector): Collect warnings for assignments. (UnusedVarCollector::visit): Visit AssignExpr in HIR default visitor. * checks/lints/unused-var/rust-unused-var-collector.h: Add visit method. * checks/lints/unused-var/rust-unused-var-context.cc (UnusedVarContext::add_assign): Add assignment in map. (UnusedVarContext::remove_assign): Remove assignment in map. (UnusedVarContext::is_variable_assigned): Check if a variable is assigned. * checks/lints/unused-var/rust-unused-var-context.h: Add a map to stock assignments. gcc/testsuite/ChangeLog: * rust/compile/issue-4260_0.rs: New test. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
gcc/rust/ChangeLog: * checks/lints/unused-var/rust-unused-var-checker.cc (UnusedVarChecker::visit): Change unused name warning to unused variable warning. * checks/lints/unused-var/rust-unused-var-collector.cc (UnusedVarCollector::visit): Remove useless methods. * checks/lints/unused-var/rust-unused-var-collector.h: Same here. * checks/lints/unused-var/rust-unused-var-context.cc (UnusedVarContext::add_variable): Add used variables to set. (UnusedVarContext::mark_used): Remove method. (UnusedVarContext::is_variable_used): Check if the set contains the hir id linked to a variable. (UnusedVarContext::as_string): Refactor method for new set. * checks/lints/unused-var/rust-unused-var-context.h: Refactor methods. * lang.opt: Change description for unused check flag. gcc/testsuite/ChangeLog: * rust/compile/static_item_0.rs: Modify warning output. * rust/compile/template_function_0.rs: Modify warning output. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
gcc/rust/ChangeLog: * Make-lang.in: Compile the right files. * checks/lints/unused-var/rust-unused-var-checker.cc: Move to... * checks/lints/unused/rust-unused-checker.cc: ...here. * checks/lints/unused-var/rust-unused-var-checker.h: Move to... * checks/lints/unused/rust-unused-checker.h: ...here. * checks/lints/unused-var/rust-unused-var-collector.cc: Move to... * checks/lints/unused/rust-unused-collector.cc: ...here. * checks/lints/unused-var/rust-unused-var-collector.h: Move to... * checks/lints/unused/rust-unused-collector.h: ...here. * checks/lints/unused-var/rust-unused-var-context.cc: Move to... * checks/lints/unused/rust-unused-context.cc: ...here. * checks/lints/unused-var/rust-unused-var-context.h: Move to... * checks/lints/unused/rust-unused-context.h: ...here. * rust-session-manager.cc (Session::compile_crate): Call the right method. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
95fc4c2 to
9353bdf
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4260
on top of PR #4055
Used the HIR default visitor for AssignExpr, added a map, to have all the unused assignments and attached some methods to it.
Since
is an HIR::PathInExpression we cannot visit the left expr of an assignment, because it would be considerered used. So I also added a walk_right to only visit the right expr.
( if we have a = b, we want to visit b)