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

redundant_pattern_matching fixit suggestion fails to apply #4344

Closed
matthiaskrgr opened this issue Aug 6, 2019 · 4 comments
Closed

redundant_pattern_matching fixit suggestion fails to apply #4344

matthiaskrgr opened this issue Aug 6, 2019 · 4 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@matthiaskrgr
Copy link
Member

Note: tools are broken since some time now which is why I'm using a fairly outdated version of clippy: clippy 0.0.212 (c3e9136 2019-07-30)

code:

fn main() {
    let foo = if let None = a() {
        println!("hi");
        a()
    } else {
        Some(3)
    };
}

fn a() -> Option<u32> {
    Some(3)
}

cargo fix fails to apply clippys suggestion:

warning: redundant pattern matching, consider using `is_none()`
 --> src/main.rs:2:22
  |
2 |       let foo = if let None = a() {
  |  _______________-      ^^^^
3 | |         println!("hi");
4 | |         a()
5 | |     } else {
6 | |         Some(3)
7 | |     };
  | |_____- help: try this: `if a().is_none()`
  |
  = note: `#[warn(clippy::redundant_pattern_matching)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

If I run cargo fix -Z unstable-options --clippy, I get an error:

warning: failed to automatically apply fixes suggested by rustc to crate `b`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected `{`, found `;`
 --> src/main.rs:2:32
  |
2 |     let _foo = if a().is_none();
  |                --              ^ expected `{`
  |                |
  |                this `if` statement has a condition, but no block

error: aborting due to previous error

Original diagnostics will follow.

warning: unused variable: `foo`
 --> src/main.rs:2:9
  |
2 |     let foo = if let None = a() {
  |         ^^^ help: consider prefixing with an underscore: `_foo`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: redundant pattern matching, consider using `is_none()`
 --> src/main.rs:2:22
  |
2 |       let foo = if let None = a() {
  |  _______________-      ^^^^
3 | |         println!("hi");
4 | |         a()
5 | |     } else {
6 | |         Some(3)
7 | |     };
  | |_____- help: try this: `if a().is_none()`
  |
  = note: `#[warn(clippy::redundant_pattern_matching)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

warning: use of a blacklisted/placeholder name `foo`
 --> src/main.rs:2:9
  |
2 |     let foo = if let None = a() {
  |         ^^^
  |
  = note: `#[warn(clippy::blacklisted_name)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name

warning: unused variable: `foo`
 --> src/main.rs:2:9
  |
2 |     let foo = if let None = a() {
  |         ^^^ help: consider prefixing with an underscore: `_foo`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: redundant pattern matching, consider using `is_none()`
 --> src/main.rs:2:22
  |
2 |       let foo = if let None = a() {
  |  _______________-      ^^^^
3 | |         println!("hi");
4 | |         a()
5 | |     } else {
6 | |         Some(3)
7 | |     };
  | |_____- help: try this: `if a().is_none()`
  |
  = note: `#[warn(clippy::redundant_pattern_matching)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

warning: use of a blacklisted/placeholder name `foo`
 --> src/main.rs:2:9
  |
2 |     let foo = if let None = a() {
  |         ^^^
  |
  = note: `#[warn(clippy::blacklisted_name)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name

    Finished dev [unoptimized + debuginfo] target(s) in 0.68s
@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Aug 6, 2019
@matthiaskrgr
Copy link
Member Author

matthiaskrgr commented Aug 7, 2019

I believe this is the same underlying error:

fn main() {
    let _ = has_flags();
}

fn has_flags() -> bool {
    let bla: Result<i32, i32> = Ok(4);
    if let Ok(_) = bla {
        true
    } else {
        false
    }
}

clippy suggests

warning: redundant pattern matching, consider using `is_ok()`
  --> src/main.rs:7:12
   |
7  |       if let Ok(_) = bla {
   |  _____-      ^^^^^
8  | |         true
9  | |     } else {
10 | |         false
11 | |     }
   | |_____- help: try this: `if bla.is_ok()`
   |
   = note: `#[warn(clippy::redundant_pattern_matching)]` on by default

It seems that clippy replaces the entire span with bla.is_ok() which does not work, we probably want to remove the preceeding "if" as well?

fn has_flags() -> bool {
    let bla: Result<i32, i32> = Ok(4);
    if bla.is_ok()
}

The code above does not compile for obvious reasons... :/

@phansch phansch self-assigned this Aug 7, 2019
@phansch

This comment has been minimized.

phansch added a commit to phansch/rust-clippy that referenced this issue Aug 7, 2019
Fixes the problem displayed in rust-lang#4344 (comment).

We now append `{}` to the suggestion so that the conditional has the
correct syntax again.

(If we were to _remove_ the `if` instead, it would trigger the
`unused_must_use` warning for `#[must_use]` types.
phansch added a commit to phansch/rust-clippy that referenced this issue Aug 8, 2019
Fixes the problem displayed in rust-lang#4344 (comment).

We now append `{}` to the suggestion so that the conditional has the
correct syntax again.

(If we were to _remove_ the `if` instead, it would trigger the
`unused_must_use` warning for `#[must_use]` types.
phansch added a commit to phansch/rust-clippy that referenced this issue Aug 21, 2019
Fixes the problem displayed in rust-lang#4344 (comment).

We now append `{}` to the suggestion so that the conditional has the
correct syntax again.

(If we were to _remove_ the `if` instead, it would trigger the
`unused_must_use` warning for `#[must_use]` types.
@flip1995
Copy link
Member

The suggestions were improved in #4352, but there is still room for more improvements:

  1. (https://github.com/rust-lang/rust-clippy/pull/4352/files#r312354699) applying the suggestions results in a syntax error, since the ; is missing sometimes. There are 3 cases that I can think of:
    • on a let _ = if ...; the ; is already there,
    • on a returning if the ; shouldn't be added,
    • and on a stray if, it has to be added.
  2. (https://github.com/rust-lang/rust-clippy/pull/4352/files#r312355415) The suggestion is semantically wrong in some cases and the returned type of the if/match statement has to be accounted for.

bors added a commit that referenced this issue Aug 21, 2019
Fix some suggestions for redundant_pattern_matching

.. and change the Applicability to `MaybeIncorrect`.

Fixes the problem displayed in #4344 (comment).

We now append `{}` to the suggestion so that the conditional has the
correct syntax again.

(If we were to _remove_ the `if` instead, it would trigger the
`unused_must_use` warning for `#[must_use]` types.)

changelog: Fix some suggestions for `redundant_pattern_matching`
@phansch phansch added the L-suggestion Lint: Improving, adding or fixing lint suggestions label Aug 24, 2019
@phansch phansch removed their assignment Aug 24, 2019
@flip1995
Copy link
Member

Fixed in #5483

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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

3 participants