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