Skip to content

handle paren in macro expand for let-init-else expr #134034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,22 @@ trait UnusedDelimLint {
match s.kind {
StmtKind::Let(ref local) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
if let Some((init, els)) = local.kind.init_else_opt() {
if els.is_some()
&& let ExprKind::Paren(paren) = &init.kind
&& !init.span.eq_ctxt(paren.span)
{
// This branch prevents cases where parentheses wrap an expression
// resulting from macro expansion, such as:
// ```
// macro_rules! x {
// () => { None::<i32> };
// }
// let Some(_) = (x!{}) else { return };
// // -> let Some(_) = (None::<i32>) else { return };
// // ~ ~ No Lint
// ```
return;
}
let ctx = match els {
None => UnusedDelimsCtx::AssignedValue,
Some(_) => UnusedDelimsCtx::AssignedValueLetElse,
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/lint/unused-parens-for-macro-call-with-brace.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//@ run-rustfix

#![deny(unused_parens)]

fn main() {
macro_rules! x {
() => { None::<i32> };
}

let Some(_) = (x!{}) else { return }; // no error
let Some(_) = (x!{}) else { return };
//~^ ERROR: unnecessary parentheses around assigned value

let Some(_) = (x!{}) else { return };
//~^ ERROR: unnecessary parentheses around pattern

let _ = x!{};
let _ = x!{};
//~^ ERROR: unnecessary parentheses around assigned value

if let Some(_) = x!{} {};
if let Some(_) = x!{} {};
//~^ ERROR: unnecessary parentheses around `let` scrutinee expression

while let Some(_) = x!{} {};
while let Some(_) = x!{} {};
//~^ ERROR: unnecessary parentheses around `let` scrutinee expression
}
28 changes: 28 additions & 0 deletions tests/ui/lint/unused-parens-for-macro-call-with-brace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//@ run-rustfix

#![deny(unused_parens)]

fn main() {
macro_rules! x {
() => { None::<i32> };
}

let Some(_) = (x!{}) else { return }; // no error
let Some(_) = ((x!{})) else { return };
//~^ ERROR: unnecessary parentheses around assigned value

let Some((_)) = (x!{}) else { return };
//~^ ERROR: unnecessary parentheses around pattern

let _ = x!{};
let _ = (x!{});
//~^ ERROR: unnecessary parentheses around assigned value

if let Some(_) = x!{} {};
if let Some(_) = (x!{}) {};
//~^ ERROR: unnecessary parentheses around `let` scrutinee expression

while let Some(_) = x!{} {};
while let Some(_) = (x!{}) {};
//~^ ERROR: unnecessary parentheses around `let` scrutinee expression
}
67 changes: 67 additions & 0 deletions tests/ui/lint/unused-parens-for-macro-call-with-brace.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
error: unnecessary parentheses around assigned value
--> $DIR/unused-parens-for-macro-call-with-brace.rs:11:19
|
LL | let Some(_) = ((x!{})) else { return };
| ^ ^
|
note: the lint level is defined here
--> $DIR/unused-parens-for-macro-call-with-brace.rs:3:9
|
LL | #![deny(unused_parens)]
| ^^^^^^^^^^^^^
help: remove these parentheses
|
LL - let Some(_) = ((x!{})) else { return };
LL + let Some(_) = (x!{}) else { return };
|

error: unnecessary parentheses around pattern
--> $DIR/unused-parens-for-macro-call-with-brace.rs:14:14
|
LL | let Some((_)) = (x!{}) else { return };
| ^ ^
|
help: remove these parentheses
|
LL - let Some((_)) = (x!{}) else { return };
LL + let Some(_) = (x!{}) else { return };
|

error: unnecessary parentheses around assigned value
--> $DIR/unused-parens-for-macro-call-with-brace.rs:18:13
|
LL | let _ = (x!{});
| ^ ^
|
help: remove these parentheses
|
LL - let _ = (x!{});
LL + let _ = x!{};
|

error: unnecessary parentheses around `let` scrutinee expression
--> $DIR/unused-parens-for-macro-call-with-brace.rs:22:22
|
LL | if let Some(_) = (x!{}) {};
| ^ ^
|
help: remove these parentheses
|
LL - if let Some(_) = (x!{}) {};
LL + if let Some(_) = x!{} {};
|

error: unnecessary parentheses around `let` scrutinee expression
--> $DIR/unused-parens-for-macro-call-with-brace.rs:26:25
|
LL | while let Some(_) = (x!{}) {};
| ^ ^
|
help: remove these parentheses
|
LL - while let Some(_) = (x!{}) {};
LL + while let Some(_) = x!{} {};
|

error: aborting due to 5 previous errors

Loading