Skip to content

Commit

Permalink
Reword rustc_on_unimplemented errors for Iterator
Browse files Browse the repository at this point in the history
 - 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)
  • Loading branch information
estebank committed Oct 10, 2018
1 parent cd7c818 commit 5b0223e
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 42 deletions.
21 changes: 20 additions & 1 deletion src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,30 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item=()>) {}
/// [impl]: index.html#implementing-iterator
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(
on(
_Self="[std::ops::Range<Idx>; 1]",
label="if you meant to iterate between two values, remove the square brackets",
note="`[start..end]` is an array of one `Range`, you might have meant to have a `Range`: `start..end`"
),
on(
_Self="&str",
label="`{Self}` is not an iterator; try calling `.chars()` or `.bytes()`"
),
label="`{Self}` is not an iterator; maybe try calling `.iter()` or a similar method"
on(
_Self="std::string::String",
label="`{Self}` is not an iterator; try calling `.chars()` or `.bytes()`"
),
on(
_Self="[]",
label="borrow the array with `&` or call `.iter()` on it to iterate over it",
note="arrays are not an iterators, but slices like the following are: `&[1, 2, 3]`"
),
on(
_Self="{integral}",
note="if you want to iterate between `0` until a value `end`, use the range syntax: `0..end`"
),
label="`{Self}` is not an iterator",
message="`{Self}` is not an iterator"
)]
#[doc(spotlight)]
pub trait Iterator {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/conservative_impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// #39872, #39553

fn will_ice(something: &u32) -> impl Iterator<Item = &u32> {
//~^ ERROR the trait bound `(): std::iter::Iterator` is not satisfied [E0277]
//~^ ERROR `()` is not an iterator
}

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/conservative_impl_trait.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the trait bound `(): std::iter::Iterator` is not satisfied
error[E0277]: `()` is not an iterator
--> $DIR/conservative_impl_trait.rs:13:33
|
LL | fn will_ice(something: &u32) -> impl Iterator<Item = &u32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not an iterator; maybe try calling `.iter()` or a similar method
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `()`
= note: the return type of a function must have a statically known size
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/feature-gates/feature-gate-trivial_bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ LL | | }
= help: see issue #48214
= help: add #![feature(trivial_bounds)] to the crate attributes to enable

error[E0277]: the trait bound `i32: std::iter::Iterator` is not satisfied
error[E0277]: `i32` is not an iterator
--> $DIR/feature-gate-trivial_bounds.rs:50:1
|
LL | / fn use_for() where i32: Iterator { //~ ERROR
LL | | for _ in 2i32 {}
LL | | }
| |_^ `i32` is not an iterator; maybe try calling `.iter()` or a similar method
| |_^ `i32` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `i32`
= note: if you want to iterate between `0` until a value `end`, use the range syntax: `0..end`
= help: see issue #48214
= help: add #![feature(trivial_bounds)] to the crate attributes to enable

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/for/for-c-in-str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

