Skip to content

Commit d04a02a

Browse files
committed
fix FP with MBEs
1 parent de7dbf2 commit d04a02a

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

clippy_lints/src/needless_parens_on_range_literals.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::{SpanRangeExt, snippet_with_applicability};
2+
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
33

44
use rustc_ast::{Expr, ExprKind};
55
use rustc_errors::Applicability;
@@ -39,7 +39,8 @@ declare_clippy_lint! {
3939
declare_lint_pass!(NeedlessParensOnRangeLiterals => [NEEDLESS_PARENS_ON_RANGE_LITERALS]);
4040

4141
fn check_for_parens(cx: &EarlyContext<'_>, e: &Expr, is_start: bool) {
42-
if let ExprKind::Paren(literal) = &e.kind
42+
if !e.span.from_expansion()
43+
&& let ExprKind::Paren(literal) = &e.kind
4344
&& let ExprKind::Lit(lit) = &literal.kind
4445
{
4546
if is_start
@@ -52,7 +53,7 @@ fn check_for_parens(cx: &EarlyContext<'_>, e: &Expr, is_start: bool) {
5253
}
5354

5455
let mut applicability = Applicability::MachineApplicable;
55-
let suggestion = snippet_with_applicability(cx, literal.span, "_", &mut applicability);
56+
let suggestion = snippet_with_context(cx, literal.span, e.span.ctxt(), "_", &mut applicability).0;
5657
span_lint_and_sugg(
5758
cx,
5859
NEEDLESS_PARENS_ON_RANGE_LITERALS,

tests/ui/needless_parens_on_range_literals.fixed

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,28 @@ fn main() {
1818
//~^ needless_parens_on_range_literals
1919
let _ = ..'z';
2020
//~^ needless_parens_on_range_literals
21+
22+
macro_rules! verbatim {
23+
($e:expr) => {
24+
$e
25+
};
26+
}
27+
macro_rules! add_paren {
28+
($e:expr) => {
29+
($e)
30+
};
31+
}
32+
33+
// lint: the paren was added by the user
34+
let _ = verbatim!(0)..1;
35+
//~^ needless_parens_on_range_literals
36+
let _ = 0..verbatim!(1);
37+
//~^ needless_parens_on_range_literals
38+
39+
// lint: the macro doesn't have anything to do with the paren
40+
let _ = 0..1;
41+
//~^ needless_parens_on_range_literals
42+
43+
// don't lint: the paren was added by the macro
44+
let _ = add_paren!(0)..1;
2145
}

tests/ui/needless_parens_on_range_literals.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,28 @@ fn main() {
1818
//~^ needless_parens_on_range_literals
1919
let _ = ..('z');
2020
//~^ needless_parens_on_range_literals
21+
22+
macro_rules! verbatim {
23+
($e:expr) => {
24+
$e
25+
};
26+
}
27+
macro_rules! add_paren {
28+
($e:expr) => {
29+
($e)
30+
};
31+
}
32+
33+
// lint: the paren was added by the user
34+
let _ = verbatim!((0))..1;
35+
//~^ needless_parens_on_range_literals
36+
let _ = 0..verbatim!((1));
37+
//~^ needless_parens_on_range_literals
38+
39+
// lint: the macro doesn't have anything to do with the paren
40+
let _ = (verbatim!(0))..1;
41+
//~^ needless_parens_on_range_literals
42+
43+
// don't lint: the paren was added by the macro
44+
let _ = add_paren!(0)..1;
2145
}

tests/ui/needless_parens_on_range_literals.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@ error: needless parenthesis on range literals can be removed
4343
LL | let _ = ..('z');
4444
| ^^^^^ help: try: `'z'`
4545

46-
error: aborting due to 7 previous errors
46+
error: needless parenthesis on range literals can be removed
47+
--> tests/ui/needless_parens_on_range_literals.rs:34:23
48+
|
49+
LL | let _ = verbatim!((0))..1;
50+
| ^^^ help: try: `0`
51+
52+
error: needless parenthesis on range literals can be removed
53+
--> tests/ui/needless_parens_on_range_literals.rs:36:26
54+
|
55+
LL | let _ = 0..verbatim!((1));
56+
| ^^^ help: try: `1`
57+
58+
error: needless parenthesis on range literals can be removed
59+
--> tests/ui/needless_parens_on_range_literals.rs:40:13
60+
|
61+
LL | let _ = (verbatim!(0))..1;
62+
| ^^^^^^^^^^^^^^ help: try: `0`
63+
64+
error: aborting due to 10 previous errors
4765

0 commit comments

Comments
 (0)