|
1 | 1 | use std::iter; |
2 | 2 |
|
3 | | -use rustc_ast as ast; |
4 | 3 | use rustc_ast::util::{classify, parser}; |
5 | | -use rustc_ast::{ExprKind,StmtKind}; |
| 4 | +use rustc_ast::{selfas ast,ExprKind,HasAttrsas _,StmtKind}; |
6 | 5 | use rustc_errors::{MultiSpan, pluralize}; |
7 | 6 | use rustc_hir::def::{DefKind,Res}; |
8 | 7 | use rustc_hir::def_id::DefId; |
@@ -780,26 +779,30 @@ trait UnusedDelimLint { |
780 | 779 | right_pos:Option<BytePos>, |
781 | 780 | is_kw:bool, |
782 | 781 | ){ |
783 | | -let spans = match value.kind{ |
784 | | - ast::ExprKind::Block(ref block,None)iflet[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)iflet[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 | +ifletSome(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 | +} |
788 | 792 | ast::ExprKind::Paren(ref expr) => { |
789 | 793 | // For the expr with attributes, like `let _ = (#[inline] || println!("Hello!"));`, |
790 | 794 | // the span should contains the attributes, or the suggestion will remove them. |
791 | | -let expr_span_with_attrs = |
792 | | -ifletSome(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 | +ifletSome(attr_lo) = expr.attrs.iter().map(|attr| attr.span.lo()).min(){ |
| 796 | + expr.span.with_lo(attr_lo) |
| 797 | +}else{ |
| 798 | + expr.span |
| 799 | +} |
800 | 800 | } |
801 | 801 | _ => return, |
802 | 802 | }; |
| 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()))); |
803 | 806 | let keep_space = ( |
804 | 807 | left_pos.is_some_and(|s| s >= value.span.lo()), |
805 | 808 | right_pos.is_some_and(|s| s <= value.span.hi()), |
|
0 commit comments