fn main() {
for c in "asdf" {
//~^ ERROR the trait bound `&str: std::iter::Iterator` is not satisfied
//~^ ERROR `&str` is not an iterator
//~| NOTE `&str` is not an iterator
//~| HELP the trait `std::iter::Iterator` is not implemented for `&str`
//~| NOTE required by `std::iter::IntoIterator::into_iter`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/for/for-c-in-str.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0277]: the trait bound `&str: std::iter::Iterator` is not satisfied
error[E0277]: `&str` is not an iterator
--> $DIR/for-c-in-str.rs:14:14
|
LL | for c in "asdf" {
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/for/for-loop-bogosity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub fn main() {
x: 1,
y: 2,
};
for x in bogus { //~ ERROR `MyStruct: std::iter::Iterator` is not satisfied
for x in bogus {
//~^ ERROR `MyStruct` is not an iterator
drop(x);
}
}
6 changes: 3 additions & 3 deletions src/test/ui/for/for-loop-bogosity.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the trait bound `MyStruct: std::iter::Iterator` is not satisfied
error[E0277]: `MyStruct` is not an iterator
--> $DIR/for-loop-bogosity.rs:27:14
|
LL | for x in bogus { //~ ERROR `MyStruct: std::iter::Iterator` is not satisfied
| ^^^^^ `MyStruct` is not an iterator; maybe try calling `.iter()` or a similar method
LL | for x in bogus {
| ^^^^^ `MyStruct` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `MyStruct`
= note: required by `std::iter::IntoIterator::into_iter`
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/issues/issue-28098.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

fn main() {
let _ = Iterator::next(&mut ());
//~^ ERROR `(): std::iter::Iterator` is not satisfied
//~^ ERROR `()` is not an iterator

for _ in false {}
//~^ ERROR `bool: std::iter::Iterator` is not satisfied
//~^ ERROR `bool` is not an iterator

let _ = Iterator::next(&mut ());
//~^ ERROR `(): std::iter::Iterator` is not satisfied
//~^ ERROR `()` is not an iterator

other()
}
Expand All @@ -25,11 +25,11 @@ pub fn other() {
// check errors are still reported globally

let _ = Iterator::next(&mut ());
//~^ ERROR `(): std::iter::Iterator` is not satisfied
//~^ ERROR `()` is not an iterator

let _ = Iterator::next(&mut ());
//~^ ERROR `(): std::iter::Iterator` is not satisfied
//~^ ERROR `()` is not an iterator

for _ in false {}
//~^ ERROR `bool: std::iter::Iterator` is not satisfied
//~^ ERROR `bool` is not an iterator
}
24 changes: 12 additions & 12 deletions src/test/ui/issues/issue-28098.stderr
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
error[E0277]: the trait bound `(): std::iter::Iterator` is not satisfied
error[E0277]: `()` is not an iterator
--> $DIR/issue-28098.rs:12:13
|
LL | let _ = Iterator::next(&mut ());
| ^^^^^^^^^^^^^^ `()` is not an iterator; maybe try calling `.iter()` or a similar method
| ^^^^^^^^^^^^^^ `()` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `()`
= note: required by `std::iter::Iterator::next`

error[E0277]: the trait bound `bool: std::iter::Iterator` is not satisfied
error[E0277]: `bool` is not an iterator
--> $DIR/issue-28098.rs:15:14
|
LL | for _ in false {}
| ^^^^^ `bool` is not an iterator; maybe try calling `.iter()` or a similar method
| ^^^^^ `bool` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `bool`
= note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: the trait bound `(): std::iter::Iterator` is not satisfied
error[E0277]: `()` is not an iterator
--> $DIR/issue-28098.rs:18:13
|
LL | let _ = Iterator::next(&mut ());
| ^^^^^^^^^^^^^^ `()` is not an iterator; maybe try calling `.iter()` or a similar method
| ^^^^^^^^^^^^^^ `()` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `()`
= note: required by `std::iter::Iterator::next`

error[E0277]: the trait bound `(): std::iter::Iterator` is not satisfied
error[E0277]: `()` is not an iterator
--> $DIR/issue-28098.rs:27:13
|
LL | let _ = Iterator::next(&mut ());
| ^^^^^^^^^^^^^^ `()` is not an iterator; maybe try calling `.iter()` or a similar method
| ^^^^^^^^^^^^^^ `()` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `()`
= note: required by `std::iter::Iterator::next`

error[E0277]: the trait bound `(): std::iter::Iterator` is not satisfied
error[E0277]: `()` is not an iterator
--> $DIR/issue-28098.rs:30:13
|
LL | let _ = Iterator::next(&mut ());
| ^^^^^^^^^^^^^^ `()` is not an iterator; maybe try calling `.iter()` or a similar method
| ^^^^^^^^^^^^^^ `()` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `()`
= note: required by `std::iter::Iterator::next`

error[E0277]: the trait bound `bool: std::iter::Iterator` is not satisfied
error[E0277]: `bool` is not an iterator
--> $DIR/issue-28098.rs:33:14
|
LL | for _ in false {}
| ^^^^^ `bool` is not an iterator; maybe try calling `.iter()` or a similar method
| ^^^^^ `bool` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `bool`
= note: required by `std::iter::IntoIterator::into_iter`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-50480.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
//~^ ERROR the trait `Copy` may not be implemented for this type
struct Foo(NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
//~^ ERROR cannot find type `NotDefined` in this scope
//~| ERROR the trait bound `i32: std::iter::Iterator` is not satisfied
//~| ERROR `i32` is not an iterator

fn main() {}
5 changes: 3 additions & 2 deletions src/test/ui/issues/issue-50480.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ error[E0412]: cannot find type `NotDefined` in this scope
LL | struct Foo(NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
| ^^^^^^^^^^ not found in this scope

error[E0277]: the trait bound `i32: std::iter::Iterator` is not satisfied
error[E0277]: `i32` is not an iterator
--> $DIR/issue-50480.rs:13:24
|
LL | struct Foo(NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
| ^^^^^^^^^^^^^^^^^^^^^^^ `i32` is not an iterator; maybe try calling `.iter()` or a similar method
| ^^^^^^^^^^^^^^^^^^^^^^^ `i32` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `i32`
= note: if you want to iterate between `0` until a value `end`, use the range syntax: `0..end`

error[E0204]: the trait `Copy` may not be implemented for this type
--> $DIR/issue-50480.rs:11:17
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/suggest-remove-refs-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
let v = vec![0, 1, 2, 3];

for (i, n) in &v.iter().enumerate() {
//~^ ERROR the trait bound
//~^ ERROR `&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator
println!("{}", i);
}
}
4 changes: 2 additions & 2 deletions src/test/ui/suggestions/suggest-remove-refs-1.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0277]: the trait bound `&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>: std::iter::Iterator` is not satisfied
error[E0277]: `&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator
--> $DIR/suggest-remove-refs-1.rs:14:19
|
LL | for (i, n) in &v.iter().enumerate() {
| -^^^^^^^^^^^^^^^^^^^^
| |
| `&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator; maybe try calling `.iter()` or a similar method
| `&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator
| help: consider removing 1 leading `&`-references
|
= help: the trait `std::iter::Iterator` is not implemented for `&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/suggest-remove-refs-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
let v = vec![0, 1, 2, 3];

for (i, n) in & & & & &v.iter().enumerate() {
//~^ ERROR the trait bound
//~^ ERROR `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator
println!("{}", i);
}
}
4 changes: 2 additions & 2 deletions src/test/ui/suggestions/suggest-remove-refs-2.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0277]: the trait bound `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>: std::iter::Iterator` is not satisfied
error[E0277]: `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator
--> $DIR/suggest-remove-refs-2.rs:14:19
|
LL | for (i, n) in & & & & &v.iter().enumerate() {
| ---------^^^^^^^^^^^^^^^^^^^^
| |
| `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator; maybe try calling `.iter()` or a similar method
| `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator
| help: consider removing 5 leading `&`-references
|
= help: the trait `std::iter::Iterator` is not implemented for `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/suggest-remove-refs-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
& &v
.iter()
.enumerate() {
//~^^^^ ERROR the trait bound
//~^^^^ ERROR `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an
println!("{}", i);
}
}
4 changes: 2 additions & 2 deletions src/test/ui/suggestions/suggest-remove-refs-3.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0277]: the trait bound `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>: std::iter::Iterator` is not satisfied
error[E0277]: `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator
--> $DIR/suggest-remove-refs-3.rs:14:19
|
LL | for (i, n) in & & &
Expand All @@ -9,7 +9,7 @@ LL | || & &v
| ||___________- help: consider removing 5 leading `&`-references
LL | | .iter()
LL | | .enumerate() {
| |_____________________^ `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator; maybe try calling `.iter()` or a similar method
| |_____________________^ `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `&&&&&std::iter::Enumerate<std::slice::Iter<'_, {integer}>>`
= note: required by `std::iter::IntoIterator::into_iter`
Expand Down

0 comments on commit 5b0223e

Please sign in to comment.