[COMMITTED 51/77] gccrs: add unused braces lint
Arthur Cohen
arthur.cohen@embecosm.com
Fri Aug 7 12:43:26 GMT 2026
Note that upstream the commit changelog is correct and the mention of
`rust-lang.cc` has been removed.
Thanks,
Arthur
On 8/7/26 2:39 PM, arthur.cohen@embecosm.com wrote:
> From: Lucas Ly Ba <lucas.ly-ba@outlook.com>
>
> gcc/rust/ChangeLog:
>
> * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit):
> Warn on unnecessary braces around an assigned value.
> * rust-lang.cc (grs_langhook_init_options_struct): Enable warn_unused.
>
> gcc/testsuite/ChangeLog:
>
> * rust/compile/unused-braces_0.rs: New test.
>
> Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
> ---
> gcc/rust/checks/lints/unused/rust-unused-checker.cc | 9 +++++++++
> gcc/testsuite/rust/compile/unused-braces_0.rs | 8 ++++++++
> 2 files changed, 17 insertions(+)
> create mode 100644 gcc/testsuite/rust/compile/unused-braces_0.rs
>
> diff --git a/gcc/rust/checks/lints/unused/rust-unused-checker.cc b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
> index 14e2df8dac8..2c521634d8b 100644
> --- a/gcc/rust/checks/lints/unused/rust-unused-checker.cc
> +++ b/gcc/rust/checks/lints/unused/rust-unused-checker.cc
> @@ -330,6 +330,15 @@ UnusedChecker::visit (HIR::LetStmt &stmt)
> "unused doc comment");
> break;
> }
> + if (stmt.has_init_expr ()
> + && stmt.get_init_expr ().get_expression_type ()
> + == HIR::Expr::ExprType::Block)
> + {
> + auto &block = static_cast<HIR::BlockExpr &> (stmt.get_init_expr ());
> + if (block.get_statements ().empty () && block.has_expr ())
> + rust_warning_at (block.get_locus (), OPT_Wunused,
> + "unnecessary braces around assigned value");
> + }
> walk (stmt);
> }
>
> diff --git a/gcc/testsuite/rust/compile/unused-braces_0.rs b/gcc/testsuite/rust/compile/unused-braces_0.rs
> new file mode 100644
> index 00000000000..d1cd3bbdf95
> --- /dev/null
> +++ b/gcc/testsuite/rust/compile/unused-braces_0.rs
> @@ -0,0 +1,8 @@
> +// { dg-additional-options "-frust-unused-check-2.0" }
> +#![feature(no_core)]
> +#![no_core]
> +
> +pub fn foo() {
> + let _x = { 5 };
> +// { dg-warning "unnecessary braces around assigned value" "" { target *-*-* } .-1 }
> +}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0x1B3465B044AD9C65.asc
Type: application/pgp-keys
Size: 3143 bytes
Desc: OpenPGP public key
URL: <https://gcc.gnu.org/pipermail/gcc-rust/attachments/20260807/601147ff/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc-rust/attachments/20260807/601147ff/attachment.sig>
More information about the Gcc-rust
mailing list