]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-unnecessary-parens.rs
Fix #66295
[rust.git] / src / test / ui / lint / lint-unnecessary-parens.rs
1 #![deny(unused_parens)]
2
3 #[derive(Eq, PartialEq)]
4 struct X { y: bool }
5 impl X {
6     fn foo(&self, conjunct: bool) -> bool { self.y && conjunct }
7 }
8
9 fn foo() -> isize {
10     return (1); //~ ERROR unnecessary parentheses around `return` value
11 }
12 fn bar(y: bool) -> X {
13     return (X { y }); //~ ERROR unnecessary parentheses around `return` value
14 }
15
16 fn unused_parens_around_return_type() -> (u32) { //~ ERROR unnecessary parentheses around type
17     panic!()
18 }
19
20 trait Trait {
21     fn test(&self);
22 }
23
24 fn passes_unused_parens_lint() -> &'static (dyn Trait) {
25     panic!()
26 }
27
28 macro_rules! baz {
29     ($($foo:expr),+) => {
30         ($($foo),*)
31     }
32 }
33
34 fn main() {
35     foo();
36     bar((true)); //~ ERROR unnecessary parentheses around function argument
37
38     if (true) {} //~ ERROR unnecessary parentheses around `if` condition
39     while (true) {} //~ ERROR unnecessary parentheses around `while` condition
40     //~^ WARN denote infinite loops with
41     match (true) { //~ ERROR unnecessary parentheses around `match` head expression
42         _ => {}
43     }
44     if let 1 = (1) {} //~ ERROR unnecessary parentheses around `let` head expression
45     while let 1 = (2) {} //~ ERROR unnecessary parentheses around `let` head expression
46     let v = X { y: false };
47     // struct lits needs parens, so these shouldn't warn.
48     if (v == X { y: true }) {}
49     if (X { y: true } == v) {}
50     if (X { y: false }.y) {}
51
52     while (X { y: false }.foo(true)) {}
53     while (true | X { y: false }.y) {}
54
55     match (X { y: false }) {
56         _ => {}
57     }
58
59     X { y: false }.foo((true)); //~ ERROR unnecessary parentheses around method argument
60
61     let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value
62     _a = (0); //~ ERROR unnecessary parentheses around assigned value
63     _a += (1); //~ ERROR unnecessary parentheses around assigned value
64
65     let _a = baz!(3, 4);
66     let _b = baz!(3);
67 }