Rust: rewrite `...` to `..=` as an idiom lint for Rust 2018 edition

Created on 24 May 2018  路  10Comments  路  Source: rust-lang/rust

Current status

Implemented in https://github.com/rust-lang/rust/pull/51149 but currently not marked as automatically applicable due to this issue which is explained below

A-edition-2018-lints A-rust-2018-preview C-enhancement P-high T-compiler WG-epoch

Most helpful comment

One remaining issue of a..=b pattern is #48501. When stabilizing a..=b, to avoid the confusing interpretation of &a...b, in the following example:

fn main() {
    match &5 {
        &3...6 => {}
        _ => {}
    }
}

if we just s/.../..=/g the code will error with

error: the range pattern here has ambiguous interpretation
 --> src/main.rs:3:10
  |
3 |         &3..=6 => {}
  |          ^^^^^ help: add parentheses to clarify the precedence: `(3 ..=6)`

Note that pattern_parentheses (#48500) is still unstable 馃檭. However, given that default binding modes (#42640) is already stable, an even more idiomatic fix would probably be

fn main() {
    match &5 {
        3..=6 => {}   // no &, no (), no whatever
        _ => {}
    }
}

All 10 comments

Do we have an existing lint for this?

If we hope to someday get rid of ... in patterns (the current description of #28237 describes it as "(silently) deprecated"), wouldn't we want a future-incompatible lint rather than an idiom lint? (... in expressions is already illegal.)

One remaining issue of a..=b pattern is #48501. When stabilizing a..=b, to avoid the confusing interpretation of &a...b, in the following example:

fn main() {
    match &5 {
        &3...6 => {}
        _ => {}
    }
}

if we just s/.../..=/g the code will error with

error: the range pattern here has ambiguous interpretation
 --> src/main.rs:3:10
  |
3 |         &3..=6 => {}
  |          ^^^^^ help: add parentheses to clarify the precedence: `(3 ..=6)`

Note that pattern_parentheses (#48500) is still unstable 馃檭. However, given that default binding modes (#42640) is already stable, an even more idiomatic fix would probably be

fn main() {
    match &5 {
        3..=6 => {}   // no &, no (), no whatever
        _ => {}
    }
}

@zackmdavis

If we hope to someday get rid of ... in patterns (the current description of #28237 describes it as "(silently) deprecated"), wouldn't we want a future-incompatible lint rather than an idiom lint? (... in expressions is already illegal.)

I classified it as an idiom lint because the code will not cause a hard error in the new edition, even if it is frowned upon.

@zackmdavis if you've got a moment, can you write up a listing of what's left after #51149 landed?

if you've got a moment, can you write up a listing of what's left after #51149 landed?

@alexcrichton: the comment above by @kennytm should still be addressed; the lint suggestion is marked as maybe-incorrect for this reason. If pattern-parentheses are stabilized (which itself reportedly needs more testing), using them for a correct suggestion when needed will be very easy.

Ok great, thanks!

visited for T-compiler triage. It seems like it was blocked on stabilization of the pattern-parentheses feature. So what are the next steps, and who wants to own this?

more triage notes: P-high. @varkor has agreed to take this on and/or mentor it.

Removing from the milestone, as idiom lints are not part of the Rust 2018 release.

Was this page helpful?
0 / 5 - 0 ratings