fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![allow(unused_variables, clippy::disallowed_names)] 2#![warn(clippy::op_ref)] 3use std::collections::HashSet; 4use std::ops::{BitAnd, Mul}; 5 6fn main() { 7 let tracked_fds: HashSet<i32> = HashSet::new(); 8 let new_fds = HashSet::new(); 9 let unwanted = &tracked_fds - &new_fds; 10 11 let foo = 5 - 6; 12 //~^ ERROR: needlessly taken reference of both operands 13 //~| NOTE: `-D clippy::op-ref` implied by `-D warnings` 14 15 let bar = String::new(); 16 let bar = "foo" == &bar; 17 18 let a = "a".to_string(); 19 let b = "a"; 20 21 if b < &a { 22 println!("OK"); 23 } 24 25 struct X(i32); 26 impl BitAnd for X { 27 type Output = X; 28 fn bitand(self, rhs: X) -> X { 29 X(self.0 & rhs.0) 30 } 31 } 32 impl<'a> BitAnd<&'a X> for X { 33 type Output = X; 34 fn bitand(self, rhs: &'a X) -> X { 35 X(self.0 & rhs.0) 36 } 37 } 38 let x = X(1); 39 let y = X(2); 40 let z = x & &y; 41 42 #[derive(Copy, Clone)] 43 struct Y(i32); 44 impl BitAnd for Y { 45 type Output = Y; 46 fn bitand(self, rhs: Y) -> Y { 47 Y(self.0 & rhs.0) 48 } 49 } 50 impl<'a> BitAnd<&'a Y> for Y { 51 type Output = Y; 52 fn bitand(self, rhs: &'a Y) -> Y { 53 Y(self.0 & rhs.0) 54 } 55 } 56 let x = Y(1); 57 let y = Y(2); 58 let z = x & y; 59 //~^ ERROR: taken reference of right operand 60} 61 62#[derive(Clone, Copy)] 63struct A(i32); 64#[derive(Clone, Copy)] 65struct B(i32); 66 67impl Mul<&A> for B { 68 type Output = i32; 69 fn mul(self, rhs: &A) -> Self::Output { 70 self.0 * rhs.0 71 } 72} 73impl Mul<A> for B { 74 type Output = i32; 75 fn mul(self, rhs: A) -> Self::Output { 76 // Should not lint because removing the reference would lead to unconditional recursion 77 self * &rhs 78 } 79} 80impl Mul<&A> for A { 81 type Output = i32; 82 fn mul(self, rhs: &A) -> Self::Output { 83 self.0 * rhs.0 84 } 85} 86impl Mul<A> for A { 87 type Output = i32; 88 fn mul(self, rhs: A) -> Self::Output { 89 let one = B(1); 90 let two = 2; 91 let three = 3; 92 let _ = one * self; 93 //~^ ERROR: taken reference of right operand 94 let _ = two + three; 95 //~^ ERROR: taken reference of right operand 96 // Removing the reference would lead to unconditional recursion 97 self * &rhs 98 } 99}