Skip to content

Commit bbc66fc

Browse files
committed
extend unused_parens lint to break/return ()
1 parent 1002e40 commit bbc66fc

File tree

4 files changed

+232
-1
lines changed

4 files changed

+232
-1
lines changed

src/librustc_lint/unused.rs

+29
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ impl EarlyLintPass for UnusedParens {
332332
WhileLet(_, ref cond, ..) => (cond, "`while let` head expression", true),
333333
ForLoop(_, ref cond, ..) => (cond, "`for` head expression", true),
334334
Match(ref head, _) => (head, "`match` head expression", true),
335+
Break(_, Some(ref value)) => (value, "`break` value", false),
335336
Ret(Some(ref value)) => (value, "`return` value", false),
336337
Assign(_, ref value) => (value, "assigned value", false),
337338
AssignOp(.., ref value) => (value, "assigned value", false),
@@ -368,6 +369,34 @@ impl EarlyLintPass for UnusedParens {
368369
return;
369370
}
370371
};
372+
match e.node {
373+
Ret(Some(ref value)) |
374+
Break(_, Some(ref value)) => {
375+
if let Tup(ref vals) = value.node {
376+
if vals.is_empty() {
377+
if e.span.ctxt().outer().expn_info()
378+
.map_or(false, |info| info.call_site.ctxt().outer()
379+
.expn_info().is_some()) {
380+
return;
381+
}
382+
let mut err = cx.struct_span_lint(
383+
UNUSED_PARENS,
384+
value.span,
385+
"unnecessary parentheses",
386+
);
387+
err.span_suggestion_short_with_applicability(
388+
value.span,
389+
"remove these parentheses",
390+
String::from(""),
391+
Applicability::MachineApplicable,
392+
);
393+
err.emit();
394+
return;
395+
}
396+
}
397+
}
398+
_ => ()
399+
}
371400
self.check_unused_parens_core(cx, &value, msg, struct_lit_needs_parens);
372401
}
373402

src/librustc_typeck/check/regionck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ use rustc::hir::{self, PatKind};
107107
macro_rules! ignore_err {
108108
($e:expr) => (match $e { Ok(e) => e, Err(_) => {
109109
debug!("ignoring mem-categorization error!");
110-
return ()
110+
return;
111111
}})
112112
}
113113

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 pretty-json -Zunstable-options
12+
// compile-pass
13+
14+
// The output for humans should just highlight the whole span without showing
15+
// the suggested replacement, but we also want to test that suggested
16+
// replacement only removes one set of parentheses, rather than naïvely
17+
// stripping away any starting or ending parenthesis characters—hence this
18+
// test of the JSON error format.
19+
20+
#![warn(unused_parens)]
21+
fn main() {
22+
loop {
23+
break();
24+
}
25+
return();
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"message": "unnecessary parentheses",
3+
"code": {
4+
"code": "unused_parens",
5+
"explanation": null
6+
},
7+
"level": "warning",
8+
"spans": [
9+
{
10+
"file_name": "$DIR/unused_parens_break_return.rs",
11+
"byte_start": 941,
12+
"byte_end": 943,
13+
"line_start": 23,
14+
"line_end": 23,
15+
"column_start": 14,
16+
"column_end": 16,
17+
"is_primary": true,
18+
"text": [
19+
{
20+
"text": " break();",
21+
"highlight_start": 14,
22+
"highlight_end": 16
23+
}
24+
],
25+
"label": null,
26+
"suggested_replacement": null,
27+
"suggestion_applicability": null,
28+
"expansion": null
29+
}
30+
],
31+
"children": [
32+
{
33+
"message": "lint level defined here",
34+
"code": null,
35+
"level": "note",
36+
"spans": [
37+
{
38+
"file_name": "$DIR/unused_parens_break_return.rs",
39+
"byte_start": 889,
40+
"byte_end": 902,
41+
"line_start": 20,
42+
"line_end": 20,
43+
"column_start": 9,
44+
"column_end": 22,
45+
"is_primary": true,
46+
"text": [
47+
{
48+
"text": "#![warn(unused_parens)]",
49+
"highlight_start": 9,
50+
"highlight_end": 22
51+
}
52+
],
53+
"label": null,
54+
"suggested_replacement": null,
55+
"suggestion_applicability": null,
56+
"expansion": null
57+
}
58+
],
59+
"children": [],
60+
"rendered": null
61+
},
62+
{
63+
"message": "remove these parentheses",
64+
"code": null,
65+
"level": "help",
66+
"spans": [
67+
{
68+
"file_name": "$DIR/unused_parens_break_return.rs",
69+
"byte_start": 941,
70+
"byte_end": 943,
71+
"line_start": 23,
72+
"line_end": 23,
73+
"column_start": 14,
74+
"column_end": 16,
75+
"is_primary": true,
76+
"text": [
77+
{
78+
"text": " break();",
79+
"highlight_start": 14,
80+
"highlight_end": 16
81+
}
82+
],
83+
"label": null,
84+
"suggested_replacement": "",
85+
"suggestion_applicability": "MachineApplicable",
86+
"expansion": null
87+
}
88+
],
89+
"children": [],
90+
"rendered": null
91+
}
92+
],
93+
"rendered": "warning: unnecessary parentheses
94+
--> $DIR/unused_parens_break_return.rs:23:14
95+
|
96+
LL | break();
97+
| ^^ help: remove these parentheses
98+
|
99+
note: lint level defined here
100+
--> $DIR/unused_parens_break_return.rs:20:9
101+
|
102+
LL | #![warn(unused_parens)]
103+
| ^^^^^^^^^^^^^
104+
105+
"
106+
}
107+
{
108+
"message": "unnecessary parentheses",
109+
"code": {
110+
"code": "unused_parens",
111+
"explanation": null
112+
},
113+
"level": "warning",
114+
"spans": [
115+
{
116+
"file_name": "$DIR/unused_parens_break_return.rs",
117+
"byte_start": 961,
118+
"byte_end": 963,
119+
"line_start": 25,
120+
"line_end": 25,
121+
"column_start": 11,
122+
"column_end": 13,
123+
"is_primary": true,
124+
"text": [
125+
{
126+
"text": " return();",
127+
"highlight_start": 11,
128+
"highlight_end": 13
129+
}
130+
],
131+
"label": null,
132+
"suggested_replacement": null,
133+
"suggestion_applicability": null,
134+
"expansion": null
135+
}
136+
],
137+
"children": [
138+
{
139+
"message": "remove these parentheses",
140+
"code": null,
141+
"level": "help",
142+
"spans": [
143+
{
144+
"file_name": "$DIR/unused_parens_break_return.rs",
145+
"byte_start": 961,
146+
"byte_end": 963,
147+
"line_start": 25,
148+
"line_end": 25,
149+
"column_start": 11,
150+
"column_end": 13,
151+
"is_primary": true,
152+
"text": [
153+
{
154+
"text": " return();",
155+
"highlight_start": 11,
156+
"highlight_end": 13
157+
}
158+
],
159+
"label": null,
160+
"suggested_replacement": "",
161+
"suggestion_applicability": "MachineApplicable",
162+
"expansion": null
163+
}
164+
],
165+
"children": [],
166+
"rendered": null
167+
}
168+
],
169+
"rendered": "warning: unnecessary parentheses
170+
--> $DIR/unused_parens_break_return.rs:25:11
171+
|
172+
LL | return();
173+
| ^^ help: remove these parentheses
174+
175+
"
176+
}

0 commit comments

Comments
 (0)