Skip to content
Draft

4367 #4392

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion gcc/rust/checks/lints/rust-lint-scan-deadcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ namespace Rust {
namespace Analysis {

// Scan item symbols and warn the symbol if it is not in the live_symbols set.
// There are three kinds of item we should handle in this pass.
// There are four kinds of item we should handle in this pass.
// 1. Function item
// 2. The function item in the impl block without trait
// 3. StructStruct, e.g., `Struct Foo{one: 1, two: 2}`. Furthermore, the unused
// struct fields will be warned too.
// 4. TupleStruct, e.g., `Struct Foo(i32, i32)`
// 5. Enum, e.g., `enum Foo { A, B }`
class ScanDeadcode : public MarkLiveBase
{
using Rust::Analysis::MarkLiveBase::visit;
Expand Down Expand Up @@ -116,6 +117,19 @@ class ScanDeadcode : public MarkLiveBase
}
}

void visit (HIR::Enum &enm) override
{
HirId hirId = enm.get_mappings ().get_hirid ();
if (should_warn (hirId) && !enm.get_visibility ().is_public ())
{
bool name_starts_underscore
= enm.get_identifier ().as_string ().at (0) == '_';
if (!name_starts_underscore)
rust_warning_at (enm.get_locus (), 0, "enum is never used: %qs",
enm.get_identifier ().as_string ().c_str ());
}
}

void visit (HIR::ImplBlock &blc) override
{
if (blc.has_impl_items ())
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/issue-4367.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
enum Foo {
// { dg-warning "enum is never used" "" { target *-*-* } .-1 }
A,
B,
}

enum _Bar {
A,
B,
}
Loading