Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

single_match gives bad suggestion #7038

Closed
VuiMuich opened this issue Apr 5, 2021 · 3 comments · Fixed by #7093
Closed

single_match gives bad suggestion #7038

VuiMuich opened this issue Apr 5, 2021 · 3 comments · Fixed by #7093
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@VuiMuich
Copy link

VuiMuich commented Apr 5, 2021

Clippy gave me a solution, that lead to compiler error error[E0369]: binary operation == cannot be applied to type 'T'
Went to discord for help and bunch of guys unanimously suggested to report this as a bug.

I wrote this simple example below as minimal way to create the same lint.

I tried this code:

struct Mytype {
  some_field: i32,
  another_field: i32,
}

impl Mytype{
  pub fn new() -> Mytype {
        Mytype {
        some_field: 1,
        another_field: 2,
      }
  }
}

fn main() {
  let item: Mytype = Mytype::new();
  let some_vec: Vec<Mytype> = vec![item];

  // clippy complains about this statement
  match some_vec.iter().find(|i| i.some_field < i.another_field ) {
    None => {
      println!("Exit early.");
      return;
    }
    _ => (),
  }

  // clippy suggests this solution which will result in an [E0369]
  // if some_vec.iter().find(|i| i.some_field < i.another_field ) == None {
  // println!("Exit early:");
  // return;
  // }

  // clippy should suggest a statement like this
  if some_vec.iter().any(|i| i.some_field > i.another_field ) {
      println!("Exit early.");
      return;
  }

  println!("proper End")
}

This is the original lint to above example:

# cargo clippy --all-targets --all-features
warning: you seem to be trying to use `match` for an equality check. Consider using `if`
  --> src/main.rs:20:3
   |
20 | /   match some_vec.iter().find(|i| i.some_field < i.another_field ) {
21 | |     None => {
22 | |       println!("Exit early.");
23 | |       return;
24 | |     }
25 | |     _ => (),
26 | |   }
   | |___^
   |
   = note: `#[warn(clippy::single_match)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match
help: try this
   |
20 |   if some_vec.iter().find(|i| i.some_field < i.another_field ) == None {
21 |     println!("Exit early.");
22 |     return;
23 |   }
   |

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.01s

And the original error when you uncomment the suggested code

   Compiling report_clippy_bug v0.1.0 (/home/vuimuich/Documents/report_clippy_bug)
error[E0369]: binary operation `==` cannot be applied to type `Option<&Mytype>`
  --> src/main.rs:29:64
   |
29 |   if some_vec.iter().find(|i| i.some_field < i.another_field ) == None {
   |      --------------------------------------------------------- ^^ ---- Option<_>
   |      |
   |      Option<&Mytype>

error: aborting due to previous error

For more information about this error, try `rustc --explain E0369`.
error: could not compile `report_clippy_bug`

To learn more, run the command again with --verbose.

Meta

  • cargo clippy -V: clippy 0.1.51 (2fd73fa 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-unknown-linux-gnu
    release: 1.51.0
    LLVM version: 11.0.1
    
@VuiMuich VuiMuich added the C-bug Category: Clippy is not doing the correct thing label Apr 5, 2021
@giraffate
Copy link
Contributor

Thanks for the report!

Clippy gave me a solution, that lead to compiler error error[E0369]: binary operation == cannot be applied to type 'T'

I couldn't reproduce a compile error in the example you show. FYI I tried this: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f1c072709e6bdc249d442ae1b812a84d

@VuiMuich
Copy link
Author

VuiMuich commented Apr 6, 2021

Oh, then simplifying to a Vec was probably a bad idea.

The original error occurred with a custom type Window. Gonna try to update the example code so it breaks the same way it did originally.

@VuiMuich
Copy link
Author

VuiMuich commented Apr 9, 2021

Updated original post with modified code and matching clippy lint and error with uncommented suggested code.

Apologies if this isn't the most minimal possible example, but I'm still relatively fresh to rust..

@giraffate giraffate added I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied good-first-issue These issues are a good way to get started with Clippy labels Apr 12, 2021
@bors bors closed this as completed in ddc2598 Apr 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants