diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/ui/op_ref.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/op_ref.rs')
-rw-r--r-- | src/tools/clippy/tests/ui/op_ref.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/op_ref.rs b/src/tools/clippy/tests/ui/op_ref.rs new file mode 100644 index 000000000..d8bf66603 --- /dev/null +++ b/src/tools/clippy/tests/ui/op_ref.rs @@ -0,0 +1,94 @@ +#![allow(unused_variables, clippy::blacklisted_name)] +#![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; + + 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; +} + +#[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 + } +} |