Skip to content

Improve the suggestion's syntax in collapsible_match check #13283

@sobolevn

Description

@sobolevn

Description

Today I got a collapsible_match warning in a project I am working on, this was the code it was raising on:

enum Token {
    Name,
    Other,
}

struct Error {
    location: u32,
    token: Option<Token>,
}

fn add_anon_function_hint(err: Option<Error>) {
    if let Some(Error { token, .. }) = err {
        if let Some(Token::Name) = token {
            println!("Name error");
        }
    }
}

It looked like this:

warning: this `if let` can be collapsed into the outer `if let`
  --> compiler-core/src/example.rs:13:9
   |
13 | /         if let Some(Token::Name) = token {
14 | |             println!("Name error");
15 | |         }
   | |_________^
   |
help: the outer pattern can be modified to include the inner pattern
  --> compiler-core/src/example.rs:12:25
   |
12 |     if let Some(Error { token, .. }) = err {
   |                         ^^^^^ replace this binding
13 |         if let Some(Token::Name) = token {
   |                ^^^^^^^^^^^^^^^^^ with this pattern, prefixed by token:
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_match
note: the lint level is defined here
  --> compiler-core/src/lib.rs:2:5
   |
2  |     clippy::all,
   |     ^^^^^^^^^^^
   = note: `#[warn(clippy::collapsible_match)]` implied by `#[warn(clippy::all)]`

Notice this part: with this pattern, prefixed by token:. I was like: is there's something missing in the error message? What kind of token should I prefix it with?

And then I realized that it was really: "with this pattern, prefixed by token:" and it is just a name of the field.

So, I propose to change this and use backticks for separating code from text. Like some other places do:

  • "note: #[warn(clippy::collapsible_match)] implied by #[warn(clippy::all)]"
  • "warning: this if let can be collapsed into the outer if let"

I have a PR ready!

Version

rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: aarch64-apple-darwin
release: 1.80.1
LLVM version: 18.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions