Skip to content
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

Implement unused_parens for const and static items #68751

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,14 @@ impl EarlyLintPass for UnusedParens {
}
}
}

fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
use ast::ItemKind::*;

if let Const(.., ref expr) | Static(.., ref expr) = item.kind {
self.check_unused_parens_expr(cx, expr, "assigned value", false, None, None);
}
}
}

declare_lint! {
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/lint/lint-unnecessary-parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ macro_rules! baz {
}
}

const CONST_ITEM: usize = (10); //~ ERROR unnecessary parentheses around assigned value
static STATIC_ITEM: usize = (10); //~ ERROR unnecessary parentheses around assigned value

fn main() {
foo();
bar((true)); //~ ERROR unnecessary parentheses around function argument
Expand Down
36 changes: 24 additions & 12 deletions src/test/ui/lint/lint-unnecessary-parens.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,73 +34,85 @@ error: unnecessary parentheses around block return value
LL | (5)
| ^^^ help: remove these parentheses

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:41:27
|
LL | const CONST_ITEM: usize = (10);
| ^^^^ help: remove these parentheses

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:42:29
|
LL | static STATIC_ITEM: usize = (10);
| ^^^^ help: remove these parentheses

error: unnecessary parentheses around function argument
--> $DIR/lint-unnecessary-parens.rs:43:9
--> $DIR/lint-unnecessary-parens.rs:46:9
|
LL | bar((true));
| ^^^^^^ help: remove these parentheses

error: unnecessary parentheses around `if` condition
--> $DIR/lint-unnecessary-parens.rs:45:8
--> $DIR/lint-unnecessary-parens.rs:48:8
|
LL | if (true) {}
| ^^^^^^ help: remove these parentheses

error: unnecessary parentheses around `while` condition
--> $DIR/lint-unnecessary-parens.rs:46:11
--> $DIR/lint-unnecessary-parens.rs:49:11
|
LL | while (true) {}
| ^^^^^^ help: remove these parentheses

warning: denote infinite loops with `loop { ... }`
--> $DIR/lint-unnecessary-parens.rs:46:5
--> $DIR/lint-unnecessary-parens.rs:49:5
|
LL | while (true) {}
| ^^^^^^^^^^^^ help: use `loop`
|
= note: `#[warn(while_true)]` on by default

error: unnecessary parentheses around `match` head expression
--> $DIR/lint-unnecessary-parens.rs:48:11
--> $DIR/lint-unnecessary-parens.rs:51:11
|
LL | match (true) {
| ^^^^^^ help: remove these parentheses

error: unnecessary parentheses around `let` head expression
--> $DIR/lint-unnecessary-parens.rs:51:16
--> $DIR/lint-unnecessary-parens.rs:54:16
|
LL | if let 1 = (1) {}
| ^^^ help: remove these parentheses

error: unnecessary parentheses around `let` head expression
--> $DIR/lint-unnecessary-parens.rs:52:19
--> $DIR/lint-unnecessary-parens.rs:55:19
|
LL | while let 1 = (2) {}
| ^^^ help: remove these parentheses

error: unnecessary parentheses around method argument
--> $DIR/lint-unnecessary-parens.rs:66:24
--> $DIR/lint-unnecessary-parens.rs:69:24
|
LL | X { y: false }.foo((true));
| ^^^^^^ help: remove these parentheses

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:68:18
--> $DIR/lint-unnecessary-parens.rs:71:18
|
LL | let mut _a = (0);
| ^^^ help: remove these parentheses

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:69:10
--> $DIR/lint-unnecessary-parens.rs:72:10
|
LL | _a = (0);
| ^^^ help: remove these parentheses

error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:70:11
--> $DIR/lint-unnecessary-parens.rs:73:11
|
LL | _a += (1);
| ^^^ help: remove these parentheses

error: aborting due to 15 previous errors
error: aborting due to 17 previous errors

1 change: 1 addition & 0 deletions src/test/ui/super-fast-paren-parsing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-pass

#![allow(unused_parens)]
#![allow(non_upper_case_globals)]
#![allow(dead_code)]
// exec-env:RUST_MIN_STACK=16000000
Expand Down