]> git.proxmox.com Git - rustc.git/blob - src/test/ui/lint/lint-unnecessary-parens.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / src / test / ui / lint / lint-unnecessary-parens.rs
1 // run-rustfix
2
3 #![deny(unused_parens)]
4 #![allow(while_true)] // for rustfix
5
6 #[derive(Eq, PartialEq)]
7 struct X { y: bool }
8 impl X {
9 fn foo(&self, conjunct: bool) -> bool { self.y && conjunct }
10 }
11
12 fn foo() -> isize {
13 return (1); //~ ERROR unnecessary parentheses around `return` value
14 }
15 fn bar(y: bool) -> X {
16 return (X { y }); //~ ERROR unnecessary parentheses around `return` value
17 }
18
19 pub fn unused_parens_around_return_type() -> (u32) { //~ ERROR unnecessary parentheses around type
20 panic!()
21 }
22
23 pub fn unused_parens_around_block_return() -> u32 {
24 let _foo = {
25 (5) //~ ERROR unnecessary parentheses around block return value
26 };
27 (5) //~ ERROR unnecessary parentheses around block return value
28 }
29
30 pub trait Trait {
31 fn test(&self);
32 }
33
34 pub fn passes_unused_parens_lint() -> &'static (dyn Trait) {
35 panic!()
36 }
37
38 macro_rules! baz {
39 ($($foo:expr),+) => {
40 ($($foo),*)
41 }
42 }
43
44 pub const CONST_ITEM: usize = (10); //~ ERROR unnecessary parentheses around assigned value
45 pub static STATIC_ITEM: usize = (10); //~ ERROR unnecessary parentheses around assigned value
46
47 fn main() {
48 foo();
49 bar((true)); //~ ERROR unnecessary parentheses around function argument
50
51 if (true) {} //~ ERROR unnecessary parentheses around `if` condition
52 while (true) {} //~ ERROR unnecessary parentheses around `while` condition
53 match (true) { //~ ERROR unnecessary parentheses around `match` scrutinee expression
54 _ => {}
55 }
56 if let 1 = (1) {} //~ ERROR unnecessary parentheses around `let` scrutinee expression
57 while let 1 = (2) {} //~ ERROR unnecessary parentheses around `let` scrutinee expression
58 let v = X { y: false };
59 // struct lits needs parens, so these shouldn't warn.
60 if (v == X { y: true }) {}
61 if (X { y: true } == v) {}
62 if (X { y: false }.y) {}
63
64 while (X { y: false }.foo(true)) {}
65 while (true | X { y: false }.y) {}
66
67 match (X { y: false }) {
68 _ => {}
69 }
70
71 X { y: false }.foo((true)); //~ ERROR unnecessary parentheses around method argument
72
73 let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value
74 _a = (0); //~ ERROR unnecessary parentheses around assigned value
75 _a += (1); //~ ERROR unnecessary parentheses around assigned value
76
77 let _a = baz!(3, 4);
78 let _b = baz!(3);
79 }