stop check paren if has different ctx

This commit is contained in:
bohan 2025-04-30 01:29:44 +08:00
parent efcbb94dcc
commit e9d2fefe0c
4 changed files with 139 additions and 0 deletions

View File

@ -942,6 +942,22 @@ trait UnusedDelimLint {
match s.kind { match s.kind {
StmtKind::Let(ref local) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => { StmtKind::Let(ref local) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
if let Some((init, els)) = local.kind.init_else_opt() { 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 { let ctx = match els {
None => UnusedDelimsCtx::AssignedValue, None => UnusedDelimsCtx::AssignedValue,
Some(_) => UnusedDelimsCtx::AssignedValueLetElse, Some(_) => UnusedDelimsCtx::AssignedValueLetElse,

View File

@ -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
}

View File

@ -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
}

View File

@ -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