Skip to content

Commit 5ccdeea

Browse files
committed
Auto merge of #46980 - zackmdavis:and_the_case_of_the_needlessly_parenthesized_arguments, r=petrochenkov
in which the unused-parens lint comes to cover function and method args Resolves #46137.
2 parents 3fd27b2 + b7739f3 commit 5ccdeea

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/liballoc/vec_deque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,7 @@ impl<T> From<VecDeque<T>> for Vec<T> {
24802480
if other.is_contiguous() {
24812481
ptr::copy(buf.offset(tail as isize), buf, len);
24822482
} else {
2483-
if (tail - head) >= cmp::min((cap - tail), head) {
2483+
if (tail - head) >= cmp::min(cap - tail, head) {
24842484
// There is enough free space in the centre for the shortest block so we can
24852485
// do this in at most three copy moves.
24862486
if (cap - tail) > head {

src/librustc_lint/unused.rs

+12
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,18 @@ impl EarlyLintPass for UnusedParens {
302302
Assign(_, ref value) => (value, "assigned value", false),
303303
AssignOp(.., ref value) => (value, "assigned value", false),
304304
InPlace(_, ref value) => (value, "emplacement value", false),
305+
Call(_, ref args) => {
306+
for arg in args {
307+
self.check_unused_parens_core(cx, arg, "function argument", false)
308+
}
309+
return;
310+
},
311+
MethodCall(_, ref args) => {
312+
for arg in &args[1..] { // first "argument" is self (which sometimes needs parens)
313+
self.check_unused_parens_core(cx, arg, "method argument", false)
314+
}
315+
return;
316+
}
305317
_ => return,
306318
};
307319
self.check_unused_parens_core(cx, &value, msg, struct_lit_needs_parens);

src/test/compile-fail/lint-unnecessary-parens.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
#[derive(Eq, PartialEq)]
1414
struct X { y: bool }
1515
impl X {
16-
fn foo(&self) -> bool { self.y }
16+
fn foo(&self, conjunct: bool) -> bool { self.y && conjunct }
1717
}
1818

1919
fn foo() -> isize {
2020
return (1); //~ ERROR unnecessary parentheses around `return` value
2121
}
22-
fn bar() -> X {
23-
return (X { y: true }); //~ ERROR unnecessary parentheses around `return` value
22+
fn bar(y: bool) -> X {
23+
return (X { y }); //~ ERROR unnecessary parentheses around `return` value
2424
}
2525

2626
fn main() {
2727
foo();
28-
bar();
28+
bar((true)); //~ ERROR unnecessary parentheses around function argument
2929

3030
if (true) {} //~ ERROR unnecessary parentheses around `if` condition
3131
while (true) {} //~ ERROR unnecessary parentheses around `while` condition
@@ -40,13 +40,15 @@ fn main() {
4040
if (X { y: true } == v) {}
4141
if (X { y: false }.y) {}
4242

43-
while (X { y: false }.foo()) {}
43+
while (X { y: false }.foo(true)) {}
4444
while (true | X { y: false }.y) {}
4545

4646
match (X { y: false }) {
4747
_ => {}
4848
}
4949

50+
X { y: false }.foo((true)); //~ ERROR unnecessary parentheses around method argument
51+
5052
let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value
5153
_a = (0); //~ ERROR unnecessary parentheses around assigned value
5254
_a += (1); //~ ERROR unnecessary parentheses around assigned value

0 commit comments

Comments
 (0)