Skip to content
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

Add a special case error for [Range<T>; 1] #23141

Closed
reem opened this issue Mar 7, 2015 · 3 comments · Fixed by #54946
Closed

Add a special case error for [Range<T>; 1] #23141

reem opened this issue Mar 7, 2015 · 3 comments · Fixed by #54946
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@reem
Copy link
Contributor

reem commented Mar 7, 2015

[Range<T>; 1] is usually a mistake and often the source of type errors where Range<T> was meant, since you get it with [a..b], which is tempting from the syntax.

@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label Mar 7, 2015
@Thiez
Copy link
Contributor

Thiez commented Mar 7, 2015

In theory &[0..10] could be correct in a for loop...

When using this program:

fn main() {
    for r in [0..10] {
        println!("{:?}", r);
    }
}

the current error message appears to be

error: the trait `core::iter::Iterator` is not implemented for the type `[core::ops::Range<_>; 1]
note: `[core::ops::Range<_>; 1]` is not an iterator; maybe try calling `.iter()` or a similar method

Surely that is understandable ("Why is the iterator in a length-1 array? Oh, better remove the []").
What bothers me more is that both messages show up three times, and count as 3 errors...

@steveklabnik
Copy link
Member

Triage: Same error, but only one time. It could be a bit better, probably.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@clarfonthey
Copy link
Contributor

Shouldn't this be a clippy issue?

estebank added a commit to estebank/rust that referenced this issue Oct 10, 2018
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (rust-lang#23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (rust-lang#36391)
 - Suggest using range literal when iterating on integers (rust-lang#34353)
 - Do not suggest `.iter()` by default (rust-lang#50773, rust-lang#46806)
bors added a commit that referenced this issue Oct 15, 2018
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors

 - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766)
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (Fix #23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (Fix #36391)
 - Suggest using range literal when iterating on integers (Fix #34353)
 - Do not suggest `.iter()` by default (Fix #50773, fix #46806)
 - Add regression test (Fix #22872)
bors added a commit that referenced this issue Oct 17, 2018
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors

 - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766)
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (Fix #23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (Fix #36391)
 - Suggest using range literal when iterating on integers (Fix #34353)
 - Do not suggest `.iter()` by default (Fix #50773, fix #46806)
 - Add regression test (Fix #22872)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants