]> git.proxmox.com Git - rustc.git/blob - src/test/ui/lint/lint-unnecessary-parens.rs
New upstream version 1.44.1+dfsg1
[rustc.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 fn unused_parens_around_block_return() -> u32 {
21 let foo = {
22 (5) //~ ERROR unnecessary parentheses around block return value
23 };
24 (5) //~ ERROR unnecessary parentheses around block return value
25 }
26
27 trait Trait {
28 fn test(&self);
29 }
30
31 fn passes_unused_parens_lint() -> &'static (dyn Trait) {
32 panic!()
33 }
34
35 macro_rules! baz {
36 ($($foo:expr),+) => {
37 ($($foo),*)
38 }
39 }
40
41 const CONST_ITEM: usize = (10); //~ ERROR unnecessary parentheses around assigned value
42 static STATIC_ITEM: usize = (10); //~ ERROR unnecessary parentheses around assigned value
43
44 fn main() {
45 foo();
46 bar((true)); //~ ERROR unnecessary parentheses around function argument
47
48 if (true) {} //~ ERROR unnecessary parentheses around `if` condition
49 while (true) {} //~ ERROR unnecessary parentheses around `while` condition
50 //~^ WARN denote infinite loops with
51 match (true) { //~ ERROR unnecessary parentheses around `match` scrutinee expression
52 _ => {}
53 }
54 if let 1 = (1) {} //~ ERROR unnecessary parentheses around `let` scrutinee expression
55 while let 1 = (2) {} //~ ERROR unnecessary parentheses around `let` scrutinee expression
56 let v = X { y: false };
57 // struct lits needs parens, so these shouldn't warn.
58 if (v == X { y: true }) {}
59 if (X { y: true } == v) {}
60 if (X { y: false }.y) {}
61
62 while (X { y: false }.foo(true)) {}
63 while (true | X { y: false }.y) {}
64
65 match (X { y: false }) {
66 _ => {}
67 }
68
69 X { y: false }.foo((true)); //~ ERROR unnecessary parentheses around method argument
70
71 let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value
72 _a = (0); //~ ERROR unnecessary parentheses around assigned value
73 _a += (1); //~ ERROR unnecessary parentheses around assigned value
74
75 let _a = baz!(3, 4);
76 let _b = baz!(3);
77 }