https://gcc.gnu.org/g:7b891aa8e49af18e14a4e54ed40ddc359b1e8d27
commit r17-4115-g7b891aa8e49af18e14a4e54ed40ddc359b1e8d27 Author: Lucas Ly Ba <[email protected]> Date: Sat Jun 27 21:48:41 2026 +0200 gccrs: add break with label and loop lint Warn on a labeled `break` whose value is a loop expression, as it is easy to confuse with an unlabeled `break` of a labeled value. 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/break-with-label-and-loop_0.rs: New test. Signed-off-by: Lucas Ly Ba <[email protected]> Diff: --- gcc/rust/checks/lints/unused/rust-unused-checker.cc | 13 +++++++++++++ gcc/rust/checks/lints/unused/rust-unused-checker.h | 1 + gcc/testsuite/rust/compile/break-with-label-and-loop_0.rs | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc b/gcc/rust/checks/lints/unused/rust-unused-checker.cc index c9c90633895b..023cba0c938a 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc +++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc @@ -400,5 +400,18 @@ UnusedChecker::visit (HIR::NegationExpr &expr) walk (expr); } +void +UnusedChecker::visit (HIR::BreakExpr &expr) +{ + if (expr.has_label () && expr.has_break_expr () + && expr.get_expr ().get_expression_type () + == HIR::Expr::ExprType::BaseLoop) + rust_warning_at ( + expr.get_locus (), OPT_Wunused, + "this labeled %<break%> expression is easy to confuse with " + "an unlabeled %<break%> with a labeled value expression"); + walk (expr); +} + } // namespace Analysis } // namespace Rust diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.h b/gcc/rust/checks/lints/unused/rust-unused-checker.h index 659087a5776a..f5a31b3df16b 100644 --- a/gcc/rust/checks/lints/unused/rust-unused-checker.h +++ b/gcc/rust/checks/lints/unused/rust-unused-checker.h @@ -54,6 +54,7 @@ private: virtual void visit (HIR::LetStmt &stmt) override; virtual void visit (HIR::BorrowExpr &expr) override; virtual void visit (HIR::NegationExpr &expr) override; + virtual void visit (HIR::BreakExpr &expr) override; virtual void visit_loop_label (HIR::LoopLabel &label) override; }; } // namespace Analysis diff --git a/gcc/testsuite/rust/compile/break-with-label-and-loop_0.rs b/gcc/testsuite/rust/compile/break-with-label-and-loop_0.rs new file mode 100644 index 000000000000..fef2e5231def --- /dev/null +++ b/gcc/testsuite/rust/compile/break-with-label-and-loop_0.rs @@ -0,0 +1,10 @@ +// { dg-additional-options "-frust-unused-check-2.0" } +#![feature(no_core)] +#![no_core] + +pub fn foo() { + 'a: loop { + break 'a loop {}; +// { dg-warning "easy to confuse" "" { target *-*-* } .-1 } + } +}
