You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)]structMyStruct{index:usize,}implMul<&MyStruct>for&MyStruct{typeOutput = MyStruct;fnmul(self,rhs:&MyStruct) -> MyStruct{MyStruct{index:self.index + rhs.index}}}fnmain(){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 clippy doesn't complain at all. Now, for convenience purposes, I would like to be able to do first_struct * &second_struct, which is why I also defined the trait for MyStruct like so:
use std::ops::Mul;#[derive(Clone,Copy)]structMyStruct{index:usize,}implMul<&MyStruct>for&MyStruct{typeOutput = MyStruct;fnmul(self,rhs:&MyStruct) -> MyStruct{MyStruct{index:self.index + rhs.index}}}implMul<&MyStruct>forMyStruct{typeOutput = MyStruct;fnmul(self,rhs:&MyStruct) -> MyStruct{&self* rhs
}}fnmain(){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, clippy now complains with:
warning: needlessly taken reference of left operand
--> src/main.rs:18:9
|
18 | &self * rhs
| -----^^^^^^
| |
| help: use the left value directly: `self`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#op_ref
= note: `#[warn(clippy::op_ref)]` on by default
Checks for arguments to == which have their address taken to satisfy a bound and suggests to dereference the other argument instead
Why is this bad?
It is more idiomatic to dereference the other argument.
Example
&x == y
Use instead
x == *y
However, in my case, I don't use the == comparator, but the * operator, so the proposed fix doesn't really make sense here as far as I can tell.
Note that the warning disappears when removing the Copy trait for MyStruct. On the one hand, this seems logical, since I can cheaply copy the object, the implementation for owned values should be identical to that of borrowed ones. But on the other hand, I do have to implement the Mul trait for MyStruct, as the program otherwise doesn't compile, like this one:
use std::ops::Mul;#[derive(Clone,Copy)]structMyStruct{index:usize,}implMul<&MyStruct>for&MyStruct{typeOutput = MyStruct;fnmul(self,rhs:&MyStruct) -> MyStruct{MyStruct{index:self.index + rhs.index}}}fnmain(){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?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
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:
This works fine, and
clippydoesn't complain at all. Now, for convenience purposes, I would like to be able to dofirst_struct * &second_struct, which is why I also defined the trait forMyStructlike so:Though this works,
clippynow complains with:Reading the linked page, it says:
However, in my case, I don't use the
==comparator, but the*operator, so the proposed fix doesn't really make sense here as far as I can tell.Note that the warning disappears when removing the
Copytrait forMyStruct. On the one hand, this seems logical, since I can cheaply copy the object, the implementation for owned values should be identical to that of borrowed ones. But on the other hand, I do have to implement theMultrait forMyStruct, as the program otherwise doesn't compile, like this one:Is there a more idiomatic way to write this code, or should I just silence this warning?
Beta Was this translation helpful? Give feedback.
All reactions