Skip to content

collapsible_if fix suggestion breaks in the presence of match! #9375

@mwkmwkmwk

Description

@mwkmwkmwk

Summary

When the outer if condition involves a match! (but the match! is not the whole condition), the collapsible_if fix suggestion will replace it with the macro definition (quite badly), completely breaking the output.

Reproducer

I tried this code:

pub enum Kind { Cat, Dog, } pub fn meow(kind: Kind, meowy: bool, awake: bool) { if matches!(kind, Kind::Cat) && meowy { if awake { println!("MEOW"); } } }

I expected to see this happen:

suggestion changing the function body to:

 if matches!(kind, Kind::Cat) && meowy && awake { println!("MEOW"); }

Instead, this happened:

warning: this `if` statement can be collapsed --> src/lib.rs:7:5 | 7 | / if matches!(kind, Kind::Cat) && meowy { 8 | | if awake { 9 | | println!("MEOW"); 10 | | } 11 | | } | |_____^ | = note: `#[warn(clippy::collapsible_if)]` on by default = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if help: collapse nested if block | 7 ~ if match $expression { 8 + $( $pattern )|+ $( if $guard )? => true, 9 + _ => false 10 + } && meowy && awake { 11 + println!("MEOW"); 12 + } |

When used with --fix, this results in an error asking for a bug report.

Version

rustc 1.65.0-nightly (addacb587 2022-08-24) binary: rustc commit-hash: addacb5878b9970ebc1665768a05cb601e7aea15 commit-date: 2022-08-24 host: x86_64-unknown-linux-gnu release: 1.65.0-nightly LLVM version: 15.0.0 

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions