File tree Expand file tree Collapse file tree 8 files changed +80
-10
lines changed Expand file tree Collapse file tree 8 files changed +80
-10
lines changed Original file line number Diff line number Diff line change 1717// <http://www.gnu.org/licenses/>.
1818
1919#include " rust-unused-var-checker.h"
20+ #include " rust-hir-expr.h"
2021#include " rust-hir-item.h"
2122
2223#include " options.h"
@@ -78,5 +79,22 @@ UnusedVarChecker::visit (HIR::IdentifierPattern &pattern)
7879 " unused name '%s'" ,
7980 pattern.get_identifier ().as_string ().c_str ());
8081}
82+ void
83+
84+ UnusedVarChecker::visit (HIR::AssignmentExpr &expr)
85+
86+ {
87+ const auto &lhs = expr.get_lhs ();
88+ std::string var_name = lhs.as_string ();
89+ bool starts_with_under_score = var_name.compare (0 , 1 , " _" ) == 0 ;
90+ NodeId ast_node_id = lhs.get_mappings ().get_nodeid ();
91+ NodeId def_id = nr_context.lookup (ast_node_id).value ();
92+ HirId id = mappings.lookup_node_to_hir (def_id).value ();
93+ if (unused_var_context->is_variable_assigned (
94+ id, lhs.get_mappings ().get_hirid ())
95+ && !starts_with_under_score)
96+ rust_warning_at (lhs.get_locus (), OPT_Wunused_variable,
97+ " unused assignment '%s'" , var_name.c_str ());
98+ }
8199} // namespace Analysis
82- } // namespace Rust
100+ } // namespace Rust
Original file line number Diff line number Diff line change 1616// along with GCC; see the file COPYING3. If not see
1717// <http://www.gnu.org/licenses/>.
1818
19+ #include " rust-hir-expr.h"
1920#include " rust-hir-item.h"
2021#include " rust-hir-pattern.h"
2122#include " rust-hir-visitor.h"
@@ -40,6 +41,7 @@ class UnusedVarChecker : public HIR::DefaultHIRVisitor
4041 virtual void visit (HIR::ConstantItem &item) override ;
4142 virtual void visit (HIR::StaticItem &item) override ;
4243 virtual void visit (HIR::IdentifierPattern &identifier) override ;
44+ virtual void visit (HIR::AssignmentExpr &identifier) override ;
4345};
4446} // namespace Analysis
45- } // namespace Rust
47+ } // namespace Rust
Original file line number Diff line number Diff line change 1717// <http://www.gnu.org/licenses/>.
1818
1919#include " rust-unused-var-collector.h"
20+ #include " rust-hir-expr.h"
2021#include " rust-hir-full-decls.h"
2122#include " rust-hir-item.h"
2223#include " rust-hir-path.h"
2324#include " rust-hir-pattern.h"
2425#include " rust-immutable-name-resolution-context.h"
26+ #include " tree-check.h"
2527
2628namespace Rust {
2729namespace Analysis {
@@ -54,8 +56,7 @@ UnusedVarCollector::visit (HIR::StaticItem &item)
5456void
5557UnusedVarCollector::visit (HIR::IdentifierPattern &pattern)
5658{
57- auto id = pattern.get_mappings ().get_hirid ();
58- unused_var_context.add_variable (id);
59+ unused_var_context.add_variable (pattern.get_mappings ().get_hirid ());
5960}
6061
6162void
@@ -75,5 +76,14 @@ UnusedVarCollector::visit (HIR::StructExprFieldIdentifier &ident)
7576{
7677 mark_path_used (ident);
7778}
79+ void
80+ UnusedVarCollector::visit (HIR::AssignmentExpr &expr)
81+ {
82+ auto def_id = get_def_id (expr);
83+ HirId id = expr.get_lhs ().get_mappings ().get_hirid ();
84+ unused_var_context.add_assign (def_id, id);
85+ walk_right (expr);
86+ }
87+
7888} // namespace Analysis
7989} // namespace Rust
Original file line number Diff line number Diff line change @@ -46,14 +46,22 @@ class UnusedVarCollector : public HIR::DefaultHIRVisitor
4646 virtual void visit (HIR::StaticItem &item) override ;
4747 virtual void visit (HIR::IdentifierPattern &pattern) override ;
4848 virtual void visit (HIR::QualifiedPathInExpression &expr) override ;
49+ virtual void visit (HIR::AssignmentExpr &expr) override ;
4950
50- template <typename T> void mark_path_used (T &path_expr)
51+ template <typename T> HirId get_def_id (T &path_expr)
5152 {
5253 NodeId ast_node_id = path_expr.get_mappings ().get_nodeid ();
53- NodeId def_id = nr_context.lookup (ast_node_id).value ();
54- HirId hir_id = mappings.lookup_node_to_hir (def_id).value ();
55- unused_var_context.mark_used (hir_id);
54+ NodeId id = nr_context.lookup (ast_node_id).value ();
55+ HirId def_id = mappings.lookup_node_to_hir (id).value ();
56+ return def_id;
57+ }
58+
59+ template <typename T> void mark_path_used (T &path_expr)
60+ {
61+ auto def_id = get_def_id (path_expr);
62+ unused_var_context.mark_used (def_id);
63+ unused_var_context.remove_assign (def_id);
5664 }
5765};
5866} // namespace Analysis
59- } // namespace Rust
67+ } // namespace Rust
Original file line number Diff line number Diff line change @@ -41,6 +41,26 @@ UnusedVarContext::is_variable_used (HirId id) const
4141 return it != is_used.end () && it->second ;
4242}
4343
44+ void
45+ UnusedVarContext::add_assign (HirId id_def, HirId id)
46+ {
47+ assigned_vars[id_def].push_back (id);
48+ }
49+
50+ void
51+ UnusedVarContext::remove_assign (HirId id_def)
52+ {
53+ if (assigned_vars.find (id_def) != assigned_vars.end ())
54+ assigned_vars[id_def].pop_back ();
55+ }
56+ bool
57+ UnusedVarContext::is_variable_assigned (HirId id_def, HirId id)
58+ {
59+ auto assigned_vec = assigned_vars[id_def];
60+ return std::find (assigned_vec.begin (), assigned_vec.end (), id)
61+ != assigned_vec.end ();
62+ }
63+
4464std::string
4565UnusedVarContext::as_string () const
4666{
Original file line number Diff line number Diff line change @@ -25,13 +25,17 @@ class UnusedVarContext
2525public:
2626 void add_variable (HirId id);
2727 void mark_used (HirId id);
28-
2928 bool is_variable_used (HirId id) const ;
3029
30+ void add_assign (HirId id_def, HirId id);
31+ void remove_assign (HirId id_def);
32+ bool is_variable_assigned (HirId id_def, HirId id);
33+
3134 std::string as_string () const ;
3235
3336private:
3437 std::map<HirId, bool > is_used;
38+ std::map<HirId, std::vector<HirId>> assigned_vars;
3539};
3640} // namespace Analysis
3741} // namespace Rust
Original file line number Diff line number Diff line change @@ -197,6 +197,13 @@ DefaultHIRVisitor::walk (AssignmentExpr &expr)
197197 expr.get_rhs ().accept_vis (*this );
198198}
199199
200+ void
201+ DefaultHIRVisitor::walk_right (AssignmentExpr &expr)
202+ {
203+ visit_outer_attrs (expr);
204+ expr.get_rhs ().accept_vis (*this );
205+ }
206+
200207void
201208DefaultHIRVisitor::walk (CompoundAssignmentExpr &expr)
202209{
Original file line number Diff line number Diff line change @@ -356,6 +356,7 @@ class DefaultHIRVisitor : public HIRFullVisitor
356356 virtual void walk (LazyBooleanExpr &) final ;
357357 virtual void walk (TypeCastExpr &) final ;
358358 virtual void walk (AssignmentExpr &) final ;
359+ virtual void walk_right (AssignmentExpr &) final ;
359360 virtual void walk (CompoundAssignmentExpr &) final ;
360361 virtual void walk (GroupedExpr &) final ;
361362
You can’t perform that action at this time.
0 commit comments