Skip to content

Commit 5af0f50

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 76397e8 commit 5af0f50

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
@@ -347,5 +347,24 @@ UnusedChecker::visit (HIR::LetStmt &stmt)
347347
walk (stmt);
348348
}
349349

350+
void
351+
UnusedChecker::visit (HIR::TypeAlias &type_alias)
352+
{
353+
bool has_bounds = type_alias.has_where_clause ();
354+
for (auto &param : type_alias.get_generic_params ())
355+
if (param->get_kind () == HIR::GenericParam::GenericKind::TYPE)
356+
{
357+
auto &type_param = static_cast<HIR::TypeParam &> (*param);
358+
if (type_param.has_type_param_bounds ())
359+
has_bounds = true;
360+
}
361+
362+
if (has_bounds)
363+
rust_warning_at (type_alias.get_locus (), OPT_Wunused_variable,
364+
"bounds on generic parameters in type aliases are not "
365+
"enforced");
366+
walk (type_alias);
367+
}
368+
350369
} // namespace Analysis
351370
} // 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
@@ -52,6 +52,7 @@ class UnusedChecker : public HIR::DefaultHIRVisitor
5252
virtual void visit (HIR::MatchExpr &expr) override;
5353
virtual void visit (HIR::ExternBlock &block) override;
5454
virtual void visit (HIR::LetStmt &stmt) override;
55+
virtual void visit (HIR::TypeAlias &type_alias) override;
5556
virtual void visit_loop_label (HIR::LoopLabel &label) override;
5657
};
5758
} // 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)