Skip to content

Commit 5648859

Browse files
Rollup merge of #88779 - estebank:unused-delims, r=davidtwco
Use more accurate spans for "unused delimiter" lint
2 parents 08cbb7d + dc02b51 commit 5648859

13 files changed

+551
-150
lines changed

compiler/rustc_lint/src/unused.rs

+52-68
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}
33
use rustc_ast as ast;
44
use rustc_ast::util::{classify, parser};
55
use rustc_ast::{ExprKind, StmtKind};
6-
use rustc_ast_pretty::pprust;
76
use rustc_errors::{pluralize, Applicability};
87
use rustc_hir as hir;
98
use rustc_hir::def::{DefKind, Res};
@@ -12,7 +11,7 @@ use rustc_middle::ty::adjustment;
1211
use rustc_middle::ty::{self, Ty};
1312
use rustc_span::symbol::Symbol;
1413
use rustc_span::symbol::{kw, sym};
15-
use rustc_span::{BytePos, Span, DUMMY_SP};
14+
use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP};
1615

1716
declare_lint! {
1817
/// The `unused_must_use` lint detects unused result of a type flagged as
@@ -491,77 +490,60 @@ trait UnusedDelimLint {
491490
left_pos: Option<BytePos>,
492491
right_pos: Option<BytePos>,
493492
) {
494-
let expr_text = if let Ok(snippet) = cx.sess().source_map().span_to_snippet(value.span) {
495-
snippet
496-
} else {
497-
pprust::expr_to_string(value)
493+
let spans = match value.kind {
494+
ast::ExprKind::Block(ref block, None) if block.stmts.len() > 0 => {
495+
let start = block.stmts[0].span;
496+
let end = block.stmts[block.stmts.len() - 1].span;
497+
if value.span.from_expansion() || start.from_expansion() || end.from_expansion() {
498+
(
499+
value.span.with_hi(value.span.lo() + BytePos(1)),
500+
value.span.with_lo(value.span.hi() - BytePos(1)),
501+
)
502+
} else {
503+
(value.span.with_hi(start.lo()), value.span.with_lo(end.hi()))
504+
}
505+
}
506+
ast::ExprKind::Paren(ref expr) => {
507+
if value.span.from_expansion() || expr.span.from_expansion() {
508+
(
509+
value.span.with_hi(value.span.lo() + BytePos(1)),
510+
value.span.with_lo(value.span.hi() - BytePos(1)),
511+
)
512+
} else {
513+
(value.span.with_hi(expr.span.lo()), value.span.with_lo(expr.span.hi()))
514+
}
515+
}
516+
_ => return,
498517
};
499518
let keep_space = (
500519
left_pos.map_or(false, |s| s >= value.span.lo()),
501520
right_pos.map_or(false, |s| s <= value.span.hi()),
502521
);
503-
self.emit_unused_delims(cx, value.span, &expr_text, ctx.into(), keep_space);
522+
self.emit_unused_delims(cx, spans, ctx.into(), keep_space);
504523
}
505524

506525
fn emit_unused_delims(
507526
&self,
508527
cx: &EarlyContext<'_>,
509-
span: Span,
510-
pattern: &str,
528+
spans: (Span, Span),
511529
msg: &str,
512530
keep_space: (bool, bool),
513531
) {
514532
// FIXME(flip1995): Quick and dirty fix for #70814. This should be fixed in rustdoc
515533
// properly.
516-
if span == DUMMY_SP {
534+
if spans.0 == DUMMY_SP || spans.1 == DUMMY_SP {
517535
return;
518536
}
519537

520-
cx.struct_span_lint(self.lint(), span, |lint| {
538+
cx.struct_span_lint(self.lint(), MultiSpan::from(vec![spans.0, spans.1]), |lint| {
521539
let span_msg = format!("unnecessary {} around {}", Self::DELIM_STR, msg);
522540
let mut err = lint.build(&span_msg);
523-
let mut ate_left_paren = false;
524-
let mut ate_right_paren = false;
525-
let parens_removed = pattern
526-
.trim_matches(|c| match c {
527-
'(' | '{' => {
528-
if ate_left_paren {
529-
false
530-
} else {
531-
ate_left_paren = true;
532-
true
533-
}
534-
}
535-
')' | '}' => {
536-
if ate_right_paren {
537-
false
538-
} else {
539-
ate_right_paren = true;
540-
true
541-
}
542-
}
543-
_ => false,
544-
})
545-
.trim();
546-
547-
let replace = {
548-
let mut replace = if keep_space.0 {
549-
let mut s = String::from(" ");
550-
s.push_str(parens_removed);
551-
s
552-
} else {
553-
String::from(parens_removed)
554-
};
555-
556-
if keep_space.1 {
557-
replace.push(' ');
558-
}
559-
replace
560-
};
561-
541+
let replacement = vec![
542+
(spans.0, if keep_space.0 { " ".into() } else { "".into() }),
543+
(spans.1, if keep_space.1 { " ".into() } else { "".into() }),
544+
];
562545
let suggestion = format!("remove these {}", Self::DELIM_STR);
563-
564-
err.span_suggestion_short(span, &suggestion, replace, Applicability::MachineApplicable);
546+
err.multipart_suggestion(&suggestion, replacement, Applicability::MachineApplicable);
565547
err.emit();
566548
});
567549
}
@@ -770,14 +752,15 @@ impl UnusedParens {
770752
// Otherwise proceed with linting.
771753
_ => {}
772754
}
773-
774-
let pattern_text =
775-
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(value.span) {
776-
snippet
777-
} else {
778-
pprust::pat_to_string(value)
779-
};
780-
self.emit_unused_delims(cx, value.span, &pattern_text, "pattern", (false, false));
755+
let spans = if value.span.from_expansion() || inner.span.from_expansion() {
756+
(
757+
value.span.with_hi(value.span.lo() + BytePos(1)),
758+
value.span.with_lo(value.span.hi() - BytePos(1)),
759+
)
760+
} else {
761+
(value.span.with_hi(inner.span.lo()), value.span.with_lo(inner.span.hi()))
762+
};
763+
self.emit_unused_delims(cx, spans, "pattern", (false, false));
781764
}
782765
}
783766
}
@@ -870,14 +853,15 @@ impl EarlyLintPass for UnusedParens {
870853
);
871854
}
872855
_ => {
873-
let pattern_text =
874-
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(ty.span) {
875-
snippet
876-
} else {
877-
pprust::ty_to_string(ty)
878-
};
879-
880-
self.emit_unused_delims(cx, ty.span, &pattern_text, "type", (false, false));
856+
let spans = if ty.span.from_expansion() || r.span.from_expansion() {
857+
(
858+
ty.span.with_hi(ty.span.lo() + BytePos(1)),
859+
ty.span.with_lo(ty.span.hi() - BytePos(1)),
860+
)
861+
} else {
862+
(ty.span.with_hi(r.span.lo()), ty.span.with_lo(r.span.hi()))
863+
};
864+
self.emit_unused_delims(cx, spans, "type", (false, false));
881865
}
882866
}
883867
}

