From bbc66fc03c1f87efa7e94eff23e9c0cf35931366 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Fri, 21 Sep 2018 18:31:24 +0200 Subject: [PATCH] extend unused_parens lint to `break`/`return ()` --- src/librustc_lint/unused.rs | 29 +++ src/librustc_typeck/check/regionck.rs | 2 +- .../ui/lint/unused_parens_break_return.rs | 26 +++ .../ui/lint/unused_parens_break_return.stderr | 176 ++++++++++++++++++ 4 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/lint/unused_parens_break_return.rs create mode 100644 src/test/ui/lint/unused_parens_break_return.stderr diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 7a9d18676cf6c..f546a95decd81 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -332,6 +332,7 @@ impl EarlyLintPass for UnusedParens { WhileLet(_, ref cond, ..) => (cond, "`while let` head expression", true), ForLoop(_, ref cond, ..) => (cond, "`for` head expression", true), Match(ref head, _) => (head, "`match` head expression", true), + Break(_, Some(ref value)) => (value, "`break` value", false), Ret(Some(ref value)) => (value, "`return` value", false), Assign(_, ref value) => (value, "assigned value", false), AssignOp(.., ref value) => (value, "assigned value", false), @@ -368,6 +369,34 @@ impl EarlyLintPass for UnusedParens { return; } }; + match e.node { + Ret(Some(ref value)) | + Break(_, Some(ref value)) => { + if let Tup(ref vals) = value.node { + if vals.is_empty() { + if e.span.ctxt().outer().expn_info() + .map_or(false, |info| info.call_site.ctxt().outer() + .expn_info().is_some()) { + return; + } + let mut err = cx.struct_span_lint( + UNUSED_PARENS, + value.span, + "unnecessary parentheses", + ); + err.span_suggestion_short_with_applicability( + value.span, + "remove these parentheses", + String::from(""), + Applicability::MachineApplicable, + ); + err.emit(); + return; + } + } + } + _ => () + } self.check_unused_parens_core(cx, &value, msg, struct_lit_needs_parens); } diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index fbf8afc3be234..843379b56733f 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -107,7 +107,7 @@ use rustc::hir::{self, PatKind}; macro_rules! ignore_err { ($e:expr) => (match $e { Ok(e) => e, Err(_) => { debug!("ignoring mem-categorization error!"); - return () + return; }}) } diff --git a/src/test/ui/lint/unused_parens_break_return.rs b/src/test/ui/lint/unused_parens_break_return.rs new file mode 100644 index 0000000000000..95d45e45ee1d6 --- /dev/null +++ b/src/test/ui/lint/unused_parens_break_return.rs @@ -0,0 +1,26 @@ +// 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 pretty-json -Zunstable-options +// compile-pass + +// 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. + +#![warn(unused_parens)] +fn main() { + loop { + break(); + } + return(); +} diff --git a/src/test/ui/lint/unused_parens_break_return.stderr b/src/test/ui/lint/unused_parens_break_return.stderr new file mode 100644 index 0000000000000..5a12700f95da5 --- /dev/null +++ b/src/test/ui/lint/unused_parens_break_return.stderr @@ -0,0 +1,176 @@ +{ + "message": "unnecessary parentheses", + "code": { + "code": "unused_parens", + "explanation": null + }, + "level": "warning", + "spans": [ + { + "file_name": "$DIR/unused_parens_break_return.rs", + "byte_start": 941, + "byte_end": 943, + "line_start": 23, + "line_end": 23, + "column_start": 14, + "column_end": 16, + "is_primary": true, + "text": [ + { + "text": " break();", + "highlight_start": 14, + "highlight_end": 16 + } + ], + "label": null, + "suggested_replacement": null, + "suggestion_applicability": null, + "expansion": null + } + ], + "children": [ + { + "message": "lint level defined here", + "code": null, + "level": "note", + "spans": [ + { + "file_name": "$DIR/unused_parens_break_return.rs", + "byte_start": 889, + "byte_end": 902, + "line_start": 20, + "line_end": 20, + "column_start": 9, + "column_end": 22, + "is_primary": true, + "text": [ + { + "text": "#![warn(unused_parens)]", + "highlight_start": 9, + "highlight_end": 22 + } + ], + "label": null, + "suggested_replacement": null, + "suggestion_applicability": null, + "expansion": null + } + ], + "children": [], + "rendered": null + }, + { + "message": "remove these parentheses", + "code": null, + "level": "help", + "spans": [ + { + "file_name": "$DIR/unused_parens_break_return.rs", + "byte_start": 941, + "byte_end": 943, + "line_start": 23, + "line_end": 23, + "column_start": 14, + "column_end": 16, + "is_primary": true, + "text": [ + { + "text": " break();", + "highlight_start": 14, + "highlight_end": 16 + } + ], + "label": null, + "suggested_replacement": "", + "suggestion_applicability": "MachineApplicable", + "expansion": null + } + ], + "children": [], + "rendered": null + } + ], + "rendered": "warning: unnecessary parentheses + --> $DIR/unused_parens_break_return.rs:23:14 + | +LL | break(); + | ^^ help: remove these parentheses + | +note: lint level defined here + --> $DIR/unused_parens_break_return.rs:20:9 + | +LL | #![warn(unused_parens)] + | ^^^^^^^^^^^^^ + +" +} +{ + "message": "unnecessary parentheses", + "code": { + "code": "unused_parens", + "explanation": null + }, + "level": "warning", + "spans": [ + { + "file_name": "$DIR/unused_parens_break_return.rs", + "byte_start": 961, + "byte_end": 963, + "line_start": 25, + "line_end": 25, + "column_start": 11, + "column_end": 13, + "is_primary": true, + "text": [ + { + "text": " return();", + "highlight_start": 11, + "highlight_end": 13 + } + ], + "label": null, + "suggested_replacement": null, + "suggestion_applicability": null, + "expansion": null + } + ], + "children": [ + { + "message": "remove these parentheses", + "code": null, + "level": "help", + "spans": [ + { + "file_name": "$DIR/unused_parens_break_return.rs", + "byte_start": 961, + "byte_end": 963, + "line_start": 25, + "line_end": 25, + "column_start": 11, + "column_end": 13, + "is_primary": true, + "text": [ + { + "text": " return();", + "highlight_start": 11, + "highlight_end": 13 + } + ], + "label": null, + "suggested_replacement": "", + "suggestion_applicability": "MachineApplicable", + "expansion": null + } + ], + "children": [], + "rendered": null + } + ], + "rendered": "warning: unnecessary parentheses + --> $DIR/unused_parens_break_return.rs:25:11 + | +LL | return(); + | ^^ help: remove these parentheses + +" +}