diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index ff1bd71bd55..5b6d9e2033c 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -40,13 +40,16 @@ fn _assert_is_object_safe(_: &dyn Iterator) {} _Self="[std::ops::RangeFrom; 1]", label="if you meant to iterate from a value onwards, remove the square brackets", note="`[start..]` is an array of one `RangeFrom`; you might have meant to have a \ - `RangeFrom` without the brackets: `start..`" + `RangeFrom` without the brackets: `start..`, keeping in mind that iterating over an \ + unbounded iterator will run forever unless you `break` or `return` from within the \ + loop" ), on( _Self="[std::ops::RangeTo; 1]", - label="if you meant to iterate until a value, remove the square brackets", - note="`[..end]` is an array of one `RangeTo`; you might have meant to have a \ - `RangeTo` without the brackets: `..end`" + label="if you meant to iterate until a value, remove the square brackets and add a \ + starting value", + note="`[..end]` is an array of one `RangeTo`; you might have meant to have a bounded \ + `Range` without the brackets: `0..end`" ), on( _Self="[std::ops::RangeInclusive; 1]", @@ -56,9 +59,22 @@ fn _assert_is_object_safe(_: &dyn Iterator) {} ), on( _Self="[std::ops::RangeToInclusive; 1]", - label="if you meant to iterate until a value, remove the square brackets", + label="if you meant to iterate until a value (including it), remove the square brackets \ + and add a starting value", note="`[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a \ - `RangeToInclusive` without the brackets: `..=end`" + bounded `RangeInclusive` without the brackets: `0..=end`" + ), + on( + _Self="std::ops::RangeTo", + label="if you meant to iterate until a value, add a starting value", + note="`..end` is a `RangeTo`, which cannot be iterated on; you might have meant to have a \ + bounded `Range`: `0..end`" + ), + on( + _Self="std::ops::RangeToInclusive", + label="if you meant to iterate until a value (including it), add a starting value", + note="`..=end` is a `RangeToInclusive`, which cannot be iterated on; you might have meant \ + to have a bounded `RangeInclusive`: `0..=end`" ), on( _Self="&str", diff --git a/src/test/ui/iterators/array-of-ranges.rs b/src/test/ui/iterators/array-of-ranges.rs index eb2309f7a3f..a7d6e80bae5 100644 --- a/src/test/ui/iterators/array-of-ranges.rs +++ b/src/test/ui/iterators/array-of-ranges.rs @@ -1,9 +1,14 @@ fn main() { for _ in [0..1] {} + for _ in [0..=1] {} + for _ in [0..] {} + for _ in [..1] {} + for _ in [..=1] {} let start = 0; let end = 0; for _ in [start..end] {} let array_of_range = [start..end]; for _ in array_of_range {} for _ in [0..1, 2..3] {} + for _ in [0..=1] {} } diff --git a/src/test/ui/iterators/array-of-ranges.stderr b/src/test/ui/iterators/array-of-ranges.stderr index 073baab67b2..fbe7e0ee748 100644 --- a/src/test/ui/iterators/array-of-ranges.stderr +++ b/src/test/ui/iterators/array-of-ranges.stderr @@ -8,9 +8,49 @@ LL | for _ in [0..1] {} = note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end` = note: required by `std::iter::IntoIterator::into_iter` -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator +error[E0277]: `[std::ops::RangeInclusive<{integer}>; 1]` is not an iterator + --> $DIR/array-of-ranges.rs:3:14 + | +LL | for _ in [0..=1] {} + | ^^^^^^^ if you meant to iterate between two values, remove the square brackets + | + = help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeInclusive<{integer}>; 1]` + = note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end` + = note: required by `std::iter::IntoIterator::into_iter` + +error[E0277]: `[std::ops::RangeFrom<{integer}>; 1]` is not an iterator + --> $DIR/array-of-ranges.rs:4:14 + | +LL | for _ in [0..] {} + | ^^^^^ if you meant to iterate from a value onwards, remove the square brackets + | + = help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeFrom<{integer}>; 1]` + = note: `[start..]` is an array of one `RangeFrom`; you might have meant to have a `RangeFrom` without the brackets: `start..`, keeping in mind that iterating over an unbounded iterator will run forever unless you `break` or `return` from within the loop + = note: required by `std::iter::IntoIterator::into_iter` + +error[E0277]: `[std::ops::RangeTo<{integer}>; 1]` is not an iterator --> $DIR/array-of-ranges.rs:5:14 | +LL | for _ in [..1] {} + | ^^^^^ if you meant to iterate until a value, remove the square brackets and add a starting value + | + = help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeTo<{integer}>; 1]` + = note: `[..end]` is an array of one `RangeTo`; you might have meant to have a bounded `Range` without the brackets: `0..end` + = note: required by `std::iter::IntoIterator::into_iter` + +error[E0277]: `[std::ops::RangeToInclusive<{integer}>; 1]` is not an iterator + --> $DIR/array-of-ranges.rs:6:14 + | +LL | for _ in [..=1] {} + | ^^^^^^ if you meant to iterate until a value (including it), remove the square brackets and add a starting value + | + = help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeToInclusive<{integer}>; 1]` + = note: `[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a bounded `RangeInclusive` without the brackets: `0..=end` + = note: required by `std::iter::IntoIterator::into_iter` + +error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator + --> $DIR/array-of-ranges.rs:9:14 + | LL | for _ in [start..end] {} | ^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets | @@ -19,7 +59,7 @@ LL | for _ in [start..end] {} = note: required by `std::iter::IntoIterator::into_iter` error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:7:14 + --> $DIR/array-of-ranges.rs:11:14 | LL | for _ in array_of_range {} | ^^^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets @@ -29,7 +69,7 @@ LL | for _ in array_of_range {} = note: required by `std::iter::IntoIterator::into_iter` error[E0277]: `[std::ops::Range<{integer}>; 2]` is not an iterator - --> $DIR/array-of-ranges.rs:8:14 + --> $DIR/array-of-ranges.rs:12:14 | LL | for _ in [0..1, 2..3] {} | ^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it @@ -38,6 +78,16 @@ LL | for _ in [0..1, 2..3] {} = note: arrays are not an iterators, but slices like the following are: `&[1, 2, 3]` = note: required by `std::iter::IntoIterator::into_iter` -error: aborting due to 4 previous errors +error[E0277]: `[std::ops::RangeInclusive<{integer}>; 1]` is not an iterator + --> $DIR/array-of-ranges.rs:13:14 + | +LL | for _ in [0..=1] {} + | ^^^^^^^ if you meant to iterate between two values, remove the square brackets + | + = help: the trait `std::iter::Iterator` is not implemented for `[std::ops::RangeInclusive<{integer}>; 1]` + = note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end` + = note: required by `std::iter::IntoIterator::into_iter` + +error: aborting due to 9 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/iterators/ranges.rs b/src/test/ui/iterators/ranges.rs new file mode 100644 index 00000000000..925d2d61a12 --- /dev/null +++ b/src/test/ui/iterators/ranges.rs @@ -0,0 +1,9 @@ +fn main() { + for _ in ..10 {} + //~^ ERROR E0277 + for _ in ..=10 {} + //~^ ERROR E0277 + for _ in 0..10 {} + for _ in 0..=10 {} + for _ in 0.. {} +} diff --git a/src/test/ui/iterators/ranges.stderr b/src/test/ui/iterators/ranges.stderr new file mode 100644 index 00000000000..e5e2d87879d --- /dev/null +++ b/src/test/ui/iterators/ranges.stderr @@ -0,0 +1,23 @@ +error[E0277]: `std::ops::RangeTo<{integer}>` is not an iterator + --> $DIR/ranges.rs:2:14 + | +LL | for _ in ..10 {} + | ^^^^ if you meant to iterate until a value, add a starting value + | + = help: the trait `std::iter::Iterator` is not implemented for `std::ops::RangeTo<{integer}>` + = note: `..end` is a `RangeTo`, which cannot be iterated on; you might have meant to have a bounded `Range`: `0..end` + = note: required by `std::iter::IntoIterator::into_iter` + +error[E0277]: `std::ops::RangeToInclusive<{integer}>` is not an iterator + --> $DIR/ranges.rs:4:14 + | +LL | for _ in ..=10 {} + | ^^^^^ if you meant to iterate until a value (including it), add a starting value + | + = help: the trait `std::iter::Iterator` is not implemented for `std::ops::RangeToInclusive<{integer}>` + = note: `..=end` is a `RangeToInclusive`, which cannot be iterated on; you might have meant to have a bounded `RangeInclusive`: `0..=end` + = note: required by `std::iter::IntoIterator::into_iter` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`.