rust-lang/rust-clippy

suspicious-operation-groupings generates a false positive, suggested code doesn't even compile

Open

#7,086 opened on Apr 15, 2021

View on GitHub
聽(8 comments)聽(0 reactions)聽(1 assignee)Rust聽(10,406 stars)聽(1,391 forks)batch import
C-bugI-false-positiveI-suggestion-causes-errorgood first issue

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

suspicious-operation-groupings generates a false positive, suggested code doesn't even compile 路 rust-lang/rust-clippy#7086 | Good First Issue