Skip to content

Commit a2d62a6

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 <lucas.ly-ba@outlook.com>
1 parent 5f32f9e commit a2d62a6

File tree

9 files changed

+29
-57
lines changed

9 files changed

+29
-57
lines changed

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "rust-hir-item.h"
2222

2323
#include "options.h"
24+
#include "rust-keyword-values.h"
2425

2526
namespace Rust {
2627
namespace Analysis {
@@ -38,25 +39,26 @@ UnusedVarChecker::go (HIR::Crate &crate)
3839
for (auto &item : crate.get_items ())
3940
item->accept_vis (*this);
4041
}
42+
4143
void
4244
UnusedVarChecker::visit (HIR::ConstantItem &item)
4345
{
4446
std::string var_name = item.get_identifier ().as_string ();
45-
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
4647
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",
48+
if (!unused_context.is_variable_used (id) && var_name[0] != '_')
49+
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
50+
"unused variable %qs",
4951
item.get_identifier ().as_string ().c_str ());
5052
}
5153

5254
void
5355
UnusedVarChecker::visit (HIR::StaticItem &item)
5456
{
5557
std::string var_name = item.get_identifier ().as_string ();
56-
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) && var_name[0] != '_')
60+
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
61+
"unused variable %qs",
6062
item.get_identifier ().as_string ().c_str ());
6163
}
6264

@@ -69,12 +71,11 @@ void
6971
UnusedVarChecker::visit (HIR::IdentifierPattern &pattern)
7072
{
7173
std::string var_name = pattern.get_identifier ().as_string ();
72-
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
7374
auto id = pattern.get_mappings ().get_hirid ();
74-
if (!unused_var_context.is_variable_used (id) && var_name != "self"
75-
&& !starts_with_under_score)
75+
if (!unused_context.is_variable_used (id)
76+
&& var_name != Values::Keywords::SELF && var_name[0] != '_')
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
@@ -85,13 +86,11 @@ UnusedVarChecker::visit (HIR::AssignmentExpr &expr)
8586
const auto &lhs = expr.get_lhs ();
8687
auto s = lhs.as_string ();
8788
std::string var_name = s.substr (0, s.find (':'));
88-
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
8989
NodeId ast_node_id = lhs.get_mappings ().get_nodeid ();
9090
NodeId def_id = nr_context.lookup (ast_node_id).value ();
9191
HirId id = mappings.lookup_node_to_hir (def_id).value ();
92-
if (unused_var_context.is_variable_assigned (id,
93-
lhs.get_mappings ().get_hirid ())
94-
&& !starts_with_under_score)
92+
if (unused_context.is_variable_assigned (id, lhs.get_mappings ().get_hirid ())
93+
&& var_name[0] != '_')
9594
rust_warning_at (lhs.get_locus (), OPT_Wunused_variable,
9695
"unused assignment %qs", var_name.c_str ());
9796
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class UnusedVarChecker : public HIR::DefaultHIRVisitor
3737
UnusedVarContext unused_var_context;
3838

3939
using HIR::DefaultHIRVisitor::visit;
40+
4041
virtual void visit (HIR::TraitItemFunc &decl) override;
4142
virtual void visit (HIR::ConstantItem &item) override;
4243
virtual void visit (HIR::StaticItem &item) override;

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 }
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// { dg-additional-options "-frust-unused-check-2.0" }
2+
#![feature(lang_items)]
3+
24
#[lang = "sized"]
35
pub trait Sized {}
46

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

0 commit comments

Comments
 (0)