From 061610640c9032fd54ad922e4d88631f9ba6f532 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 15 Nov 2022 16:43:50 +0900 Subject: [PATCH 1/2] simplify `emit_unused_delims_expr` --- compiler/rustc_lint/src/unused.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 46706e49844..766194b15c8 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -517,16 +517,9 @@ trait UnusedDelimLint { right_pos: Option, ) { let spans = match value.kind { - ast::ExprKind::Block(ref block, None) if block.stmts.len() > 0 => { - let start = block.stmts[0].span; - let end = block.stmts[block.stmts.len() - 1].span; - if let Some(start) = start.find_ancestor_inside(value.span) - && let Some(end) = end.find_ancestor_inside(value.span) - { - Some(( - value.span.with_hi(start.lo()), - value.span.with_lo(end.hi()), - )) + ast::ExprKind::Block(ref block, None) if block.stmts.len() == 1 => { + if let Some(span) = block.stmts[0].span.find_ancestor_inside(value.span) { + Some((value.span.with_hi(span.lo()), value.span.with_lo(span.hi()))) } else { None } From 1cf4132a1610de4319a382aa447f9fe3da52a19c Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 15 Nov 2022 16:44:23 +0900 Subject: [PATCH 2/2] return when expr has errors add ui tests --- compiler/rustc_lint/src/unused.rs | 5 +++++ src/test/ui/lint/issue-104392.rs | 11 +++++++++++ src/test/ui/lint/issue-104392.stderr | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 src/test/ui/lint/issue-104392.rs create mode 100644 src/test/ui/lint/issue-104392.stderr diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 766194b15c8..33580c2e521 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -518,6 +518,11 @@ trait UnusedDelimLint { ) { let spans = match value.kind { ast::ExprKind::Block(ref block, None) if block.stmts.len() == 1 => { + if let StmtKind::Expr(expr) = &block.stmts[0].kind + && let ExprKind::Err = expr.kind + { + return + } if let Some(span) = block.stmts[0].span.find_ancestor_inside(value.span) { Some((value.span.with_hi(span.lo()), value.span.with_lo(span.hi()))) } else { diff --git a/src/test/ui/lint/issue-104392.rs b/src/test/ui/lint/issue-104392.rs new file mode 100644 index 00000000000..d5608edb46f --- /dev/null +++ b/src/test/ui/lint/issue-104392.rs @@ -0,0 +1,11 @@ +fn main() { + { unsafe 92 } //~ ERROR expected `{`, found `92` +} + +fn foo() { + { mod 92 } //~ ERROR expected identifier, found `92` +} + +fn bar() { + { trait 92 } //~ ERROR expected identifier, found `92` +} diff --git a/src/test/ui/lint/issue-104392.stderr b/src/test/ui/lint/issue-104392.stderr new file mode 100644 index 00000000000..8e466439ae6 --- /dev/null +++ b/src/test/ui/lint/issue-104392.stderr @@ -0,0 +1,27 @@ +error: expected `{`, found `92` + --> $DIR/issue-104392.rs:2:14 + | +LL | { unsafe 92 } + | ------ ^^ expected `{` + | | + | while parsing this `unsafe` expression + | +help: try placing this code inside a block + | +LL | { unsafe { 92 } } + | + + + +error: expected identifier, found `92` + --> $DIR/issue-104392.rs:6:11 + | +LL | { mod 92 } + | ^^ expected identifier + +error: expected identifier, found `92` + --> $DIR/issue-104392.rs:10:13 + | +LL | { trait 92 } + | ^^ expected identifier + +error: aborting due to 3 previous errors +