about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/op_ref.fixed
blob: 4bf4b91888c8c2e5dfcddb1e4c7bf3b5de2d849c (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
#![allow(unused_variables, clippy::disallowed_names)]
#![warn(clippy::op_ref)]
use std::collections::HashSet;
use std::ops::{BitAnd, Mul};

fn main() {
    let tracked_fds: HashSet<i32> = HashSet::new();
    let new_fds = HashSet::new();
    let unwanted = &tracked_fds - &new_fds;

    let foo = 5 - 6;
    //~^ op_ref

    let bar = String::new();
    let bar = "foo" == &bar;

    let a = "a".to_string();
    let b = "a";

    if b < &a {
        println!("OK");
    }

    struct X(i32);
    impl BitAnd for X {
        type Output = X;
        fn bitand(self, rhs: X) -> X {
            X(self.0 & rhs.0)
        }
    }
    impl<'a> BitAnd<&'a X> for X {
        type Output = X;
        fn bitand(self, rhs: &'a X) -> X {
            X(self.0 & rhs.0)
        }
    }
    let x = X(1);
    let y = X(2);
    let z = x & &y;

    #[derive(Copy, Clone)]
    struct Y(i32);
    impl BitAnd for Y {
        type Output = Y;
        fn bitand(self, rhs: Y) -> Y {
            Y(self.0 & rhs.0)
        }
    }
    impl<'a> BitAnd<&'a Y> for Y {
        type Output = Y;
        fn bitand(self, rhs: &'a Y) -> Y {
            Y(self.0 & rhs.0)
        }
    }
    let x = Y(1);
    let y = Y(2);
    let z = x & y;
    //~^ op_ref
}

#[derive(Clone, Copy)]
struct A(i32);
#[derive(Clone, Copy)]
struct B(i32);

impl Mul<&A> for B {
    type Output = i32;
    fn mul(self, rhs: &A) -> Self::Output {
        self.0 * rhs.0
    }
}
impl Mul<A> for B {
    type Output = i32;
    fn mul(self, rhs: A) -> Self::Output {
        // Should not lint because removing the reference would lead to unconditional recursion
        self * &rhs
    }
}
impl Mul<&A> for A {
    type Output = i32;
    fn mul(self, rhs: &A) -> Self::Output {
        self.0 * rhs.0
    }
}
impl Mul<A> for A {
    type Output = i32;
    fn mul(self, rhs: A) -> Self::Output {
        let one = B(1);
        let two = 2;
        let three = 3;
        let _ = one * self;
        //~^ op_ref

        let _ = two + three;
        //~^ op_ref

        // Removing the reference would lead to unconditional recursion
        self * &rhs
    }
}

mod issue_2597 {
    fn ex1() {
        let a: &str = "abc";
        let b: String = "abc".to_owned();
        println!("{}", a > &b);
    }

    pub fn ex2<T: Ord + PartialOrd>(array: &[T], val: &T, idx: usize) -> bool {
        &array[idx] < val
    }
}

#[allow(clippy::needless_if)]
fn issue15063() {
    use std::ops::BitAnd;

    macro_rules! mac {
        ($e:expr) => {
            $e.clone()
        };
    }

    let x = 1;
    if x == mac!(1) {}
    //~^ op_ref

    #[derive(Copy, Clone)]
    struct Y(i32);
    impl BitAnd for Y {
        type Output = Y;
        fn bitand(self, rhs: Y) -> Y {
            Y(self.0 & rhs.0)
        }
    }
    impl<'a> BitAnd<&'a Y> for Y {
        type Output = Y;
        fn bitand(self, rhs: &'a Y) -> Y {
            Y(self.0 & rhs.0)
        }
    }
    let x = Y(1);
    let y = Y(2);
    let z = x & mac!(y);
    //~^ op_ref
}