rust-lang/rust-clippy

suspicious-operation-groupings false positive

Open

#6,722 建立於 2021年2月11日

在 GitHub 查看
 (12 留言) (1 反應) (1 負責人)Rust (1,391 fork)batch import
C-bugI-false-positivegood first issue

倉庫指標

Star
 (10,406 star)
PR 合併指標
 (平均合併 19天 22小時) (30 天內合併 113 個 PR)

描述

Lint name: suspicious-operation-groupings

I tried this code:

pub fn truncated_cone_volume([r0, r1]: [f32; 2], height: f32) -> f32 {
    use std::f32::consts::PI;
    PI * height * (r0 * r0 + r0 * r1 + r1 * r1) / 3.0
}

Latest clippy gives me this:

warning: This sequence of operators looks suspiciously like a bug.
 --> src/lib.rs:3:40
  |
3 |     PI * height * (r0 * r0 + r0 * r1 + r1 * r1) / 3.0
  |                                        ^^^^^^^ help: I think you meant: `PI * r1`
  |
  = 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

(playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4dbce29a2cf5bed0efc48214538bea53)


Minimal reproduce:

pub fn false_positive(r0: f32, r1: f32) -> f32 {
    r0 * r0 + r0 * r1 + r1 * r1
}
2 |     r0 * r0 + r0 * r1 + r1 * r1
  |                         ^^^^^^^ help: I think you meant: `r0 * r1`

I don't know how this lint is implemented, but maybe it should only trigger if the number of variables involved is the same as the number of binary expressions?

For instance, this is a true positive:

pub fn true_positive(r0: f32, r1: f32, r2: f32) -> f32 {
    r0 * r0 + r1 * r1 + r2 * r1
}

…although the suggestion is probably not the best:

2 |     r0 * r0 + r1 * r1 + r2 * r1
  |               ^^^^^^^ help: I think you meant: `r0 * r1`

貢獻者指南