From 8a14022c5d31e1648bd1212a52a9f1a9ddbf3fa1 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Sat, 30 Sep 2017 00:53:26 -0700 Subject: [PATCH] correct unused-parens lint suggestion to strip exact pair --- src/librustc_lint/unused.rs | 26 ++++++++++++++++++- .../ui/lint/unused_parens_json_suggestion.rs | 25 ++++++++++++++++++ .../lint/unused_parens_json_suggestion.stderr | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/lint/unused_parens_json_suggestion.rs create mode 100644 src/test/ui/lint/unused_parens_json_suggestion.stderr diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 887245b15d357..e2ade19b6e285 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -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); diff --git a/src/test/ui/lint/unused_parens_json_suggestion.rs b/src/test/ui/lint/unused_parens_json_suggestion.rs new file mode 100644 index 0000000000000..d7cbd11472a64 --- /dev/null +++ b/src/test/ui/lint/unused_parens_json_suggestion.rs @@ -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 or the MIT license +// , 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)); +} diff --git a/src/test/ui/lint/unused_parens_json_suggestion.stderr b/src/test/ui/lint/unused_parens_json_suggestion.stderr new file mode 100644 index 0000000000000..140224e081489 --- /dev/null +++ b/src/test/ui/lint/unused_parens_json_suggestion.stderr @@ -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}