Skip to content

Commit 475be10

Browse files
committed
in which unused-parens suggestions heed what the user actually wrote
Aaron Hill pointed out that unnecessary parens around a macro call (paradigmatically, `format!`) yielded a suggestion of hideous macro-expanded code. (The slightly unusual choice of using the pretty-printer to compose suggestions was quite recently commented on in the commit message for 1081bbb ("abolish ICE when pretty-printing async block"), but without any grounds to condemn it as a 𝘣𝘢𝘥 choice. Hill's report provides the grounds.) `span_to_snippet` is fallable as far as the type system is concerned (because, who knows, macros or something), so the pretty-printing can live on in the oft-neglected `else` branch. Resolves rust-lang#55109.
1 parent 01ca85b commit 475be10

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/librustc_lint/unused.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,13 @@ impl UnusedParens {
281281
let necessary = struct_lit_needs_parens &&
282282
parser::contains_exterior_struct_lit(&inner);
283283
if !necessary {
284-
let pattern = pprust::expr_to_string(value);
285-
Self::remove_outer_parens(cx, value.span, &pattern, msg);
284+
let expr_text = if let Ok(snippet) = cx.sess().source_map()
285+
.span_to_snippet(value.span) {
286+
snippet
287+
} else {
288+
pprust::expr_to_string(value)
289+
};
290+
Self::remove_outer_parens(cx, value.span, &expr_text, msg);
286291
}
287292
}
288293
}
@@ -292,8 +297,13 @@ impl UnusedParens {
292297
value: &ast::Pat,
293298
msg: &str) {
294299
if let ast::PatKind::Paren(_) = value.node {
295-
let pattern = pprust::pat_to_string(value);
296-
Self::remove_outer_parens(cx, value.span, &pattern, msg);
300+
let pattern_text = if let Ok(snippet) = cx.sess().source_map()
301+
.span_to_snippet(value.span) {
302+
snippet
303+
} else {
304+
pprust::pat_to_string(value)
305+
};
306+
Self::remove_outer_parens(cx, value.span, &pattern_text, msg);
297307
}
298308
}
299309

src/test/ui/lint/suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn main() {
5656
while true {
5757
//~^ WARN denote infinite loops
5858
//~| HELP use `loop`
59-
let mut a = (1);
59+
let mut registry_no = (format!("NX-{}", 74205));
6060
//~^ WARN does not need to be mutable
6161
//~| HELP remove this `mut`
6262
//~| WARN unnecessary parentheses
@@ -72,6 +72,6 @@ fn main() {
7272
//~^ WARN this pattern is redundant
7373
//~| HELP remove this
7474
}
75-
println!("{} {}", a, b);
75+
println!("{} {}", registry_no, b);
7676
}
7777
}

src/test/ui/lint/suggestions.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: unnecessary parentheses around assigned value
2-
--> $DIR/suggestions.rs:59:21
2+
--> $DIR/suggestions.rs:59:31
33
|
4-
LL | let mut a = (1);
5-
| ^^^ help: remove these parentheses
4+
LL | let mut registry_no = (format!("NX-{}", 74205));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
66
|
77
note: lint level defined here
88
--> $DIR/suggestions.rs:13:21
@@ -21,8 +21,8 @@ LL | #[no_debug] // should suggest removal of deprecated attribute
2121
warning: variable does not need to be mutable
2222
--> $DIR/suggestions.rs:59:13
2323
|
24-
LL | let mut a = (1);
25-
| ----^
24+
LL | let mut registry_no = (format!("NX-{}", 74205));
25+
| ----^^^^^^^^^^^
2626
| |
2727
| help: remove this `mut`
2828
|

0 commit comments

Comments
 (0)