Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in which unused-parens suggestions heed what the user actually wrote #55138

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,13 @@ impl UnusedParens {
let necessary = struct_lit_needs_parens &&
parser::contains_exterior_struct_lit(&inner);
if !necessary {
let pattern = pprust::expr_to_string(value);
Self::remove_outer_parens(cx, value.span, &pattern, msg);
let expr_text = if let Ok(snippet) = cx.sess().source_map()
.span_to_snippet(value.span) {
snippet
} else {
pprust::expr_to_string(value)
};
Self::remove_outer_parens(cx, value.span, &expr_text, msg);
}
}
}
Expand All @@ -292,8 +297,13 @@ impl UnusedParens {
value: &ast::Pat,
msg: &str) {
if let ast::PatKind::Paren(_) = value.node {
let pattern = pprust::pat_to_string(value);
Self::remove_outer_parens(cx, value.span, &pattern, msg);
let pattern_text = if let Ok(snippet) = cx.sess().source_map()
.span_to_snippet(value.span) {
snippet
} else {
pprust::pat_to_string(value)
};
Self::remove_outer_parens(cx, value.span, &pattern_text, msg);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/lint/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn main() {
while true {
//~^ WARN denote infinite loops
//~| HELP use `loop`
let mut a = (1);
let mut registry_no = (format!("NX-{}", 74205));
//~^ WARN does not need to be mutable
//~| HELP remove this `mut`
//~| WARN unnecessary parentheses
Expand All @@ -72,6 +72,6 @@ fn main() {
//~^ WARN this pattern is redundant
//~| HELP remove this
}
println!("{} {}", a, b);
println!("{} {}", registry_no, b);
}
}
10 changes: 5 additions & 5 deletions src/test/ui/lint/suggestions.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
warning: unnecessary parentheses around assigned value
--> $DIR/suggestions.rs:59:21
--> $DIR/suggestions.rs:59:31
|
LL | let mut a = (1);
| ^^^ help: remove these parentheses
LL | let mut registry_no = (format!("NX-{}", 74205));
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
|
note: lint level defined here
--> $DIR/suggestions.rs:13:21
Expand All @@ -21,8 +21,8 @@ LL | #[no_debug] // should suggest removal of deprecated attribute
warning: variable does not need to be mutable
--> $DIR/suggestions.rs:59:13
|
LL | let mut a = (1);
| ----^
LL | let mut registry_no = (format!("NX-{}", 74205));
| ----^^^^^^^^^^^
| |
| help: remove this `mut`
|
Expand Down