Skip to content

Commit 80ba1df

Browse files
committed
gccrs: refactor unused var lint
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 <[email protected]>
1 parent 4cf082b commit 80ba1df

File tree

8 files changed

+19
-48
lines changed

8 files changed

+19
-48
lines changed

gcc/rust/checks/lints/unused-var/rust-unused-var-checker.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ UnusedVarChecker::visit (HIR::ConstantItem &item)
4444
std::string var_name = item.get_identifier ().as_string ();
4545
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
4646
auto id = item.get_mappings ().get_hirid ();
47-
if (!unused_var_context.is_variable_used (id) && !starts_with_under_score)
48-
rust_warning_at (item.get_locus (), OPT_Wunused_variable, "unused name %qs",
47+
if (!unused_context.is_variable_used (id) && !starts_with_under_score)
48+
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
49+
"unused variable %qs",
4950
item.get_identifier ().as_string ().c_str ());
5051
}
5152

@@ -55,8 +56,8 @@ UnusedVarChecker::visit (HIR::StaticItem &item)
5556
std::string var_name = item.get_identifier ().as_string ();
5657
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
5758
auto id = item.get_mappings ().get_hirid ();
58-
if (!unused_var_context.is_variable_used (id) && !starts_with_under_score)
59-
rust_warning_at (item.get_locus (), OPT_Wunused_variable, "unused name %qs",
59+
if (!unused_context.is_variable_used (id) && !starts_with_under_score)
60+
rust_warning_at (item.get_locus (), OPT_Wunused_variable, "unused variable %qs",
6061
item.get_identifier ().as_string ().c_str ());
6162
}
6263

@@ -74,7 +75,7 @@ UnusedVarChecker::visit (HIR::IdentifierPattern &pattern)
7475
if (!unused_var_context.is_variable_used (id) && var_name != "self"
7576
&& !starts_with_under_score)
7677
rust_warning_at (pattern.get_locus (), OPT_Wunused_variable,
77-
"unused name %qs",
78+
"unused variable %qs",
7879
pattern.get_identifier ().as_string ().c_str ());
7980
}
8081
void

gcc/rust/checks/lints/unused-var/rust-unused-var-collector.cc

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,25 @@ UnusedVarCollector::go (HIR::Crate &crate)
3939
item->accept_vis (*this);
4040
}
4141

42-
void
43-
UnusedVarCollector::visit (HIR::ConstantItem &item)
44-
{
45-
unused_var_context.add_variable (item.get_mappings ().get_hirid ());
46-
walk (item);
47-
}
48-
49-
void
50-
UnusedVarCollector::visit (HIR::StaticItem &item)
51-
{
52-
unused_var_context.add_variable (item.get_mappings ().get_hirid ());
53-
walk (item);
54-
}
55-
56-
void
57-
UnusedVarCollector::visit (HIR::IdentifierPattern &pattern)
58-
{
59-
unused_var_context.add_variable (pattern.get_mappings ().get_hirid ());
60-
}
61-
6242
void
6343
UnusedVarCollector::visit (HIR::PathInExpression &expr)
6444
{
6545
mark_path_used (expr);
46+
walk (expr);
6647
}
6748

6849
void
6950
UnusedVarCollector::visit (HIR::QualifiedPathInExpression &expr)
7051
{
7152
mark_path_used (expr);
53+
walk (expr);
7254
}
7355

7456
void
7557
UnusedVarCollector::visit (HIR::StructExprFieldIdentifier &ident)
7658
{
7759
mark_path_used (ident);
60+
walk (ident);
7861
}
7962
void
8063
UnusedVarCollector::visit (HIR::AssignmentExpr &expr)

