]> git.lizzy.rs Git - rust.git/blob - tests/ui/suspicious_operation_groupings.rs
Don't lint `if_same_then_else` with `if let` conditions
[rust.git] / tests / ui / suspicious_operation_groupings.rs
1 #![warn(clippy::suspicious_operation_groupings)]
2 #![allow(clippy::eq_op)]
3
4 struct Vec3 {
5     x: f64,
6     y: f64,
7     z: f64,
8 }
9
10 impl Eq for Vec3 {}
11
12 impl PartialEq for Vec3 {
13     fn eq(&self, other: &Self) -> bool {
14         // This should trigger the lint because `self.x` is compared to `other.y`
15         self.x == other.y && self.y == other.y && self.z == other.z
16     }
17 }
18
19 struct S {
20     a: i32,
21     b: i32,
22     c: i32,
23     d: i32,
24 }
25
26 fn buggy_ab_cmp(s1: &S, s2: &S) -> bool {
27     // There's no `s1.b`
28     s1.a < s2.a && s1.a < s2.b
29 }
30
31 struct SaOnly {
32     a: i32,
33 }
34
35 impl S {
36     fn a(&self) -> i32 {
37         0
38     }
39 }
40
41 fn do_not_give_bad_suggestions_for_this_unusual_expr(s1: &S, s2: &SaOnly) -> bool {
42     // This is superficially similar to `buggy_ab_cmp`, but we should not suggest
43     // `s2.b` since that is invalid.
44     s1.a < s2.a && s1.a() < s1.b
45 }
46
47 fn do_not_give_bad_suggestions_for_this_macro_expr(s1: &S, s2: &SaOnly) -> bool {
48     macro_rules! s1 {
49         () => {
50             S {
51                 a: 1,
52                 b: 1,
53                 c: 1,
54                 d: 1,
55             }
56         };
57     }
58
59     // This is superficially similar to `buggy_ab_cmp`, but we should not suggest
60     // `s2.b` since that is invalid.
61     s1.a < s2.a && s1!().a < s1.b
62 }
63
64 fn do_not_give_bad_suggestions_for_this_incorrect_expr(s1: &S, s2: &SaOnly) -> bool {
65     // There's two `s1.b`, but we should not suggest `s2.b` since that is invalid
66     s1.a < s2.a && s1.b < s1.b
67 }
68
69 fn permissable(s1: &S, s2: &S) -> bool {
70     // Something like this seems like it might actually be what is desired.
71     s1.a == s2.b
72 }
73
74 fn non_boolean_operators(s1: &S, s2: &S) -> i32 {
75     // There's no `s2.c`
76     s1.a * s2.a + s1.b * s2.b + s1.c * s2.b + s1.d * s2.d
77 }
78
79 fn odd_number_of_pairs(s1: &S, s2: &S) -> i32 {
80     // There's no `s2.b`
81     s1.a * s2.a + s1.b * s2.c + s1.c * s2.c
82 }
83
84 fn not_caught_by_eq_op_middle_change_left(s1: &S, s2: &S) -> i32 {
85     // There's no `s1.b`
86     s1.a * s2.a + s2.b * s2.b + s1.c * s2.c
87 }
88
89 fn not_caught_by_eq_op_middle_change_right(s1: &S, s2: &S) -> i32 {
90     // There's no `s2.b`
91     s1.a * s2.a + s1.b * s1.b + s1.c * s2.c
92 }
93
94 fn not_caught_by_eq_op_start(s1: &S, s2: &S) -> i32 {
95     // There's no `s2.a`
96     s1.a * s1.a + s1.b * s2.b + s1.c * s2.c
97 }
98
99 fn not_caught_by_eq_op_end(s1: &S, s2: &S) -> i32 {
100     // There's no `s2.c`
101     s1.a * s2.a + s1.b * s2.b + s1.c * s1.c
102 }
103
104 fn the_cross_product_should_not_lint(s1: &S, s2: &S) -> (i32, i32, i32) {
105     (
106         s1.b * s2.c - s1.c * s2.b,
107         s1.c * s2.a - s1.a * s2.c,
108         s1.a * s2.b - s1.b * s2.a,
109     )
110 }
111
112 fn outer_parens_simple(s1: &S, s2: &S) -> i32 {
113     // There's no `s2.b`
114     (s1.a * s2.a + s1.b * s1.b)
115 }
116
117 fn outer_parens(s1: &S, s2: &S) -> i32 {
118     // There's no `s2.c`
119     (s1.a * s2.a + s1.b * s2.b + s1.c * s2.b + s1.d * s2.d)
120 }
121
122 fn inner_parens(s1: &S, s2: &S) -> i32 {
123     // There's no `s2.c`
124     (s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d)
125 }
126
127 fn outer_and_some_inner_parens(s1: &S, s2: &S) -> i32 {
128     // There's no `s2.c`
129     ((s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d))
130 }
131
132 fn all_parens_balanced_tree(s1: &S, s2: &S) -> i32 {
133     // There's no `s2.c`
134     (((s1.a * s2.a) + (s1.b * s2.b)) + ((s1.c * s2.b) + (s1.d * s2.d)))
135 }
136
137 fn all_parens_left_tree(s1: &S, s2: &S) -> i32 {
138     // There's no `s2.c`
139     (((s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b)) + (s1.d * s2.d))
140 }
141
142 fn all_parens_right_tree(s1: &S, s2: &S) -> i32 {
143     // There's no `s2.c`
144     ((s1.a * s2.a) + ((s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d)))
145 }
146
147 fn inside_other_binop_expression(s1: &S, s2: &S) -> i32 {
148     // There's no `s1.b`
149     (s1.a * s2.a + s2.b * s2.b) / 2
150 }
151
152 fn inside_function_call(s1: &S, s2: &S) -> i32 {
153     // There's no `s1.b`
154     i32::swap_bytes(s1.a * s2.a + s2.b * s2.b)
155 }
156
157 fn inside_larger_boolean_expression(s1: &S, s2: &S) -> bool {
158     // There's no `s1.c`
159     s1.a > 0 && s1.b > 0 && s1.d == s2.c && s1.d == s2.d
160 }
161
162 fn inside_larger_boolean_expression_with_unsorted_ops(s1: &S, s2: &S) -> bool {
163     // There's no `s1.c`
164     s1.a > 0 && s1.d == s2.c && s1.b > 0 && s1.d == s2.d
165 }
166
167 struct Nested {
168     inner: ((i32,), (i32,), (i32,)),
169 }
170
171 fn changed_middle_ident(n1: &Nested, n2: &Nested) -> bool {
172     // There's no `n2.inner.2.0`
173     (n1.inner.0).0 == (n2.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.1).0
174 }
175
176 // `eq_op` should catch this one.
177 fn changed_initial_ident(n1: &Nested, n2: &Nested) -> bool {
178     // There's no `n2.inner.0.0`
179     (n1.inner.0).0 == (n1.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.2).0
180 }
181
182 fn inside_fn_with_similar_expression(s1: &S, s2: &S, strict: bool) -> bool {
183     if strict {
184         s1.a < s2.a && s1.b < s2.b
185     } else {
186         // There's no `s1.b` in this subexpression
187         s1.a <= s2.a && s1.a <= s2.b
188     }
189 }
190
191 fn inside_an_if_statement(s1: &mut S, s2: &S) {
192     // There's no `s1.b`
193     if s1.a < s2.a && s1.a < s2.b {
194         s1.c = s2.c;
195     }
196 }
197
198 fn maximum_unary_minus_right_tree(s1: &S, s2: &S) -> i32 {
199     // There's no `s2.c`
200     -(-(-s1.a * -s2.a) + (-(-s1.b * -s2.b) + -(-s1.c * -s2.b) + -(-s1.d * -s2.d)))
201 }
202
203 fn unary_minus_and_an_if_expression(s1: &S, s2: &S) -> i32 {
204     // There's no `s1.b`
205     -(if -s1.a < -s2.a && -s1.a < -s2.b { s1.c } else { s2.a })
206 }
207
208 fn main() {}