Closed
Description
Summary
implementing Shl
with side effects (like calling println!
) causes clippy to still throw out an "unnecessary_operation" warning, but only if the right hand side value comes from a function call
if you implement an empty drop it also lints for the value that does not come from a function call
this is also true for other std::ops
traits like Add
Lint Name
unnecessary_operation
Reproducer
(i know the code is a bit fucked up)
use std::{fmt::Display, ops::Shl};
#[allow(non_camel_case_types)]
struct cout;
impl<T> Shl<T> for cout
where
T: Display,
{
type Output = Self;
fn shl(self, rhs: T) -> Self::Output {
println!("{}", rhs);
self
}
}
fn n() -> i32 {
42
}
#[allow(unused_must_use)]
#[allow(clippy::no_effect)]
fn main() {
cout << 142; // <= no unnecessary_operation warning
cout << n(); // <= unnecessary_operation warning
}
the output for cargo clippy:
$ cargo clippy
warning: unnecessary operation
--> src/main.rs:25:2
|
25 | cout << n(); // <= unnecessary_operation warning
| ^^^^^^^^^^^^ help: statement can be reduced to: `cout;n();`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
= note: `#[warn(clippy::unnecessary_operation)]` on by default
warning: `stuff` (bin "stuff") generated 1 warning (run `cargo clippy --fix --bin "stuff"` to apply 1 suggestion)
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
the lint only triggers on the second line, and if i remove the suggestion only the 420 gets printed
$ cargo run
420
42
$ cargo clippy --fix
$ cargo run
420
if you implement Drop
for cout
, the lint triggers on both lines
Version
rustc 1.75.0-beta.1 (782883f60 2023-11-12)
binary: rustc
commit-hash: 782883f609713fe9617ba64d90086742ec62d374
commit-date: 2023-11-12
host: x86_64-unknown-linux-gnu
release: 1.75.0-beta.1
LLVM version: 17.0.4
Additional Labels
No response