gcc/rust/checks/lints/unused-var/rust-unused-var-collector.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ class UnusedVarCollector : public HIR::DefaultHIRVisitor
4242
using HIR::DefaultHIRVisitor::visit;
4343
virtual void visit (HIR::PathInExpression &expr) override;
4444
virtual void visit (HIR::StructExprFieldIdentifier &ident) override;
45-
virtual void visit (HIR::ConstantItem &item) override;
46-
virtual void visit (HIR::StaticItem &item) override;
47-
virtual void visit (HIR::IdentifierPattern &pattern) override;
4845
virtual void visit (HIR::QualifiedPathInExpression &expr) override;
4946
virtual void visit (HIR::AssignmentExpr &expr) override;
5047

@@ -59,7 +56,7 @@ class UnusedVarCollector : public HIR::DefaultHIRVisitor
5956
template <typename T> void mark_path_used (T &path_expr)
6057
{
6158
auto def_id = get_def_id (path_expr);
62-
unused_var_context.mark_used (def_id);
59+
unused_var_context.add_variable (def_id);
6360
unused_var_context.remove_assign (def_id);
6461
}
6562
};

gcc/rust/checks/lints/unused-var/rust-unused-var-context.cc

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,15 @@ namespace Analysis {
2323

2424
void
2525
UnusedVarContext::add_variable (HirId id)
26-
{
27-
if (is_used.find (id) == is_used.end ())
28-
is_used.insert ({id, false});
29-
}
3026

31-
void
32-
UnusedVarContext::mark_used (HirId id)
3327
{
34-
is_used[id] = true;
28+
used_vars.emplace (id);
3529
}
3630

3731
bool
3832
UnusedVarContext::is_variable_used (HirId id) const
3933
{
40-
auto it = is_used.find (id);
41-
return it != is_used.end () && it->second;
34+
return used_vars.find (id) != used_vars.end ();
4235
}
4336

4437
void
@@ -66,10 +59,9 @@ UnusedVarContext::as_string () const
6659
{
6760
std::stringstream ss;
6861
ss << "UnusedVarContext: ";
69-
for (const auto &pair : is_used)
62+
for (const auto &v : used_vars)
7063
{
71-
ss << "HirId: " << pair.first << " Used: " << (pair.second ? "Yes" : "No")
72-
<< "\n";
64+
ss << "HirId: " << v << "\n";
7365
}
7466
return ss.str ();
7567
}

gcc/rust/checks/lints/unused-var/rust-unused-var-context.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ class UnusedVarContext
2525
{
2626
public:
2727
void add_variable (HirId id);
28-
void mark_used (HirId id);
2928
bool is_variable_used (HirId id) const;
30-
3129
void add_assign (HirId id_def, HirId id);
3230
void remove_assign (HirId id_def);
3331
bool is_variable_assigned (HirId id_def, HirId id);
3432

3533
std::string as_string () const;
3634

3735
private:
38-
std::map<HirId, bool> is_used;
36+
std::unordered_set<HirId> used_vars;
3937
std::map<HirId, std::vector<HirId>> assigned_vars;
4038
};
4139

gcc/rust/lang.opt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,6 @@ Define a built-in offset_of macro in the compiler and assume it is present
235235

236236
frust-unused-check-2.0
237237
Rust Var(flag_unused_check_2_0)
238-
Use the new unused variable check instead of old one
238+
Use the new unused variable check implementation.
239239

240240
; This comment is to ensure we retain the blank line above.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// { dg-additional-options "-frust-unused-check-2.0" }
22
static TEST: usize = 1;
3-
// { dg-warning "unused name" "" { target *-*-* } .-1 }
3+
// { dg-warning "unused variable .TEST." "" { target *-*-* } .-1 }

gcc/testsuite/rust/compile/template_function_0.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
pub trait Sized {}
44

55
pub fn test<T> (a: usize) -> () {
6-
// { dg-warning "unused name" "" { target *-*-* } .-1 }
7-
}
6+
// { dg-warning "unused variable .a." "" { target *-*-* } .-1 }
7+
}

0 commit comments

Comments
 (0)