-
Notifications
You must be signed in to change notification settings - Fork 1.6k
match_overlapping_arm: Does not seem valid #5986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think the lint is correct here. In the ASCII table A possible fix would be to write the second arm like this: |
I reported this issue because I have generally taken advantage of the fact that any match arm implicitly excludes any patterns that comes before it. I use this a lot in other cases like match n_updates {
Ok(1) => (),
Ok(n_updates) => error!(log, #"poller", "Updating orphan job";
"n_updates" => n_updates),
Err(e) => error!(log, #"poller", "Updating orphan job";
"error" => %e),
} So according to my personal experience I don't see this as an anti-pattern, and I think indeed the proposed fix is not good style. |
Hey, I'd like to contribute to this! I've had the same issue in the past and wonder if this really needs fixing as well or just shouldn't be warn-by-default. Is there anyone that could confirm if this is the way to go? |
Hey @Gadiguibou, I would say that the outcome of the discussion is still not clear :) The example given in the issue that created this lint (#471) has more probability to be an error than to leverage the order of the arms: let x = 5;
match x {
1 ... 10 => println!("1 ... 10"),
5 ... 15 => println!("5 ... 15"),
_ => (),
} IMO including What if the lint checked that the ranges overlap partially, but excluded cases where one pattern is completely included in the other? To make it more graphical; this would be linted:
but this would not be linted:
|
That sounds very nice indeed, the order of the arms would probably have to be considered as well. Or have an entirely new lint for cases where a match arm is unreachable because of overlap with a previous arm. Edit: the simplicity of the implementation is probably something to consider as well. Adding many lints for very similar cases is probably unnecessary and inefficient. |
The compiler will already complain about this ;) Playground I like the suggestion by @ebroto. I think this is the way to go, instead of downgrading the lint 👍 |
Do not lint when range is completely included into another one This fix has been developed following this [comment](#5986 (comment)). So this will be linted: ``` |----------| |-----------| ``` Now this won't be linted: ``` |---| |--------------------| ``` and this will still lint: ``` |--------| |--------------| ``` Fixes: #5986 changelog: none
At least I can't make sense of it:
The code:
Obviously I want the second arm only when the first arm was not matched.
Meta
cargo clippy -V
: clippy 0.0.212 (de521cb 2020-08-21)rustc -Vv
:The text was updated successfully, but these errors were encountered: