]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/op_ref.rs
Fix `unnecessary_cast` suggestion when taking a reference
[rust.git] / tests / ui / op_ref.rs
index bacf9f1057bbe401f78ffa766d41f8f53201ee35..07226b0a1a83b272a250e20429b555b30f04bacd 100644 (file)
@@ -1,19 +1,7 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-
-
-
-
-#![allow(unused_variables, clippy::blacklisted_name)]
-
+#![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();
@@ -31,4 +19,76 @@ fn main() {
     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;
+}
+
+#[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;
+        let _ = two + &three;
+        // Removing the reference would lead to unconditional recursion
+        self * &rhs
+    }
 }