Skip to content

collapsible_match suggestion changes behaviour #6740

@glts

Description

@glts

Lint name: collapsible_match

Clippy suggests collapsing the nested match in the following code snippet (the full code is here):

pub fn focus_error(error: &ConfigError) -> &dyn Error {
    match error {
        ConfigError::ReadConfig(e) => match e {
            ReadConfigError::Parse(e) => e,
            e => e,
        },
        e => e,
    }
}

I expected to see this happen: no Clippy warning

Instead, this happened:

warning: Unnecessary nested match
   --> src/config/read.rs:293:39
    |
293 |           ConfigError::ReadConfig(e) => match e {
    |  _______________________________________^
294 | |             ReadConfigError::Parse(e) => e,
295 | |             e => e,
296 | |         },
    | |_________^
    |
    = note: `#[warn(clippy::collapsible_match)]` on by default
help: The outer pattern can be modified to include the inner pattern.
   --> src/config/read.rs:293:33
    |
293 |         ConfigError::ReadConfig(e) => match e {
    |                                 ^ Replace this binding
294 |             ReadConfigError::Parse(e) => e,
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^ with this pattern
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_match

This suggestion is invalid. It changes the type of the error object is returned and noticeably changes the behaviour of the code (in this case the error message printed to the command-line changes).

Meta

  • cargo clippy -V: clippy 0.0.212 (cb75ad5 2021-02-10)
  • rustc -Vv:
    rustc 1.50.0 (cb75ad5db 2021-02-10)
    binary: rustc
    commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
    commit-date: 2021-02-10
    host: x86_64-unknown-linux-gnu
    release: 1.50.0
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't havegood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions