Skip to content

Perhaps this match_bool case could be considered idiomatic? #3713

Closed
@swfsql

Description

@swfsql

Hello, I have a suggestion for the match_bool lint
https://rust-lang.github.io/rust-clippy/master/index.html#match_bool

I'm not sure if this is idiomatic, buy I often attach (@) matched values into variables, eg.:

match some_hash_set.insert(some_key) {
        _previously_unset @ true => (),
        _previously_unset @ false => {
                 panic!("D'oh!")
        }
}

I actually prefer reading this interface like this, because I can kind of simultaneously see both branches (the "total" effect of each branch) and I kind of know beforehand that such value (_previously_unset) won't be used anywhere else, for anything else.


The "literal" translation for the if-else case would be:

{ // inner scope
        let previously_unset = some_hash_set.insert(some_key);
        if (!previously_unset) {
                panic!("D'oh!")
        }
}

But in this case, to see the "total" effect of previously_unset @ true, I'd have to look past the if statement and see where the variable would still be used (in which, in this case, would be nowhere.. since the scope ended).


Both cases have 6 lines, and the first one is slightly more verbose. But I actually am more comfortable at reasoning about the first case - it feels familiar, as if the function returned an enum of two cases..

so... this is a suggestion and perhaps you could agree with!
Thanks in advance,

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