summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/suspicious_operation_groupings.rs
blob: dd4f3b71c378f698f2d8bee2697d566f725ea74b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//@run-rustfix
#![warn(clippy::suspicious_operation_groupings)]
#![allow(dead_code, unused_parens, clippy::eq_op)]

struct Vec3 {
    x: f64,
    y: f64,
    z: f64,
}

impl Eq for Vec3 {}

impl PartialEq for Vec3 {
    fn eq(&self, other: &Self) -> bool {
        // This should trigger the lint because `self.x` is compared to `other.y`
        self.x == other.y && self.y == other.y && self.z == other.z
    }
}

struct S {
    a: i32,
    b: i32,
    c: i32,
    d: i32,
}

fn buggy_ab_cmp(s1: &S, s2: &S) -> bool {
    // There's no `s1.b`
    s1.a < s2.a && s1.a < s2.b
}

struct SaOnly {
    a: i32,
}

impl S {
    fn a(&self) -> i32 {
        0
    }
}

fn do_not_give_bad_suggestions_for_this_unusual_expr(s1: &S, s2: &SaOnly) -> bool {
    // This is superficially similar to `buggy_ab_cmp`, but we should not suggest
    // `s2.b` since that is invalid.
    s1.a < s2.a && s1.a() < s1.b
}

fn do_not_give_bad_suggestions_for_this_macro_expr(s1: &S, s2: &SaOnly) -> bool {
    macro_rules! s1 {
        () => {
            S {
                a: 1,
                b: 1,
                c: 1,
                d: 1,
            }
        };
    }

    // This is superficially similar to `buggy_ab_cmp`, but we should not suggest
    // `s2.b` since that is invalid.
    s1.a < s2.a && s1!().a < s1.b
}

fn do_not_give_bad_suggestions_for_this_incorrect_expr(s1: &S, s2: &SaOnly) -> bool {
    // There's two `s1.b`, but we should not suggest `s2.b` since that is invalid
    s1.a < s2.a && s1.b < s1.b
}

fn permissable(s1: &S, s2: &S) -> bool {
    // Something like this seems like it might actually be what is desired.
    s1.a == s2.b
}

fn non_boolean_operators(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    s1.a * s2.a + s1.b * s2.b + s1.c * s2.b + s1.d * s2.d
}

fn odd_number_of_pairs(s1: &S, s2: &S) -> i32 {
    // There's no `s2.b`
    s1.a * s2.a + s1.b * s2.c + s1.c * s2.c
}

fn not_caught_by_eq_op_middle_change_left(s1: &S, s2: &S) -> i32 {
    // There's no `s1.b`
    s1.a * s2.a + s2.b * s2.b + s1.c * s2.c
}

fn not_caught_by_eq_op_middle_change_right(s1: &S, s2: &S) -> i32 {
    // There's no `s2.b`
    s1.a * s2.a + s1.b * s1.b + s1.c * s2.c
}

fn not_caught_by_eq_op_start(s1: &S, s2: &S) -> i32 {
    // There's no `s2.a`
    s1.a * s1.a + s1.b * s2.b + s1.c * s2.c
}

fn not_caught_by_eq_op_end(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    s1.a * s2.a + s1.b * s2.b + s1.c * s1.c
}

fn the_cross_product_should_not_lint(s1: &S, s2: &S) -> (i32, i32, i32) {
    (
        s1.b * s2.c - s1.c * s2.b,
        s1.c * s2.a - s1.a * s2.c,
        s1.a * s2.b - s1.b * s2.a,
    )
}

fn outer_parens_simple(s1: &S, s2: &S) -> i32 {
    // There's no `s2.b`
    (s1.a * s2.a + s1.b * s1.b)
}

fn outer_parens(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    (s1.a * s2.a + s1.b * s2.b + s1.c * s2.b + s1.d * s2.d)
}

fn inner_parens(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    (s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d)
}

fn outer_and_some_inner_parens(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    ((s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d))
}

fn all_parens_balanced_tree(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    (((s1.a * s2.a) + (s1.b * s2.b)) + ((s1.c * s2.b) + (s1.d * s2.d)))
}

fn all_parens_left_tree(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    (((s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b)) + (s1.d * s2.d))
}

fn all_parens_right_tree(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    ((s1.a * s2.a) + ((s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d)))
}

fn inside_other_binop_expression(s1: &S, s2: &S) -> i32 {
    // There's no `s1.b`
    (s1.a * s2.a + s2.b * s2.b) / 2
}

fn inside_function_call(s1: &S, s2: &S) -> i32 {
    // There's no `s1.b`
    i32::swap_bytes(s1.a * s2.a + s2.b * s2.b)
}

fn inside_larger_boolean_expression(s1: &S, s2: &S) -> bool {
    // There's no `s1.c`
    s1.a > 0 && s1.b > 0 && s1.d == s2.c && s1.d == s2.d
}

fn inside_larger_boolean_expression_with_unsorted_ops(s1: &S, s2: &S) -> bool {
    // There's no `s1.c`
    s1.a > 0 && s1.d == s2.c && s1.b > 0 && s1.d == s2.d
}

struct Nested {
    inner: ((i32,), (i32,), (i32,)),
}

fn changed_middle_ident(n1: &Nested, n2: &Nested) -> bool {
    // There's no `n2.inner.2.0`
    (n1.inner.0).0 == (n2.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.1).0
}

// `eq_op` should catch this one.
fn changed_initial_ident(n1: &Nested, n2: &Nested) -> bool {
    // There's no `n2.inner.0.0`
    (n1.inner.0).0 == (n1.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.2).0
}

fn inside_fn_with_similar_expression(s1: &S, s2: &S, strict: bool) -> bool {
    if strict {
        s1.a < s2.a && s1.b < s2.b
    } else {
        // There's no `s1.b` in this subexpression
        s1.a <= s2.a && s1.a <= s2.b
    }
}

fn inside_an_if_statement(s1: &mut S, s2: &S) {
    // There's no `s1.b`
    if s1.a < s2.a && s1.a < s2.b {
        s1.c = s2.c;
    }
}

fn maximum_unary_minus_right_tree(s1: &S, s2: &S) -> i32 {
    // There's no `s2.c`
    -(-(-s1.a * -s2.a) + (-(-s1.b * -s2.b) + -(-s1.c * -s2.b) + -(-s1.d * -s2.d)))
}

fn unary_minus_and_an_if_expression(s1: &S, s2: &S) -> i32 {
    // There's no `s1.b`
    -(if -s1.a < -s2.a && -s1.a < -s2.b { s1.c } else { s2.a })
}

fn main() {}