From b7739f37db43b50121f8feee3bf2aa02890f9b79 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Sat, 23 Dec 2017 19:28:33 -0800 Subject: [PATCH] in which the unused-parens lint comes to cover function and method args Resolves #46137. --- src/liballoc/vec_deque.rs | 2 +- src/librustc_lint/unused.rs | 12 ++++++++++++ src/test/compile-fail/lint-unnecessary-parens.rs | 12 +++++++----- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/liballoc/vec_deque.rs b/src/liballoc/vec_deque.rs index f56aa23a4eb2f..1f6c6660d9bea 100644 --- a/src/liballoc/vec_deque.rs +++ b/src/liballoc/vec_deque.rs @@ -2480,7 +2480,7 @@ impl From> for Vec { if other.is_contiguous() { ptr::copy(buf.offset(tail as isize), buf, len); } else { - if (tail - head) >= cmp::min((cap - tail), head) { + if (tail - head) >= cmp::min(cap - tail, head) { // There is enough free space in the centre for the shortest block so we can // do this in at most three copy moves. if (cap - tail) > head { diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 4e066ecf999e3..ef6475f9ee4c7 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -302,6 +302,18 @@ impl EarlyLintPass for UnusedParens { Assign(_, ref value) => (value, "assigned value", false), AssignOp(.., ref value) => (value, "assigned value", false), InPlace(_, ref value) => (value, "emplacement value", false), + Call(_, ref args) => { + for arg in args { + self.check_unused_parens_core(cx, arg, "function argument", false) + } + return; + }, + MethodCall(_, ref args) => { + for arg in &args[1..] { // first "argument" is self (which sometimes needs parens) + self.check_unused_parens_core(cx, arg, "method argument", false) + } + return; + } _ => return, }; self.check_unused_parens_core(cx, &value, msg, struct_lit_needs_parens); diff --git a/src/test/compile-fail/lint-unnecessary-parens.rs b/src/test/compile-fail/lint-unnecessary-parens.rs index b5eac73a55d1c..7cd0a6bbf0fd0 100644 --- a/src/test/compile-fail/lint-unnecessary-parens.rs +++ b/src/test/compile-fail/lint-unnecessary-parens.rs @@ -13,19 +13,19 @@ #[derive(Eq, PartialEq)] struct X { y: bool } impl X { - fn foo(&self) -> bool { self.y } + fn foo(&self, conjunct: bool) -> bool { self.y && conjunct } } fn foo() -> isize { return (1); //~ ERROR unnecessary parentheses around `return` value } -fn bar() -> X { - return (X { y: true }); //~ ERROR unnecessary parentheses around `return` value +fn bar(y: bool) -> X { + return (X { y }); //~ ERROR unnecessary parentheses around `return` value } fn main() { foo(); - bar(); + bar((true)); //~ ERROR unnecessary parentheses around function argument if (true) {} //~ ERROR unnecessary parentheses around `if` condition while (true) {} //~ ERROR unnecessary parentheses around `while` condition @@ -40,13 +40,15 @@ fn main() { if (X { y: true } == v) {} if (X { y: false }.y) {} - while (X { y: false }.foo()) {} + while (X { y: false }.foo(true)) {} while (true | X { y: false }.y) {} match (X { y: false }) { _ => {} } + X { y: false }.foo((true)); //~ ERROR unnecessary parentheses around method argument + let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value _a = (0); //~ ERROR unnecessary parentheses around assigned value _a += (1); //~ ERROR unnecessary parentheses around assigned value