]>
Commit | Line | Data |
---|---|---|
c620b35d | 1 | //@ run-rustfix |
f035d41b XL |
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 | ||
fe692bf9 FG |
38 | pub fn parens_with_keyword(e: &[()]) -> i32 { |
39 | if true {} //~ ERROR unnecessary parentheses around `if` | |
40 | while true {} //~ ERROR unnecessary parentheses around `while` | |
41 | for _ in e {} //~ ERROR unnecessary parentheses around `for` | |
42 | match 1 { _ => ()} //~ ERROR unnecessary parentheses around `match` | |
43 | return 1; //~ ERROR unnecessary parentheses around `return` value | |
44 | } | |
45 | ||
f035d41b XL |
46 | macro_rules! baz { |
47 | ($($foo:expr),+) => { | |
48 | ($($foo),*) | |
31ef2f64 FG |
49 | }; |
50 | } | |
51 | ||
52 | macro_rules! unit { | |
53 | () => { | |
54 | () | |
55 | }; | |
56 | } | |
57 | ||
58 | struct One; | |
59 | ||
60 | impl std::ops::Sub<One> for () { | |
61 | type Output = i32; | |
62 | fn sub(self, _: One) -> Self::Output { | |
63 | -1 | |
64 | } | |
65 | } | |
66 | ||
67 | impl std::ops::Neg for One { | |
68 | type Output = i32; | |
69 | fn neg(self) -> Self::Output { | |
70 | -1 | |
f035d41b XL |
71 | } |
72 | } | |
73 | ||
74 | pub const CONST_ITEM: usize = 10; //~ ERROR unnecessary parentheses around assigned value | |
75 | pub static STATIC_ITEM: usize = 10; //~ ERROR unnecessary parentheses around assigned value | |
76 | ||
77 | fn main() { | |
78 | foo(); | |
79 | bar(true); //~ ERROR unnecessary parentheses around function argument | |
80 | ||
81 | if true {} //~ ERROR unnecessary parentheses around `if` condition | |
82 | while true {} //~ ERROR unnecessary parentheses around `while` condition | |
83 | match true { //~ ERROR unnecessary parentheses around `match` scrutinee expression | |
84 | _ => {} | |
85 | } | |
86 | if let 1 = 1 {} //~ ERROR unnecessary parentheses around `let` scrutinee expression | |
87 | while let 1 = 2 {} //~ ERROR unnecessary parentheses around `let` scrutinee expression | |
88 | let v = X { y: false }; | |
89 | // struct lits needs parens, so these shouldn't warn. | |
90 | if (v == X { y: true }) {} | |
91 | if (X { y: true } == v) {} | |
92 | if (X { y: false }.y) {} | |
29967ef6 XL |
93 | // this shouldn't warn, because the parens are necessary to disambiguate let chains |
94 | if let true = (true && false) {} | |
f035d41b XL |
95 | |
96 | while (X { y: false }.foo(true)) {} | |
97 | while (true | X { y: false }.y) {} | |
98 | ||
99 | match (X { y: false }) { | |
100 | _ => {} | |
101 | } | |
102 | ||
103 | X { y: false }.foo(true); //~ ERROR unnecessary parentheses around method argument | |
104 | ||
105 | let mut _a = 0; //~ ERROR unnecessary parentheses around assigned value | |
106 | _a = 0; //~ ERROR unnecessary parentheses around assigned value | |
107 | _a += 1; //~ ERROR unnecessary parentheses around assigned value | |
108 | ||
ed00b5ec FG |
109 | let mut _a = 3; //~ ERROR unnecessary parentheses around pattern |
110 | let mut _a = 3; //~ ERROR unnecessary parentheses around pattern | |
111 | let mut _a = 3; //~ ERROR unnecessary parentheses around pattern | |
112 | ||
113 | let _a = 3; //~ ERROR unnecessary parentheses around pattern | |
114 | let _a = 3; //~ ERROR unnecessary parentheses around pattern | |
115 | let _a = 3; //~ ERROR unnecessary parentheses around pattern | |
116 | ||
f035d41b XL |
117 | let _a = baz!(3, 4); |
118 | let _b = baz!(3); | |
31ef2f64 FG |
119 | |
120 | let _ = { | |
121 | unit!() - One //~ ERROR unnecessary parentheses around block return value | |
122 | } + { | |
123 | unit![] - One //~ ERROR unnecessary parentheses around block return value | |
124 | } + { | |
125 | // FIXME: false positive. This parenthesis is required. | |
126 | unit! {} - One //~ ERROR unnecessary parentheses around block return value | |
127 | }; | |
f035d41b | 128 | } |