Skip to content

Commit bca4279

Browse files
authored
Rollup merge of rust-lang#141550 - Urgau:unused_braces-attrs, r=chenyukang
Fix `unused_braces` lint suggestion when encountering attributes This PR fixes the `unused_braces` lint suggestion when encountering attributes by not removing them in the suggestion. Fixes rust-lang#141549
2 parents d5a26c5 + 4765fd6 commit bca4279

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::iter;
22

3-
use rustc_ast as ast;
43
use rustc_ast::util::{classify, parser};
5-
use rustc_ast::{ExprKind, StmtKind};
4+
use rustc_ast::{self as ast, ExprKind, HasAttrs as _, StmtKind};
65
use rustc_errors::{MultiSpan, pluralize};
76
use rustc_hir::def::{DefKind, Res};
87
use rustc_hir::def_id::DefId;
@@ -780,26 +779,30 @@ trait UnusedDelimLint {
780779
right_pos: Option<BytePos>,
781780
is_kw: bool,
782781
) {
783-
let spans = match value.kind {
784-
ast::ExprKind::Block(ref block, None) if let [stmt] = block.stmts.as_slice() => stmt
785-
.span
786-
.find_ancestor_inside(value.span)
787-
.map(|span| (value.span.with_hi(span.lo()), value.span.with_lo(span.hi()))),
782+
let span_with_attrs = match value.kind {
783+
ast::ExprKind::Block(ref block, None) if let [stmt] = block.stmts.as_slice() => {
784+
// For the statements with attributes, like `{ #[allow()] println!("Hello!") }`,
785+
// the span should contains the attributes, or the suggestion will remove them.
786+
if let Some(attr_lo) = stmt.attrs().iter().map(|attr| attr.span.lo()).min() {
787+
stmt.span.with_lo(attr_lo)
788+
} else {
789+
stmt.span
790+
}
791+
}
788792
ast::ExprKind::Paren(ref expr) => {
789793
// For the expr with attributes, like `let _ = (#[inline] || println!("Hello!"));`,
790794
// the span should contains the attributes, or the suggestion will remove them.
791-
let expr_span_with_attrs =
792-
if let Some(attr_lo) = expr.attrs.iter().map(|attr| attr.span.lo()).min() {
793-
expr.span.with_lo(attr_lo)
794-
} else {
795-
expr.span
796-
};
797-
expr_span_with_attrs.find_ancestor_inside(value.span).map(|expr_span| {
798-
(value.span.with_hi(expr_span.lo()), value.span.with_lo(expr_span.hi()))
799-
})
795+
if let Some(attr_lo) = expr.attrs.iter().map(|attr| attr.span.lo()).min() {
796+
expr.span.with_lo(attr_lo)
797+
} else {
798+
expr.span
799+
}
800800
}
801801
_ => return,
802802
};
803+
let spans = span_with_attrs
804+
.find_ancestor_inside(value.span)
805+
.map(|span| (value.span.with_hi(span.lo()), value.span.with_lo(span.hi())));
803806
let keep_space = (
804807
left_pos.is_some_and(|s| s >= value.span.lo()),
805808
right_pos.is_some_and(|s| s <= value.span.hi()),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
//@ run-rustfix
3+
4+
#![allow(dead_code)]
5+
#![warn(unused_braces)]
6+
7+
use std::cmp::Ordering;
8+
9+
#[rustfmt::skip]
10+
fn ptr_cmp<T: ?Sized>(p1: *const T, p2: *const T) -> Ordering {
11+
#[expect(ambiguous_wide_pointer_comparisons)] p1.cmp(&p2)
12+
//~^ WARN unnecessary braces around block return value
13+
}
14+
15+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
//@ run-rustfix
3+
4+
#![allow(dead_code)]
5+
#![warn(unused_braces)]
6+
7+
use std::cmp::Ordering;
8+
9+
#[rustfmt::skip]
10+
fn ptr_cmp<T: ?Sized>(p1: *const T, p2: *const T) -> Ordering {
11+
{ #[expect(ambiguous_wide_pointer_comparisons)] p1.cmp(&p2) }
12+
//~^ WARN unnecessary braces around block return value
13+
}
14+
15+
fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
warning: unnecessary braces around block return value
2+
--> $DIR/unused-braces-attrs-issue-141549.rs:11:5
3+
|
4+
LL | { #[expect(ambiguous_wide_pointer_comparisons)] p1.cmp(&p2) }
5+
| ^^ ^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-braces-attrs-issue-141549.rs:5:9
9+
|
10+
LL | #![warn(unused_braces)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these braces
13+
|
14+
LL - { #[expect(ambiguous_wide_pointer_comparisons)] p1.cmp(&p2) }
15+
LL + #[expect(ambiguous_wide_pointer_comparisons)] p1.cmp(&p2)
16+
|
17+
18+
warning: 1 warning emitted
19+

0 commit comments

Comments
 (0)