Skip to content

Commit 4da5fdc

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 4da5fdc

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef RUST_HIR_SCAN_DEADCODE
2020
#define RUST_HIR_SCAN_DEADCODE
2121

22+
#include "options.h"
2223
#include "rust-hir-full-decls.h"
2324
#include "rust-hir-map.h"
2425
#include "rust-lint-marklive.h"
@@ -133,6 +134,32 @@ class ScanDeadcode : public MarkLiveBase
133134
item->accept_vis (*this);
134135
}
135136

137+
void visit (HIR::ConstantItem &item) override
138+
{
139+
if (!flag_unused_check_2_0)
140+
return;
141+
std::string var_name = item.get_identifier ().as_string ();
142+
bool starts_with_under_score = var_name.at (0) == '_';
143+
HirId hirId = item.get_mappings ().get_hirid ();
144+
if (should_warn (hirId) && !starts_with_under_score)
145+
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
146+
"deadcode const item %qs",
147+
item.get_identifier ().as_string ().c_str ());
148+
}
149+
150+
void visit (HIR::StaticItem &item) override
151+
{
152+
if (!flag_unused_check_2_0)
153+
return;
154+
std::string var_name = item.get_identifier ().as_string ();
155+
bool starts_with_under_score = var_name.at (0) == '_';
156+
HirId hirId = item.get_mappings ().get_hirid ();
157+
if (should_warn (hirId) && !starts_with_under_score)
158+
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
159+
"deadcode static item %qs",
160+
item.get_identifier ().as_string ().c_str ());
161+
}
162+
136163
private:
137164
std::set<HirId> live_symbols;
138165
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 item .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)