Skip to content

match_single_binding is false positive on temporaries  #6614

Open
@MikailBag

Description

@MikailBag

Lint name: match_single_binding

I tried this code:

fn main() {
    match format_args!("hello {}", "world") {
        f => dbg!(f),
    };
}

(playground link)

Clippy prints a warning:

warning: this match could be written as a `let` statement

With a proposal:

let f = format_args!("hello {}", "world");
dbg!(f);

But suggested code does not compile, because f is bound to a temporary value and its lifetime ends before dbg!.

Meta

  • cargo clippy -V: 0.1.51 (2021-01-20 a4cbb44)
  • rustc -Vv:
rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-unknown-linux-gnu
release: 1.49.0

Activity

added
C-bugCategory: Clippy is not doing the correct thing
I-false-positiveIssue: The lint was triggered on code it shouldn't have
on Jan 21, 2021
added
E-mediumCall for participation: Medium difficulty level problem and requires some initial experience.
T-middleType: Probably requires verifiying types
on Jan 26, 2021
camsteffen

camsteffen commented on Feb 5, 2021

@camsteffen
Contributor

I tried to fix this with HIR and ScopeTree to no avail. I think this will require MIR analysis. Of course we could special-case format_args! but I think that is too rare of a use-case for that.

added
T-MIRType: This lint will require working with the MIR
E-hardCall for participation: This a hard problem and requires more experience or effort to work on
and removed
T-middleType: Probably requires verifiying types
E-mediumCall for participation: Medium difficulty level problem and requires some initial experience.
on Feb 5, 2021
Fishrock123

Fishrock123 commented on Apr 12, 2021

@Fishrock123
Contributor

Just ran into this with todo!(), which seems very silly:

warning: this match could be replaced by its body itself
  --> src/thing.rs:19:9
   |
19 | /         match event.action() {
20 | |             _ => todo!("make thing do"),
21 | |         }
   | |_________^ help: consider using the match body instead: `todo!("make thing do")`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding
camsteffen

camsteffen commented on Apr 12, 2021

@camsteffen
Contributor

I don't think it would be worth it to special-case todo! since that indicates incomplete code by definition.

Fishrock123

Fishrock123 commented on Apr 13, 2021

@Fishrock123
Contributor

Oh, on re-reading this maybe this is a separate issue. I think it should not warn in the case of todo!. I think it would be worth it, that should be highlighted in different ways.

bgeron

bgeron commented on Apr 1, 2022

@bgeron

I just ran into the same issue.

To be honest — matching with a single match arm is a bit silly, let is normally meant for that. And if you put a block in the match, now you have two indentation levels!

match format_args!("hello {}", "world") {
    f => {
        dbg!(f),
        dbg!(f),
    }
}

It seems nice to have different syntax for this use case. For instance:

let f = format_args!("hello {}", "world") in {
    dbg!(f),
    dbg!(f),
};

But the lint needs a fix irrespective of any new syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedT-MIRType: This lint will require working with the MIR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bgeron@Fishrock123@camsteffen@MikailBag@giraffate

        Issue actions

          match_single_binding is false positive on temporaries · Issue #6614 · rust-lang/rust-clippy