Skip to content

Commit 8a14022

Browse files
committed
correct unused-parens lint suggestion to strip exact pair
1 parent e3b4989 commit 8a14022

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/librustc_lint/unused.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,32 @@ impl UnusedParens {
334334
let mut err = cx.struct_span_lint(UNUSED_PARENS,
335335
value.span,
336336
&span_msg);
337+
// Remove exactly one pair of parentheses (rather than naïvely
338+
// stripping all paren characters)
339+
let mut ate_left_paren = false;
340+
let mut ate_right_paren = false;
337341
let parens_removed = pprust::expr_to_string(value)
338-
.trim_matches(|c| c == '(' || c == ')').to_owned();
342+
.trim_matches(|c| {
343+
match c {
344+
'(' => {
345+
if ate_left_paren {
346+
false
347+
} else {
348+
ate_left_paren = true;
349+
true
350+
}
351+
},
352+
')' => {
353+
if ate_right_paren {
354+
false
355+
} else {
356+
ate_right_paren = true;
357+
true
358+
}
359+
},
360+
_ => false,
361+
}
362+
}).to_owned();
339363
err.span_suggestion_short(value.span,
340364
"remove these parentheses",
341365
parens_removed);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --error-format json
12+
13+
// ignore-windows (see Issue #44968)
14+
15+
// The output for humans should just highlight the whole span without showing
16+
// the suggested replacement, but we also want to test that suggested
17+
// replacement only removes one set of parentheses, rather than naïvely
18+
// stripping away any starting or ending parenthesis characters—hence this
19+
// test of the JSON error format.
20+
21+
fn main() {
22+
// We want to suggest the properly-balanced expression `1 / (2 + 3)`, not
23+
// the malformed `1 / (2 + 3`
24+
let _a = (1 / (2 + 3));
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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}

0 commit comments

Comments
 (0)