-
|
Note: this is essentially just a port of a question I already asked on StackOverflow, to no avail. I figured that a question about clippy may be better answered here! Let us consider the following simple program: use std::ops::Mul;
#[derive(Clone, Copy)]
struct MyStruct {
index: usize,
}
impl Mul<&MyStruct> for &MyStruct {
type Output = MyStruct;
fn mul(self, rhs: &MyStruct) -> MyStruct {
MyStruct { index: self.index + rhs.index }
}
}
fn main() {
let first_struct = MyStruct { index: 0 };
let second_struct = MyStruct { index: 1 };
let ref_sum = &first_struct * &second_struct;
println!("New index is {}", ref_sum.index);
}This works fine, and use std::ops::Mul;
#[derive(Clone, Copy)]
struct MyStruct {
index: usize,
}
impl Mul<&MyStruct> for &MyStruct {
type Output = MyStruct;
fn mul(self, rhs: &MyStruct) -> MyStruct {
MyStruct { index: self.index + rhs.index }
}
}
impl Mul<&MyStruct> for MyStruct {
type Output = MyStruct;
fn mul(self, rhs: &MyStruct) -> MyStruct {
&self * rhs
}
}
fn main() {
let first_struct = MyStruct { index: 0 };
let second_struct = MyStruct { index: 1 };
let first_struct_ref = &first_struct;
let second_struct_ref = &second_struct;
let ref_sum = first_struct_ref * second_struct_ref;
println!("New index is {}", ref_sum.index);
let other_sum = first_struct * second_struct_ref;
println!("New index is {}", other_sum.index);
}Though this works, Reading the linked page, it says:
However, in my case, I don't use the Note that the warning disappears when removing the use std::ops::Mul;
#[derive(Clone, Copy)]
struct MyStruct {
index: usize,
}
impl Mul<&MyStruct> for &MyStruct {
type Output = MyStruct;
fn mul(self, rhs: &MyStruct) -> MyStruct {
MyStruct { index: self.index + rhs.index }
}
}
fn main() {
let first_struct = MyStruct { index: 0 };
let second_struct = MyStruct { index: 1 };
let first_struct_ref = &first_struct;
let second_struct_ref = &second_struct;
let ref_sum = first_struct_ref * second_struct_ref;
println!("New index is {}", ref_sum.index);
let other_sum = first_struct * second_struct_ref;
println!("New index is {}", other_sum.index);
}Is there a more idiomatic way to write this code, or should I just silence this warning? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
It shouldn't lint there because it would become recursive, for that it looks like there's an open issue #12463 |
Beta Was this translation helpful? Give feedback.
It shouldn't lint there because it would become recursive, for that it looks like there's an open issue #12463