correct unused-parens lint suggestion to strip exact pair
This commit is contained in:
parent
e3b498971d
commit
8a14022c5d
@ -334,8 +334,32 @@ impl UnusedParens {
|
||||
let mut err = cx.struct_span_lint(UNUSED_PARENS,
|
||||
value.span,
|
||||
&span_msg);
|
||||
// Remove exactly one pair of parentheses (rather than naïvely
|
||||
// stripping all paren characters)
|
||||
let mut ate_left_paren = false;
|
||||
let mut ate_right_paren = false;
|
||||
let parens_removed = pprust::expr_to_string(value)
|
||||
.trim_matches(|c| c == '(' || c == ')').to_owned();
|
||||
.trim_matches(|c| {
|
||||
match c {
|
||||
'(' => {
|
||||
if ate_left_paren {
|
||||
false
|
||||
} else {
|
||||
ate_left_paren = true;
|
||||
true
|
||||
}
|
||||
},
|
||||
')' => {
|
||||
if ate_right_paren {
|
||||
false
|
||||
} else {
|
||||
ate_right_paren = true;
|
||||
true
|
||||
}
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}).to_owned();
|
||||
err.span_suggestion_short(value.span,
|
||||
"remove these parentheses",
|
||||
parens_removed);
|
||||
|
25
src/test/ui/lint/unused_parens_json_suggestion.rs
Normal file
25
src/test/ui/lint/unused_parens_json_suggestion.rs
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// compile-flags: --error-format json
|
||||
|
||||
// ignore-windows (see Issue #44968)
|
||||
|
||||
// The output for humans should just highlight the whole span without showing
|
||||
// the suggested replacement, but we also want to test that suggested
|
||||
// replacement only removes one set of parentheses, rather than naïvely
|
||||
// stripping away any starting or ending parenthesis characters—hence this
|
||||
// test of the JSON error format.
|
||||
|
||||
fn main() {
|
||||
// We want to suggest the properly-balanced expression `1 / (2 + 3)`, not
|
||||
// the malformed `1 / (2 + 3`
|
||||
let _a = (1 / (2 + 3));
|
||||
}
|
1
src/test/ui/lint/unused_parens_json_suggestion.stderr
Normal file
1
src/test/ui/lint/unused_parens_json_suggestion.stderr
Normal file
@ -0,0 +1 @@
|
||||
{"message":"unnecessary parentheses around assigned value","code":null,"level":"warning","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1014,"byte_end":1027,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"#[warn(unused_parens)] on by default","code":null,"level":"note","spans":[],"children":[],"rendered":null},{"message":"remove these parentheses","code":null,"level":"help","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1014,"byte_end":1027,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":"1 / (2 + 3)","expansion":null}],"children":[],"rendered":" let _a = 1 / (2 + 3);"}],"rendered":null}
|
Loading…
Reference in New Issue
Block a user