-
Notifications
You must be signed in to change notification settings - Fork 200
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.
+444
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright (C) 2025 Free Software Foundation, Inc. | ||
|
|
||
| // This file is part of GCC. | ||
|
|
||
| // GCC is free software; you can redistribute it and/or modify it under | ||
| // the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 3, or (at your option) any later | ||
| // version. | ||
|
|
||
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| // for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with GCC; see the file COPYING3. If not see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #include "rust-unused-checker.h" | ||
| #include "rust-hir-expr.h" | ||
| #include "rust-hir-item.h" | ||
|
|
||
| #include "options.h" | ||
| #include "rust-keyword-values.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Analysis { | ||
| UnusedChecker::UnusedChecker () | ||
| : nr_context ( | ||
| Resolver2_0::ImmutableNameResolutionContext::get ().resolver ()), | ||
| mappings (Analysis::Mappings::get ()), unused_context (UnusedContext ()) | ||
| {} | ||
| void | ||
| UnusedChecker::go (HIR::Crate &crate) | ||
| { | ||
| UnusedCollector collector (unused_context); | ||
| collector.go (crate); | ||
| for (auto &item : crate.get_items ()) | ||
| item->accept_vis (*this); | ||
| } | ||
|
|
||
| void | ||
| UnusedChecker::visit (HIR::ConstantItem &item) | ||
| { | ||
| std::string var_name = item.get_identifier ().as_string (); | ||
| auto id = item.get_mappings ().get_hirid (); | ||
| if (!unused_context.is_variable_used (id) && var_name[0] != '_') | ||
| rust_warning_at (item.get_locus (), OPT_Wunused_variable, | ||
| "unused variable %qs", | ||
| item.get_identifier ().as_string ().c_str ()); | ||
| } | ||
|
|
||
| void | ||
| UnusedChecker::visit (HIR::StaticItem &item) | ||
| { | ||
| std::string var_name = item.get_identifier ().as_string (); | ||
| auto id = item.get_mappings ().get_hirid (); | ||
| if (!unused_context.is_variable_used (id) && var_name[0] != '_') | ||
| rust_warning_at (item.get_locus (), OPT_Wunused_variable, | ||
| "unused variable %qs", | ||
| item.get_identifier ().as_string ().c_str ()); | ||
| } | ||
|
|
||
| void | ||
| UnusedChecker::visit (HIR::TraitItemFunc &item) | ||
| { | ||
| // TODO: check trait item functions if they are not derived. | ||
| } | ||
| void | ||
| UnusedChecker::visit (HIR::IdentifierPattern &pattern) | ||
| { | ||
| std::string var_name = pattern.get_identifier ().as_string (); | ||
| auto id = pattern.get_mappings ().get_hirid (); | ||
| if (!unused_context.is_variable_used (id) | ||
| && var_name != Values::Keywords::SELF && var_name[0] != '_') | ||
| rust_warning_at (pattern.get_locus (), OPT_Wunused_variable, | ||
| "unused variable %qs", | ||
| pattern.get_identifier ().as_string ().c_str ()); | ||
| } | ||
| void | ||
|
|
||
| UnusedChecker::visit (HIR::AssignmentExpr &expr) | ||
|
|
||
| { | ||
| const auto &lhs = expr.get_lhs (); | ||
| auto s = lhs.as_string (); | ||
| std::string var_name = s.substr (0, s.find (':')); | ||
| NodeId ast_node_id = lhs.get_mappings ().get_nodeid (); | ||
| NodeId def_id = nr_context.lookup (ast_node_id).value (); | ||
| HirId id = mappings.lookup_node_to_hir (def_id).value (); | ||
| if (unused_context.is_variable_assigned (id, lhs.get_mappings ().get_hirid ()) | ||
| && var_name[0] != '_') | ||
| rust_warning_at (lhs.get_locus (), OPT_Wunused_variable, | ||
| "unused assignment %qs", var_name.c_str ()); | ||
| } | ||
| } // namespace Analysis | ||
| } // namespace Rust | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright (C) 2025 Free Software Foundation, Inc. | ||
|
|
||
| // This file is part of GCC. | ||
|
|
||
| // GCC is free software; you can redistribute it and/or modify it under | ||
| // the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 3, or (at your option) any later | ||
| // version. | ||
|
|
||
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| // for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with GCC; see the file COPYING3. If not see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #include "rust-hir-expr.h" | ||
| #include "rust-hir-item.h" | ||
| #include "rust-hir-pattern.h" | ||
| #include "rust-hir-visitor.h" | ||
| #include "rust-unused-collector.h" | ||
| #include "rust-immutable-name-resolution-context.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Analysis { | ||
| class UnusedChecker : public HIR::DefaultHIRVisitor | ||
| { | ||
| public: | ||
| UnusedChecker (); | ||
| void go (HIR::Crate &crate); | ||
|
|
||
| private: | ||
| const Resolver2_0::NameResolutionContext &nr_context; | ||
| Analysis::Mappings &mappings; | ||
| UnusedContext unused_context; | ||
|
|
||
| using HIR::DefaultHIRVisitor::visit; | ||
|
|
||
| virtual void visit (HIR::TraitItemFunc &decl) override; | ||
| virtual void visit (HIR::ConstantItem &item) override; | ||
| virtual void visit (HIR::StaticItem &item) override; | ||
| virtual void visit (HIR::IdentifierPattern &identifier) override; | ||
| virtual void visit (HIR::AssignmentExpr &identifier) override; | ||
| }; | ||
| } // namespace Analysis | ||
| } // namespace Rust |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (C) 2025 Free Software Foundation, Inc. | ||
|
|
||
| // This file is part of GCC. | ||
|
|
||
| // GCC is free software; you can redistribute it and/or modify it under | ||
| // the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 3, or (at your option) any later | ||
| // version. | ||
|
|
||
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| // for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with GCC; see the file COPYING3. If not see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #include "rust-unused-collector.h" | ||
| #include "rust-hir-expr.h" | ||
| #include "rust-hir-full-decls.h" | ||
| #include "rust-hir-item.h" | ||
| #include "rust-hir-path.h" | ||
| #include "rust-hir-pattern.h" | ||
| #include "rust-immutable-name-resolution-context.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Analysis { | ||
| UnusedCollector::UnusedCollector (UnusedContext &context) | ||
| : nr_context ( | ||
| Resolver2_0::ImmutableNameResolutionContext::get ().resolver ()), | ||
| mappings (Analysis::Mappings::get ()), unused_context (context) | ||
| {} | ||
| void | ||
| UnusedCollector::go (HIR::Crate &crate) | ||
| { | ||
| for (auto &item : crate.get_items ()) | ||
| item->accept_vis (*this); | ||
| } | ||
|
|
||
| void | ||
| UnusedCollector::visit (HIR::PathInExpression &expr) | ||
| { | ||
| mark_path_used (expr); | ||
| walk (expr); | ||
| } | ||
|
|
||
| void | ||
| UnusedCollector::visit (HIR::QualifiedPathInExpression &expr) | ||
| { | ||
| mark_path_used (expr); | ||
| walk (expr); | ||
| } | ||
|
|
||
| void | ||
| UnusedCollector::visit (HIR::StructExprFieldIdentifier &ident) | ||
| { | ||
| mark_path_used (ident); | ||
| walk (ident); | ||
| } | ||
| void | ||
| UnusedCollector::visit (HIR::AssignmentExpr &expr) | ||
| { | ||
| auto def_id = get_def_id (expr.get_lhs ()); | ||
| HirId id = expr.get_lhs ().get_mappings ().get_hirid (); | ||
| unused_context.add_assign (def_id, id); | ||
| visit_outer_attrs (expr); | ||
| expr.get_rhs ().accept_vis (*this); | ||
| } | ||
|
|
||
| } // namespace Analysis | ||
| } // namespace Rust |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright (C) 2025 Free Software Foundation, Inc. | ||
|
|
||
| // This file is part of GCC. | ||
|
|
||
| // GCC is free software; you can redistribute it and/or modify it under | ||
| // the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 3, or (at your option) any later | ||
| // version. | ||
|
|
||
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| // for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with GCC; see the file COPYING3. If not see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #include "rust-hir-expr.h" | ||
| #include "rust-hir-path.h" | ||
| #include "rust-hir-pattern.h" | ||
| #include "rust-hir-visitor.h" | ||
| #include "rust-mapping-common.h" | ||
| #include "rust-name-resolution-context.h" | ||
| #include "rust-unused-context.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Analysis { | ||
| class UnusedCollector : public HIR::DefaultHIRVisitor | ||
| { | ||
| public: | ||
| UnusedCollector (UnusedContext &context); | ||
| void go (HIR::Crate &crate); | ||
|
|
||
| private: | ||
| const Resolver2_0::NameResolutionContext &nr_context; | ||
| Analysis::Mappings &mappings; | ||
| UnusedContext &unused_context; | ||
|
|
||
| using HIR::DefaultHIRVisitor::visit; | ||
| virtual void visit (HIR::PathInExpression &expr) override; | ||
| virtual void visit (HIR::StructExprFieldIdentifier &ident) override; | ||
| virtual void visit (HIR::QualifiedPathInExpression &expr) override; | ||
| virtual void visit (HIR::AssignmentExpr &expr) override; | ||
|
|
||
| template <typename T> HirId get_def_id (T &path_expr) | ||
| { | ||
| NodeId ast_node_id = path_expr.get_mappings ().get_nodeid (); | ||
| NodeId id = nr_context.lookup (ast_node_id).value (); | ||
| HirId def_id = mappings.lookup_node_to_hir (id).value (); | ||
| return def_id; | ||
| } | ||
|
|
||
| template <typename T> void mark_path_used (T &path_expr) | ||
| { | ||
| auto def_id = get_def_id (path_expr); | ||
| unused_context.add_variable (def_id); | ||
| unused_context.remove_assign (def_id); | ||
| } | ||
| }; | ||
| } // namespace Analysis | ||
| } // namespace Rust |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright (C) 2025 Free Software Foundation, Inc. | ||
|
|
||
| // This file is part of GCC. | ||
|
|
||
| // GCC is free software; you can redistribute it and/or modify it under | ||
| // the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 3, or (at your option) any later | ||
| // version. | ||
|
|
||
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| // for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with GCC; see the file COPYING3. If not see | ||
| // <http://www.gnu.org/licenses/>. | ||
|
|
||
| #include "rust-unused-context.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Analysis { | ||
|
|
||
| void | ||
| UnusedContext::add_variable (HirId id) | ||
|
|
||
| { | ||
| used_vars.emplace (id); | ||
| } | ||
|
|
||
| bool | ||
| UnusedContext::is_variable_used (HirId id) const | ||
| { | ||
| return used_vars.find (id) != used_vars.end (); | ||
| } | ||
|
|
||
| void | ||
| UnusedContext::add_assign (HirId id_def, HirId id) | ||
| { | ||
| assigned_vars[id_def].push_back (id); | ||
| } | ||
|
|
||
| void | ||
| UnusedContext::remove_assign (HirId id_def) | ||
| { | ||
| if (assigned_vars.find (id_def) != assigned_vars.end ()) | ||
| assigned_vars[id_def].pop_back (); | ||
| } | ||
| bool | ||
| UnusedContext::is_variable_assigned (HirId id_def, HirId id) | ||
| { | ||
| auto assigned_vec = assigned_vars[id_def]; | ||
| return std::find (assigned_vec.begin (), assigned_vec.end (), id) | ||
| != assigned_vec.end (); | ||
| } | ||
|
|
||
| std::string | ||
| UnusedContext::as_string () const | ||
| { | ||
| std::stringstream ss; | ||
| ss << "UnusedContext: "; | ||
| for (const auto &v : used_vars) | ||
| { | ||
| ss << "HirId: " << v << "\n"; | ||
| } | ||
| return ss.str (); | ||
| } | ||
|
|
||
| } // namespace Analysis | ||
| } // namespace Rust |
Oops, something went wrong.
Oops, something went wrong.
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.
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're trying to parse the string representation from your AST ? Something looks wrong over here.