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

Suggest using ranges in loops on primitive types, instead of calling .iter() #34353

Closed
alexandermerritt opened this issue Jun 19, 2016 · 3 comments · Fixed by #54946
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@alexandermerritt
Copy link
Contributor

alexandermerritt commented Jun 19, 2016

The following code results in output that isn't specifically helpful:

fn main() {
    let max: usize = 10;
    for m in max { ; }
}

Compiler output:

error: the trait bound `usize: std::iter::Iterator` is not satisfied [--explain E0277]
 --> <anon>:3:5
3 |>     for m in max {
  |>     ^
note: `usize` is not an iterator; maybe try calling `.iter()` or a similar method
note: required by `std::iter::IntoIterator::into_iter`

Primitive types do not [currently] implement Iterator. Perhaps the output could see the type is primitive and suggest to create a type that implements Iterator, such as a range with 0..max?

Meta

rustc 1.11.0-nightly (bb4a79b08 2016-06-15)
binary: rustc
commit-hash: bb4a79b087158f396b984bdf552d2c90890b12a3
commit-date: 2016-06-15
host: x86_64-unknown-linux-gnu
release: 1.11.0-nightly
@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 19, 2016
@Aatch
Copy link
Contributor

Aatch commented Jun 21, 2016

That error message is already somewhat special, but it uses a special attribute on the trait itself. This would require actually special-casing Iterator in this case, and I'm not sure how useful that would be for the effort. I'm skeptical that this kind of thing is 1) particularly common and 2) something that somebody would deliberately do. What I mean by that second point is that there's a difference between doing for m in max and going "oh I meant for m in 0..max and doing that expecting it to work.

@alexandermerritt
Copy link
Contributor Author

@Aatch These are valid points. I agree that if not trivial to implement, it doesn't make sense given this is probably a rare occurrence.

Mainly I was thinking the easiest could be expanding the already existing hint (hard-coded string?) to perhaps be maybe try calling .iter() or create a Range if this is a primitive type with 0..max which avoids adding any special-case handling internally.

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@estebank
Copy link
Contributor

Once #47613 gets merged, this can be trivially added.

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