src/test/ui/async-await/issues/issue-54752-async-block.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ warning: unnecessary parentheses around assigned value
22
--> $DIR/issue-54752-async-block.rs:6:22
33
|
44
LL | fn main() { let _a = (async { }); }
5-
| ^^^^^^^^^^^^ help: remove these parentheses
5+
| ^ ^
66
|
77
= note: `#[warn(unused_parens)]` on by default
8+
help: remove these parentheses
9+
|
10+
LL - fn main() { let _a = (async { }); }
11+
LL + fn main() { let _a = async { }; }
12+
|
813

914
warning: 1 warning emitted
1015

src/test/ui/const-generics/unused_braces.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ warning: unnecessary braces around const expression
22
--> $DIR/unused_braces.rs:9:14
33
|
44
LL | let _: A<{ 7 }>;
5-
| ^^^^^ help: remove these braces
5+
| ^^ ^^
66
|
77
note: the lint level is defined here
88
--> $DIR/unused_braces.rs:3:9
99
|
1010
LL | #![warn(unused_braces)]
1111
| ^^^^^^^^^^^^^
12+
help: remove these braces
13+
|
14+
LL - let _: A<{ 7 }>;
15+
LL + let _: A<7>;
16+
|
1217

1318
warning: 1 warning emitted
1419

0 commit comments

Comments
 (0)