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