Skip to content

Commit 3958409

Browse files
committed
gccrs: refactor dead code lint
This patch is simple, it only moves the check of unused static items and unused const items into the dead-code scan visitor. gcc/rust/ChangeLog: * checks/lints/rust-lint-scan-deadcode.h: Warns if there is an unused static item or unused const item. * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit): Removes static item and const item. * checks/lints/unused/rust-unused-checker.h: Same here. gcc/testsuite/ChangeLog: * rust/compile/static_item_0.rs: Change warning description. * rust/compile/const_item_0.rs: New test. Signed-off-by: Lucas Ly Ba <[email protected]>
1 parent 85f2a83 commit 3958409

File tree

5 files changed

+26
-27
lines changed

5 files changed

+26
-27
lines changed

gcc/rust/checks/lints/rust-lint-scan-deadcode.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ class ScanDeadcode : public MarkLiveBase
133133
item->accept_vis (*this);
134134
}
135135

136+
void visit (HIR::ConstantItem &item) override
137+
{
138+
std::string var_name = item.get_identifier ().as_string ();
139+
bool starts_with_under_score = var_name.at (0) == '_';
140+
HirId hirId = item.get_mappings ().get_hirid ();
141+
if (should_warn (hirId) && !starts_with_under_score)
142+
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
143+
"deadcode const item %qs",
144+
item.get_identifier ().as_string ().c_str ());
145+
}
146+
147+
void visit (HIR::StaticItem &item) override
148+
{
149+
std::string var_name = item.get_identifier ().as_string ();
150+
bool starts_with_under_score = var_name.at (0) == '_';
151+
HirId hirId = item.get_mappings ().get_hirid ();
152+
if (should_warn (hirId) && !starts_with_under_score)
153+
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
154+
"deadcode static item %qs",
155+
item.get_identifier ().as_string ().c_str ());
156+
}
157+
136158
private:
137159
std::set<HirId> live_symbols;
138160
Resolver::Resolver *resolver;

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,13 @@ UnusedChecker::go (HIR::Crate &crate)
3737
for (auto &item : crate.get_items ())
3838
item->accept_vis (*this);
3939
}
40-
void
41-
UnusedChecker::visit (HIR::ConstantItem &item)
42-
{
43-
std::string var_name = item.get_identifier ().as_string ();
44-
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
45-
auto id = item.get_mappings ().get_hirid ();
46-
if (!unused_context.is_variable_used (id) && !starts_with_under_score)
47-
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
48-
"unused variable %qs",
49-
item.get_identifier ().as_string ().c_str ());
50-
}
51-
52-
void
53-
UnusedChecker::visit (HIR::StaticItem &item)
54-
{
55-
std::string var_name = item.get_identifier ().as_string ();
56-
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
57-
auto id = item.get_mappings ().get_hirid ();
58-
if (!unused_context.is_variable_used (id) && !starts_with_under_score)
59-
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
60-
"unused variable %qs",
61-
item.get_identifier ().as_string ().c_str ());
62-
}
6340

6441
void
6542
UnusedChecker::visit (HIR::TraitItemFunc &item)
6643
{
6744
// TODO: check trait item functions if they are not derived.
6845
}
46+
6947
void
7048
UnusedChecker::visit (HIR::IdentifierPattern &pattern)
7149
{

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class UnusedChecker : public HIR::DefaultHIRVisitor
3838

3939
using HIR::DefaultHIRVisitor::visit;
4040
virtual void visit (HIR::TraitItemFunc &decl) override;
41-
virtual void visit (HIR::ConstantItem &item) override;
42-
virtual void visit (HIR::StaticItem &item) override;
4341
virtual void visit (HIR::IdentifierPattern &identifier) override;
4442
virtual void visit (HIR::AssignmentExpr &identifier) override;
4543
virtual void visit (HIR::StructPatternFieldIdent &identifier) override;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const TEST: usize = 1;
2+
// { dg-warning "deadcode const .TEST." "" { target *-*-* } .-1 }
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
// { dg-additional-options "-frust-unused-check-2.0" }
21
static TEST: usize = 1;
3-
// { dg-warning "unused variable .TEST." "" { target *-*-* } .-1 }
2+
// { dg-warning "deadcode static item .TEST." "" { target *-*-* } .-1 }

0 commit comments

Comments
 (0)