From 33d3c27d38df5af198ecb9ace7546cab37818dc5 Mon Sep 17 00:00:00 2001 From: surechen Date: Tue, 12 Nov 2024 10:17:41 +0800 Subject: [PATCH] For expr `return (_ = 42);` unused_paren lint should not be triggered fixes #131989 --- compiler/rustc_lint/src/unused.rs | 2 ++ .../unused-parens-assign-expr-in-ret-issue-131989.rs | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/ui/lint/unused/unused-parens-assign-expr-in-ret-issue-131989.rs diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index b50a95e7d2b..52126afcf5a 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -1063,6 +1063,8 @@ impl UnusedDelimLint for UnusedParens { _, _, ) if node.is_lazy())) + && !(ctx == UnusedDelimsCtx::ReturnValue + && matches!(inner.kind, ast::ExprKind::Assign(_, _, _))) { self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw) } diff --git a/tests/ui/lint/unused/unused-parens-assign-expr-in-ret-issue-131989.rs b/tests/ui/lint/unused/unused-parens-assign-expr-in-ret-issue-131989.rs new file mode 100644 index 00000000000..56973f128e1 --- /dev/null +++ b/tests/ui/lint/unused/unused-parens-assign-expr-in-ret-issue-131989.rs @@ -0,0 +1,9 @@ +//@ check-pass +#![warn(unused_parens)] + +fn foo() { + return (_ = 42); + // lint unused_parens should not be triggered here. +} + +fn main() {}