Skip to content

extend unused_parens lint to break/return () #54440

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

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 29 additions & 0 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
@@ -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);
}

2 changes: 1 addition & 1 deletion src/librustc_typeck/check/regionck.rs
Original file line number Diff line number Diff line change
@@ -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;
}})
}

26 changes: 26 additions & 0 deletions src/test/ui/lint/unused_parens_break_return.rs
Original file line number Diff line number Diff line change
@@ -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 <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 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the kind of thing that // run-rustfix is for? Checking the full JSON feels fragile...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I didn't know about run-rustfix! I mainly wanted to check the applicability, so this should work nicely.


#![warn(unused_parens)]
fn main() {
loop {
break();
}
return();
}
176 changes: 176 additions & 0 deletions src/test/ui/lint/unused_parens_break_return.stderr
Original file line number Diff line number Diff line change
@@ -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

"
}