rust-lang / rust-lang/rust-clippy
suspicious-operation-groupings generates a false positive, suggested code doesn't even compile
Open
@esther-ff is already working on this.
Since Aug 8, 2025.
C-bug
good first issue
I-false-positive
I-suggestion-causes-error
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Lint name: suspicious-operation-groupings
I tried this code:
use std::net::Ipv4Addr;
struct Message {
source_ip: Ipv4Addr,
port: u16,
}
struct Info {
ip: Ipv4Addr,
port: u16,
}
fn process_response(response: Message, info: &Info) -> bool {
if response.source_ip != info.ip || response.port != info.port {
return false;
}
true
}
fn main() {
let response = Message {
source_ip: [1, 2, 3, 4].into(),
port: 80,
};
let info = Info {
ip: [2, 3, 4, 5].into(),
port: 80,
};
process_response(response, &info);
}
I expected to see this happen: No clippy warnings.
Instead, this happened:
warning: This sequence of operators looks suspiciously like a bug.
--> src\main.rs:14:8
|
14 | if response.source_ip != info.ip || response.port != info.port {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: I think you meant: `response.source_ip != info.source_ip`
|
= note: `#[warn(clippy::suspicious_operation_groupings)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_operation_groupings
warning: This sequence of operators looks suspiciously like a bug.
--> src\main.rs:14:8
|
14 | if response.source_ip != info.ip || response.port != info.port {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: I think you meant: `response.source_ip != info.source_ip`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_operation_groupings
warning: 2 warnings emitted
I see what kind of bugs the lint is trying to prevent.
However, the suggestion doesn't even compile. Checking if the suggested struct members exist should prevent many false positives.
Meta
cargo clippy -V: clippy 0.1.51 (2fd73fab 2021-03-23)rustc -Vv:rustc 1.51.0 (2fd73fabe 2021-03-23) binary: rustc commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0 commit-date: 2021-03-23 host: x86_64-pc-windows-msvc release: 1.51.0 LLVM version: 11.0.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.