Skip to content

Commit 1557103

Browse files
committed
gccrs: add type alias bounds lint
Warn on bounds applied to the generic parameters of a type alias, as those bounds are not enforced. gcc/rust/ChangeLog: * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit): New. * checks/lints/unused/rust-unused-checker.h (UnusedChecker::visit): New. gcc/testsuite/ChangeLog: * rust/compile/type-alias-bounds_0.rs: New test. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
1 parent 206d052 commit 1557103

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,24 @@ UnusedChecker::visit (HIR::MatchExpr &expr)
323323
walk (expr);
324324
}
325325

326+
void
327+
UnusedChecker::visit (HIR::TypeAlias &type_alias)
328+
{
329+
bool has_bounds = type_alias.has_where_clause ();
330+
for (auto &param : type_alias.get_generic_params ())
331+
if (param->get_kind () == HIR::GenericParam::GenericKind::TYPE)
332+
{
333+
auto &type_param = static_cast<HIR::TypeParam &> (*param);
334+
if (type_param.has_type_param_bounds ())
335+
has_bounds = true;
336+
}
337+
338+
if (has_bounds)
339+
rust_warning_at (type_alias.get_locus (), OPT_Wunused_variable,
340+
"bounds on generic parameters in type aliases are not "
341+
"enforced");
342+
walk (type_alias);
343+
}
344+
326345
} // namespace Analysis
327346
} // namespace Rust

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class UnusedChecker : public HIR::DefaultHIRVisitor
5050
virtual void visit (HIR::LifetimeParam &lft) override;
5151
virtual void visit (HIR::StructPatternFieldIdentPat &field) override;
5252
virtual void visit (HIR::MatchExpr &expr) override;
53+
virtual void visit (HIR::TypeAlias &type_alias) override;
5354
virtual void visit_loop_label (HIR::LoopLabel &label) override;
5455
};
5556
} // namespace Analysis
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// { dg-additional-options "-frust-unused-check-2.0" }
2+
#![feature(no_core, lang_items)]
3+
#![no_core]
4+
5+
#[lang = "sized"]
6+
pub trait Sized {}
7+
8+
pub trait Foo {}
9+
10+
pub type Alias<T: Foo> = T;
11+
// { dg-warning "bounds on generic parameters in type aliases are not enforced" "" { target *-*-* } .-1 }

0 commit comments

Comments
 (0)