From f326adf53d0828bd798cc19f4eb0a60d76d5d72c Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Fri, 25 Oct 2019 17:25:58 +0200 Subject: [PATCH 1/4] Add `IntoIterator` impl `for [T; N]` (arrays by value) --- library/core/src/array/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 8f52985d1df71..345abdd16b718 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -191,6 +191,16 @@ impl fmt::Debug for [T; N] { } } +#[stable(feature = "array_into_iter_impl", since = "1.53.0")] +impl IntoIterator for [T; N] { + type Item = T; + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + IntoIter::new(self) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T, const N: usize> IntoIterator for &'a [T; N] { type Item = &'a T; From e6bccc9d020b9b1b587808b5c388de8124eb2d15 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Tue, 28 Jul 2020 19:02:17 +0200 Subject: [PATCH 2/4] Remove `array_into_iter` lint (tracking issue #66145) This was lint's only purpose was to detect code that would break once arrays would implement `IntoIterator`. Now that this impl exists, this lint will never be triggered and thus is useless. --- compiler/rustc_lint/src/array_into_iter.rs | 125 --------- compiler/rustc_lint/src/lib.rs | 3 - .../iterators/into-iter-on-arrays-lint.fixed | 61 ----- .../ui/iterators/into-iter-on-arrays-lint.rs | 61 ----- .../iterators/into-iter-on-arrays-lint.stderr | 247 ------------------ ...issue-78660-cap-lints-future-compat.stderr | 11 - 6 files changed, 508 deletions(-) delete mode 100644 compiler/rustc_lint/src/array_into_iter.rs delete mode 100644 src/test/ui/iterators/into-iter-on-arrays-lint.fixed delete mode 100644 src/test/ui/iterators/into-iter-on-arrays-lint.rs delete mode 100644 src/test/ui/iterators/into-iter-on-arrays-lint.stderr delete mode 100644 src/test/ui/lint/issue-78660-cap-lints-future-compat.stderr diff --git a/compiler/rustc_lint/src/array_into_iter.rs b/compiler/rustc_lint/src/array_into_iter.rs deleted file mode 100644 index 0b5bd39f7f984..0000000000000 --- a/compiler/rustc_lint/src/array_into_iter.rs +++ /dev/null @@ -1,125 +0,0 @@ -use crate::{LateContext, LateLintPass, LintContext}; -use rustc_errors::Applicability; -use rustc_hir as hir; -use rustc_middle::ty; -use rustc_middle::ty::adjustment::{Adjust, Adjustment}; -use rustc_session::lint::FutureBreakage; -use rustc_span::symbol::sym; - -declare_lint! { - /// The `array_into_iter` lint detects calling `into_iter` on arrays. - /// - /// ### Example - /// - /// ```rust - /// # #![allow(unused)] - /// [1, 2, 3].into_iter().for_each(|n| { *n; }); - /// ``` - /// - /// {{produces}} - /// - /// ### Explanation - /// - /// In the future, it is planned to add an `IntoIter` implementation for - /// arrays such that it will iterate over *values* of the array instead of - /// references. Due to how method resolution works, this will change - /// existing code that uses `into_iter` on arrays. The solution to avoid - /// this warning is to use `iter()` instead of `into_iter()`. - /// - /// This is a [future-incompatible] lint to transition this to a hard error - /// in the future. See [issue #66145] for more details and a more thorough - /// description of the lint. - /// - /// [issue #66145]: https://github.com/rust-lang/rust/issues/66145 - /// [future-incompatible]: ../index.md#future-incompatible-lints - pub ARRAY_INTO_ITER, - Warn, - "detects calling `into_iter` on arrays", - @future_incompatible = FutureIncompatibleInfo { - reference: "issue #66145 ", - edition: None, - future_breakage: Some(FutureBreakage { - date: None - }) - }; -} - -declare_lint_pass!( - /// Checks for instances of calling `into_iter` on arrays. - ArrayIntoIter => [ARRAY_INTO_ITER] -); - -impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter { - fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { - // We only care about method call expressions. - if let hir::ExprKind::MethodCall(call, span, args, _) = &expr.kind { - if call.ident.name != sym::into_iter { - return; - } - - // Check if the method call actually calls the libcore - // `IntoIterator::into_iter`. - let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap(); - match cx.tcx.trait_of_item(def_id) { - Some(trait_id) if cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_id) => {} - _ => return, - }; - - // As this is a method call expression, we have at least one - // argument. - let receiver_arg = &args[0]; - - // Peel all `Box<_>` layers. We have to special case `Box` here as - // `Box` is the only thing that values can be moved out of via - // method call. `Box::new([1]).into_iter()` should trigger this - // lint. - let mut recv_ty = cx.typeck_results().expr_ty(receiver_arg); - let mut num_box_derefs = 0; - while recv_ty.is_box() { - num_box_derefs += 1; - recv_ty = recv_ty.boxed_ty(); - } - - // Make sure we found an array after peeling the boxes. - if !matches!(recv_ty.kind(), ty::Array(..)) { - return; - } - - // Make sure that there is an autoref coercion at the expected - // position. The first `num_box_derefs` adjustments are the derefs - // of the box. - match cx.typeck_results().expr_adjustments(receiver_arg).get(num_box_derefs) { - Some(Adjustment { kind: Adjust::Borrow(_), .. }) => {} - _ => return, - } - - // Emit lint diagnostic. - let target = match *cx.typeck_results().expr_ty_adjusted(receiver_arg).kind() { - ty::Ref(_, inner_ty, _) if inner_ty.is_array() => "[T; N]", - ty::Ref(_, inner_ty, _) if matches!(inner_ty.kind(), ty::Slice(..)) => "[T]", - - // We know the original first argument type is an array type, - // we know that the first adjustment was an autoref coercion - // and we know that `IntoIterator` is the trait involved. The - // array cannot be coerced to something other than a reference - // to an array or to a slice. - _ => bug!("array type coerced to something other than array or slice"), - }; - cx.struct_span_lint(ARRAY_INTO_ITER, *span, |lint| { - lint.build(&format!( - "this method call currently resolves to `<&{} as IntoIterator>::into_iter` (due \ - to autoref coercions), but that might change in the future when \ - `IntoIterator` impls for arrays are added.", - target, - )) - .span_suggestion( - call.ident.span, - "use `.iter()` instead of `.into_iter()` to avoid ambiguity", - "iter".into(), - Applicability::MachineApplicable, - ) - .emit(); - }) - } - } -} diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index e2724b52453a5..1ac078f1f397b 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -47,7 +47,6 @@ extern crate rustc_middle; #[macro_use] extern crate rustc_session; -mod array_into_iter; pub mod builtin; mod context; mod early; @@ -76,7 +75,6 @@ use rustc_session::lint::builtin::{ use rustc_span::symbol::{Ident, Symbol}; use rustc_span::Span; -use array_into_iter::ArrayIntoIter; use builtin::*; use internal::*; use methods::*; @@ -166,7 +164,6 @@ macro_rules! late_lint_passes { // FIXME: Turn the computation of types which implement Debug into a query // and change this to a module lint pass MissingDebugImplementations: MissingDebugImplementations::default(), - ArrayIntoIter: ArrayIntoIter, ClashingExternDeclarations: ClashingExternDeclarations::new(), DropTraitConstraints: DropTraitConstraints, TemporaryCStringAsPtr: TemporaryCStringAsPtr, diff --git a/src/test/ui/iterators/into-iter-on-arrays-lint.fixed b/src/test/ui/iterators/into-iter-on-arrays-lint.fixed deleted file mode 100644 index 7f511bde3cbfc..0000000000000 --- a/src/test/ui/iterators/into-iter-on-arrays-lint.fixed +++ /dev/null @@ -1,61 +0,0 @@ -// run-pass -// run-rustfix - -fn main() { - let small = [1, 2]; - let big = [0u8; 33]; - - // Expressions that should trigger the lint - small.iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - [1, 2].iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - big.iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - [0u8; 33].iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - - Box::new(small).iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new([1, 2]).iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new(big).iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new([0u8; 33]).iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - - Box::new(Box::new(small)).iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new(Box::new([1, 2])).iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new(Box::new(big)).iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new(Box::new([0u8; 33])).iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - - // Expressions that should not - (&[1, 2]).into_iter(); - (&small).into_iter(); - (&[0u8; 33]).into_iter(); - (&big).into_iter(); - - for _ in &[1, 2] {} - (&small as &[_]).into_iter(); - small[..].into_iter(); - std::iter::IntoIterator::into_iter(&[1, 2]); - - #[allow(array_into_iter)] - [0, 1].into_iter(); -} diff --git a/src/test/ui/iterators/into-iter-on-arrays-lint.rs b/src/test/ui/iterators/into-iter-on-arrays-lint.rs deleted file mode 100644 index d5fe83a7834b6..0000000000000 --- a/src/test/ui/iterators/into-iter-on-arrays-lint.rs +++ /dev/null @@ -1,61 +0,0 @@ -// run-pass -// run-rustfix - -fn main() { - let small = [1, 2]; - let big = [0u8; 33]; - - // Expressions that should trigger the lint - small.into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - [1, 2].into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - big.into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - [0u8; 33].into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - - Box::new(small).into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new([1, 2]).into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new(big).into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new([0u8; 33]).into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - - Box::new(Box::new(small)).into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new(Box::new([1, 2])).into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new(Box::new(big)).into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - Box::new(Box::new([0u8; 33])).into_iter(); - //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` - //~| WARNING this was previously accepted by the compiler but is being phased out - - // Expressions that should not - (&[1, 2]).into_iter(); - (&small).into_iter(); - (&[0u8; 33]).into_iter(); - (&big).into_iter(); - - for _ in &[1, 2] {} - (&small as &[_]).into_iter(); - small[..].into_iter(); - std::iter::IntoIterator::into_iter(&[1, 2]); - - #[allow(array_into_iter)] - [0, 1].into_iter(); -} diff --git a/src/test/ui/iterators/into-iter-on-arrays-lint.stderr b/src/test/ui/iterators/into-iter-on-arrays-lint.stderr deleted file mode 100644 index 211315c3fcf05..0000000000000 --- a/src/test/ui/iterators/into-iter-on-arrays-lint.stderr +++ /dev/null @@ -1,247 +0,0 @@ -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:9:11 - | -LL | small.into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = note: `#[warn(array_into_iter)]` on by default - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:12:12 - | -LL | [1, 2].into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:15:9 - | -LL | big.into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:18:15 - | -LL | [0u8; 33].into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:22:21 - | -LL | Box::new(small).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:25:22 - | -LL | Box::new([1, 2]).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:28:19 - | -LL | Box::new(big).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:31:25 - | -LL | Box::new([0u8; 33]).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:35:31 - | -LL | Box::new(Box::new(small)).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:38:32 - | -LL | Box::new(Box::new([1, 2])).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:41:29 - | -LL | Box::new(Box::new(big)).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:44:35 - | -LL | Box::new(Box::new([0u8; 33])).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -warning: 12 warnings emitted - -Future incompatibility report: Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:9:11 - | -LL | small.into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = note: `#[warn(array_into_iter)]` on by default - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:12:12 - | -LL | [1, 2].into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:15:9 - | -LL | big.into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:18:15 - | -LL | [0u8; 33].into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:22:21 - | -LL | Box::new(small).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:25:22 - | -LL | Box::new([1, 2]).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:28:19 - | -LL | Box::new(big).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:31:25 - | -LL | Box::new([0u8; 33]).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:35:31 - | -LL | Box::new(Box::new(small)).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:38:32 - | -LL | Box::new(Box::new([1, 2])).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:41:29 - | -LL | Box::new(Box::new(big)).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:44:35 - | -LL | Box::new(Box::new([0u8; 33])).into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - -Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/into-iter-on-arrays-lint.rs:60:12 - | -LL | [0, 1].into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | -note: the lint level is defined here - --> $DIR/into-iter-on-arrays-lint.rs:59:13 - | -LL | #[allow(array_into_iter)] - | ^^^^^^^^^^^^^^^ - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - diff --git a/src/test/ui/lint/issue-78660-cap-lints-future-compat.stderr b/src/test/ui/lint/issue-78660-cap-lints-future-compat.stderr deleted file mode 100644 index 79958ba90d409..0000000000000 --- a/src/test/ui/lint/issue-78660-cap-lints-future-compat.stderr +++ /dev/null @@ -1,11 +0,0 @@ -Future incompatibility report: Future breakage date: None, diagnostic: -warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. - --> $DIR/issue-78660-cap-lints-future-compat.rs:9:12 - | -LL | ["hi"].into_iter(); - | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` - | - = note: `-D array-into-iter` implied by `-D warnings` - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #66145 - From 25efe72a3be1909a890340152c286065fc194968 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Tue, 28 Jul 2020 19:03:56 +0200 Subject: [PATCH 3/4] Adjust docs and tests for new `IntoIterator` impl for arrays --- library/std/src/primitive_docs.rs | 23 +---- src/test/ui/iterators/array-of-ranges.rs | 11 +- src/test/ui/iterators/array-of-ranges.stderr | 102 ------------------- src/test/ui/iterators/array.rs | 5 +- src/test/ui/iterators/array.stderr | 36 ------- 5 files changed, 8 insertions(+), 169 deletions(-) delete mode 100644 src/test/ui/iterators/array-of-ranges.stderr delete mode 100644 src/test/ui/iterators/array.stderr diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 64b22b64f4bf1..ad4737ec2f618 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -498,7 +498,7 @@ mod prim_pointer {} /// - [`Copy`] /// - [`Clone`] /// - [`Debug`] -/// - [`IntoIterator`] (implemented for `&[T; N]` and `&mut [T; N]`) +/// - [`IntoIterator`] (implemented for `[T; N]`, `&[T; N]` and `&mut [T; N]`) /// - [`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`] /// - [`Hash`] /// - [`AsRef`], [`AsMut`] @@ -526,31 +526,16 @@ mod prim_pointer {} /// assert_eq!([1, 2], &array[1..]); /// /// // This loop prints: 0 1 2 -/// for x in &array { +/// for x in array { /// print!("{} ", x); /// } /// ``` /// -/// An array itself is not iterable: -/// -/// ```compile_fail,E0277 -/// let array: [i32; 3] = [0; 3]; -/// -/// for x in array { } -/// // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied -/// ``` -/// -/// The solution is to coerce the array to a slice by calling a slice method: +/// You can also iterate over reference to the array's elements: /// /// ``` -/// # let array: [i32; 3] = [0; 3]; -/// for x in array.iter() { } -/// ``` -/// -/// You can also use the array reference's [`IntoIterator`] implementation: +/// let array: [i32; 3] = [0; 3]; /// -/// ``` -/// # let array: [i32; 3] = [0; 3]; /// for x in &array { } /// ``` /// diff --git a/src/test/ui/iterators/array-of-ranges.rs b/src/test/ui/iterators/array-of-ranges.rs index d2dfc7ec327d3..037540a3e89e6 100644 --- a/src/test/ui/iterators/array-of-ranges.rs +++ b/src/test/ui/iterators/array-of-ranges.rs @@ -1,23 +1,16 @@ +// check-pass + fn main() { for _ in [0..1] {} -//~^ ERROR is not an iterator for _ in [0..=1] {} -//~^ ERROR is not an iterator for _ in [0..] {} -//~^ ERROR is not an iterator for _ in [..1] {} -//~^ ERROR is not an iterator for _ in [..=1] {} -//~^ ERROR is not an iterator let start = 0; let end = 0; for _ in [start..end] {} -//~^ ERROR is not an iterator let array_of_range = [start..end]; for _ in array_of_range {} -//~^ ERROR is not an iterator for _ in [0..1, 2..3] {} -//~^ ERROR is not an iterator for _ in [0..=1] {} -//~^ ERROR is not an iterator } diff --git a/src/test/ui/iterators/array-of-ranges.stderr b/src/test/ui/iterators/array-of-ranges.stderr deleted file mode 100644 index 7d58eb948ea81..0000000000000 --- a/src/test/ui/iterators/array-of-ranges.stderr +++ /dev/null @@ -1,102 +0,0 @@ -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:2:14 - | -LL | for _ in [0..1] {} - | ^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 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 because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:4:14 - | -LL | for _ in [0..=1] {} - | ^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[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 because of the requirements on the impl of `IntoIterator` for `[RangeInclusive<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeFrom<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:6:14 - | -LL | for _ in [0..] {} - | ^^^^^ if you meant to iterate from a value onwards, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[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 because of the requirements on the impl of `IntoIterator` for `[RangeFrom<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeTo<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:8: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 `Iterator` is not implemented for `[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 because of the requirements on the impl of `IntoIterator` for `[RangeTo<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeToInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:10: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 `Iterator` is not implemented for `[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 because of the requirements on the impl of `IntoIterator` for `[RangeToInclusive<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:14:14 - | -LL | for _ in [start..end] {} - | ^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 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 because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:17:14 - | -LL | for _ in array_of_range {} - | ^^^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 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 because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 2]` is not an iterator - --> $DIR/array-of-ranges.rs:19:14 - | -LL | for _ in [0..1, 2..3] {} - | ^^^^^^^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 2]` - = note: required by `into_iter` - -error[E0277]: `[RangeInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:21:14 - | -LL | for _ in [0..=1] {} - | ^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[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 because of the requirements on the impl of `IntoIterator` for `[RangeInclusive<{integer}>; 1]` - = note: required by `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/array.rs b/src/test/ui/iterators/array.rs index 33c84f6fa3582..5985c74e11fdf 100644 --- a/src/test/ui/iterators/array.rs +++ b/src/test/ui/iterators/array.rs @@ -1,9 +1,8 @@ +// check-pass + fn main() { for _ in [1, 2] {} -//~^ ERROR is not an iterator let x = [1, 2]; for _ in x {} -//~^ ERROR is not an iterator for _ in [1.0, 2.0] {} -//~^ ERROR is not an iterator } diff --git a/src/test/ui/iterators/array.stderr b/src/test/ui/iterators/array.stderr deleted file mode 100644 index 7e2b600fb7af2..0000000000000 --- a/src/test/ui/iterators/array.stderr +++ /dev/null @@ -1,36 +0,0 @@ -error[E0277]: `[{integer}; 2]` is not an iterator - --> $DIR/array.rs:2:14 - | -LL | for _ in [1, 2] {} - | ^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{integer}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{integer}; 2]` - = note: required by `into_iter` - -error[E0277]: `[{integer}; 2]` is not an iterator - --> $DIR/array.rs:5:14 - | -LL | for _ in x {} - | ^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{integer}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{integer}; 2]` - = note: required by `into_iter` - -error[E0277]: `[{float}; 2]` is not an iterator - --> $DIR/array.rs:7:14 - | -LL | for _ in [1.0, 2.0] {} - | ^^^^^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{float}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{float}; 2]` - = note: required by `into_iter` - -error: aborting due to 3 previous errors - -For more information about this error, try `rustc --explain E0277`. From 30f0ded1838747b4790a0554355cf03a410f0053 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Tue, 28 Jul 2020 20:12:32 +0200 Subject: [PATCH 4/4] Use the `IntoIterator` array impl in several places across `library/*` I surely missed quite a few spots, but I think I got most of them. And it's not like changing this is particularly important. --- .../alloc/src/collections/btree/map/tests.rs | 2 +- library/alloc/src/collections/btree/set.rs | 18 +++++----- .../alloc/src/collections/btree/set/tests.rs | 20 +++++------ library/alloc/src/vec/mod.rs | 8 ++--- library/alloc/tests/binary_heap.rs | 6 ++-- library/alloc/tests/linked_list.rs | 2 +- library/alloc/tests/string.rs | 3 +- library/alloc/tests/vec.rs | 14 ++++---- library/alloc/tests/vec_deque.rs | 10 +++--- library/core/src/char/decode.rs | 4 +-- library/core/src/char/methods.rs | 4 +-- library/core/src/iter/traits/iterator.rs | 10 +++--- .../core/tests/iter/adapters/intersperse.rs | 2 +- library/core/tests/iter/adapters/peekable.rs | 4 +-- library/core/tests/iter/adapters/zip.rs | 20 +++++------ library/core/tests/iter/traits/iterator.rs | 16 ++++----- library/proc_macro/src/quote.rs | 5 ++- library/std/src/collections/hash/map.rs | 3 +- library/std/src/collections/hash/map/tests.rs | 12 +++---- library/std/src/collections/hash/set.rs | 36 +++++++++---------- library/std/src/env/tests.rs | 4 +-- 21 files changed, 101 insertions(+), 102 deletions(-) diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index e636e490e1bf4..b6275d59cafd0 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -1289,7 +1289,7 @@ fn test_borrow() { fn test_entry() { let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)]; - let mut map: BTreeMap<_, _> = xs.iter().cloned().collect(); + let mut map: BTreeMap<_, _> = xs.into_iter().collect(); // Existing key (insert) match map.entry(1) { diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index a331b8d8e4bbb..08481e2fe504a 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -473,7 +473,7 @@ impl BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let set: BTreeSet<_> = [1, 2, 3].into_iter().collect(); /// assert_eq!(set.contains(&1), true); /// assert_eq!(set.contains(&4), false); /// ``` @@ -497,7 +497,7 @@ impl BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let set: BTreeSet<_> = [1, 2, 3].into_iter().collect(); /// assert_eq!(set.get(&2), Some(&2)); /// assert_eq!(set.get(&4), None); /// ``` @@ -518,7 +518,7 @@ impl BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let a: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let a: BTreeSet<_> = [1, 2, 3].into_iter().collect(); /// let mut b = BTreeSet::new(); /// /// assert_eq!(a.is_disjoint(&b), true); @@ -543,7 +543,7 @@ impl BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let sup: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let sup: BTreeSet<_> = [1, 2, 3].into_iter().collect(); /// let mut set = BTreeSet::new(); /// /// assert_eq!(set.is_subset(&sup), true); @@ -619,7 +619,7 @@ impl BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let sub: BTreeSet<_> = [1, 2].iter().cloned().collect(); + /// let sub: BTreeSet<_> = [1, 2].into_iter().collect(); /// let mut set = BTreeSet::new(); /// /// assert_eq!(set.is_superset(&sub), false); @@ -831,7 +831,7 @@ impl BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let mut set: BTreeSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let mut set: BTreeSet<_> = [1, 2, 3].into_iter().collect(); /// assert_eq!(set.take(&2), Some(2)); /// assert_eq!(set.take(&2), None); /// ``` @@ -985,7 +985,7 @@ impl BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let set: BTreeSet = [1, 2, 3].iter().cloned().collect(); + /// let set: BTreeSet = [1, 2, 3].into_iter().collect(); /// let mut set_iter = set.iter(); /// assert_eq!(set_iter.next(), Some(&1)); /// assert_eq!(set_iter.next(), Some(&2)); @@ -998,7 +998,7 @@ impl BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let set: BTreeSet = [3, 1, 2].iter().cloned().collect(); + /// let set: BTreeSet = [3, 1, 2].into_iter().collect(); /// let mut set_iter = set.iter(); /// assert_eq!(set_iter.next(), Some(&1)); /// assert_eq!(set_iter.next(), Some(&2)); @@ -1069,7 +1069,7 @@ impl IntoIterator for BTreeSet { /// ``` /// use std::collections::BTreeSet; /// - /// let set: BTreeSet = [1, 2, 3, 4].iter().cloned().collect(); + /// let set: BTreeSet = [1, 2, 3, 4].into_iter().collect(); /// /// let v: Vec<_> = set.into_iter().collect(); /// assert_eq!(v, [1, 2, 3, 4]); diff --git a/library/alloc/src/collections/btree/set/tests.rs b/library/alloc/src/collections/btree/set/tests.rs index 4cb6e3d6619dc..14ea41fccc939 100644 --- a/library/alloc/src/collections/btree/set/tests.rs +++ b/library/alloc/src/collections/btree/set/tests.rs @@ -113,8 +113,8 @@ fn test_intersection() { #[test] fn test_intersection_size_hint() { - let x: BTreeSet = [3, 4].iter().copied().collect(); - let y: BTreeSet = [1, 2, 3].iter().copied().collect(); + let x: BTreeSet = [3, 4].into_iter().collect(); + let y: BTreeSet = [1, 2, 3].into_iter().collect(); let mut iter = x.intersection(&y); assert_eq!(iter.size_hint(), (1, Some(1))); assert_eq!(iter.next(), Some(&3)); @@ -165,7 +165,7 @@ fn test_difference() { #[test] fn test_difference_size_hint() { - let s246: BTreeSet = [2, 4, 6].iter().copied().collect(); + let s246: BTreeSet = [2, 4, 6].into_iter().collect(); let s23456: BTreeSet = (2..=6).collect(); let mut iter = s246.difference(&s23456); assert_eq!(iter.size_hint(), (0, Some(3))); @@ -241,8 +241,8 @@ fn test_symmetric_difference() { #[test] fn test_symmetric_difference_size_hint() { - let x: BTreeSet = [2, 4].iter().copied().collect(); - let y: BTreeSet = [1, 2, 3].iter().copied().collect(); + let x: BTreeSet = [2, 4].into_iter().collect(); + let y: BTreeSet = [1, 2, 3].into_iter().collect(); let mut iter = x.symmetric_difference(&y); assert_eq!(iter.size_hint(), (0, Some(5))); assert_eq!(iter.next(), Some(&1)); @@ -269,8 +269,8 @@ fn test_union() { #[test] fn test_union_size_hint() { - let x: BTreeSet = [2, 4].iter().copied().collect(); - let y: BTreeSet = [1, 2, 3].iter().copied().collect(); + let x: BTreeSet = [2, 4].into_iter().collect(); + let y: BTreeSet = [1, 2, 3].into_iter().collect(); let mut iter = x.union(&y); assert_eq!(iter.size_hint(), (3, Some(5))); assert_eq!(iter.next(), Some(&1)); @@ -338,8 +338,8 @@ fn test_retain() { #[test] fn test_drain_filter() { - let mut x: BTreeSet<_> = [1].iter().copied().collect(); - let mut y: BTreeSet<_> = [1].iter().copied().collect(); + let mut x: BTreeSet<_> = [1].into_iter().collect(); + let mut y: BTreeSet<_> = [1].into_iter().collect(); x.drain_filter(|_| true); y.drain_filter(|_| false); @@ -423,7 +423,7 @@ fn test_zip() { fn test_from_iter() { let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - let set: BTreeSet<_> = xs.iter().cloned().collect(); + let set: BTreeSet<_> = xs.into_iter().collect(); for x in &xs { assert!(set.contains(x)); diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 20c2aae1789ad..72ca099981d31 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -887,7 +887,7 @@ impl Vec { /// /// ``` /// let mut vec = Vec::with_capacity(10); - /// vec.extend([1, 2, 3].iter().cloned()); + /// vec.extend([1, 2, 3]); /// assert_eq!(vec.capacity(), 10); /// vec.shrink_to_fit(); /// assert!(vec.capacity() >= 3); @@ -915,7 +915,7 @@ impl Vec { /// ``` /// #![feature(shrink_to)] /// let mut vec = Vec::with_capacity(10); - /// vec.extend([1, 2, 3].iter().cloned()); + /// vec.extend([1, 2, 3]); /// assert_eq!(vec.capacity(), 10); /// vec.shrink_to(4); /// assert!(vec.capacity() >= 4); @@ -948,7 +948,7 @@ impl Vec { /// /// ``` /// let mut vec = Vec::with_capacity(10); - /// vec.extend([1, 2, 3].iter().cloned()); + /// vec.extend([1, 2, 3]); /// /// assert_eq!(vec.capacity(), 10); /// let slice = vec.into_boxed_slice(); @@ -2533,7 +2533,7 @@ impl Vec { /// ``` /// let mut v = vec![1, 2, 3]; /// let new = [7, 8]; - /// let u: Vec<_> = v.splice(..2, new.iter().cloned()).collect(); + /// let u: Vec<_> = v.splice(..2, new.into_iter()).collect(); /// assert_eq!(v, &[7, 8, 3]); /// assert_eq!(u, &[1, 2]); /// ``` diff --git a/library/alloc/tests/binary_heap.rs b/library/alloc/tests/binary_heap.rs index ce794a9a4afa2..431840e8914bd 100644 --- a/library/alloc/tests/binary_heap.rs +++ b/library/alloc/tests/binary_heap.rs @@ -264,7 +264,7 @@ fn test_empty_peek_mut() { fn test_from_iter() { let xs = vec![9, 8, 7, 6, 5, 4, 3, 2, 1]; - let mut q: BinaryHeap<_> = xs.iter().rev().cloned().collect(); + let mut q: BinaryHeap<_> = xs.iter().rev().copied().collect(); for &x in &xs { assert_eq!(q.pop().unwrap(), x); @@ -273,7 +273,7 @@ fn test_from_iter() { #[test] fn test_drain() { - let mut q: BinaryHeap<_> = [9, 8, 7, 6, 5, 4, 3, 2, 1].iter().cloned().collect(); + let mut q: BinaryHeap<_> = [9, 8, 7, 6, 5, 4, 3, 2, 1].into_iter().collect(); assert_eq!(q.drain().take(5).count(), 5); @@ -282,7 +282,7 @@ fn test_drain() { #[test] fn test_drain_sorted() { - let mut q: BinaryHeap<_> = [9, 8, 7, 6, 5, 4, 3, 2, 1].iter().cloned().collect(); + let mut q: BinaryHeap<_> = [9, 8, 7, 6, 5, 4, 3, 2, 1].into_iter().collect(); assert_eq!(q.drain_sorted().take(5).collect::>(), vec![9, 8, 7, 6, 5]); diff --git a/library/alloc/tests/linked_list.rs b/library/alloc/tests/linked_list.rs index afcb9e03fd097..c02b74ec7859c 100644 --- a/library/alloc/tests/linked_list.rs +++ b/library/alloc/tests/linked_list.rs @@ -304,7 +304,7 @@ fn test_show() { let list: LinkedList<_> = (0..10).collect(); assert_eq!(format!("{:?}", list), "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"); - let list: LinkedList<_> = vec!["just", "one", "test", "more"].iter().cloned().collect(); + let list: LinkedList<_> = vec!["just", "one", "test", "more"].into_iter().collect(); assert_eq!(format!("{:?}", list), "[\"just\", \"one\", \"test\", \"more\"]"); } diff --git a/library/alloc/tests/string.rs b/library/alloc/tests/string.rs index 9ec0ee97ab926..5f74e454324cf 100644 --- a/library/alloc/tests/string.rs +++ b/library/alloc/tests/string.rs @@ -154,8 +154,7 @@ fn test_from_utf16() { (String::from("\u{20000}"), vec![0xD840, 0xDC00]), ]; - for p in &pairs { - let (s, u) = (*p).clone(); + for (s, u) in pairs { let s_as_utf16 = s.encode_utf16().collect::>(); let u_as_string = String::from_utf16(&u).unwrap(); diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index c142536cd2dfb..d4617d22d1188 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -458,7 +458,7 @@ fn test_partition() { fn test_zip_unzip() { let z1 = vec![(1, 4), (2, 5), (3, 6)]; - let (left, right): (Vec<_>, Vec<_>) = z1.iter().cloned().unzip(); + let (left, right): (Vec<_>, Vec<_>) = z1.into_iter().unzip(); assert_eq!((1, 4), (left[0], right[0])); assert_eq!((2, 5), (left[1], right[1])); @@ -793,7 +793,7 @@ fn test_drain_leak() { fn test_splice() { let mut v = vec![1, 2, 3, 4, 5]; let a = [10, 11, 12]; - v.splice(2..4, a.iter().cloned()); + v.splice(2..4, a.into_iter()); assert_eq!(v, &[1, 2, 10, 11, 12, 5]); v.splice(1..3, Some(20)); assert_eq!(v, &[1, 20, 11, 12, 5]); @@ -803,7 +803,7 @@ fn test_splice() { fn test_splice_inclusive_range() { let mut v = vec![1, 2, 3, 4, 5]; let a = [10, 11, 12]; - let t1: Vec<_> = v.splice(2..=3, a.iter().cloned()).collect(); + let t1: Vec<_> = v.splice(2..=3, a.into_iter()).collect(); assert_eq!(v, &[1, 2, 10, 11, 12, 5]); assert_eq!(t1, &[3, 4]); let t2: Vec<_> = v.splice(1..=2, Some(20)).collect(); @@ -816,7 +816,7 @@ fn test_splice_inclusive_range() { fn test_splice_out_of_bounds() { let mut v = vec![1, 2, 3, 4, 5]; let a = [10, 11, 12]; - v.splice(5..6, a.iter().cloned()); + v.splice(5..6, a.into_iter()); } #[test] @@ -824,14 +824,14 @@ fn test_splice_out_of_bounds() { fn test_splice_inclusive_out_of_bounds() { let mut v = vec![1, 2, 3, 4, 5]; let a = [10, 11, 12]; - v.splice(5..=5, a.iter().cloned()); + v.splice(5..=5, a.into_iter()); } #[test] fn test_splice_items_zero_sized() { let mut vec = vec![(), (), ()]; let vec2 = vec![]; - let t: Vec<_> = vec.splice(1..2, vec2.iter().cloned()).collect(); + let t: Vec<_> = vec.splice(1..2, vec2.into_iter()).collect(); assert_eq!(vec, &[(), ()]); assert_eq!(t, &[()]); } @@ -848,7 +848,7 @@ fn test_splice_unbounded() { fn test_splice_forget() { let mut v = vec![1, 2, 3, 4, 5]; let a = [10, 11, 12]; - std::mem::forget(v.splice(2..4, a.iter().cloned())); + std::mem::forget(v.splice(2..4, a.into_iter())); assert_eq!(v, &[1, 2]); } diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs index 0919b1325bceb..68fc991c86dcf 100644 --- a/library/alloc/tests/vec_deque.rs +++ b/library/alloc/tests/vec_deque.rs @@ -497,7 +497,7 @@ fn test_drain() { #[test] fn test_from_iter() { let v = vec![1, 2, 3, 4, 5, 6, 7]; - let deq: VecDeque<_> = v.iter().cloned().collect(); + let deq: VecDeque<_> = v.iter().copied().collect(); let u: Vec<_> = deq.iter().cloned().collect(); assert_eq!(u, v); @@ -648,7 +648,7 @@ fn test_show() { let ringbuf: VecDeque<_> = (0..10).collect(); assert_eq!(format!("{:?}", ringbuf), "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"); - let ringbuf: VecDeque<_> = vec!["just", "one", "test", "more"].iter().cloned().collect(); + let ringbuf: VecDeque<_> = vec!["just", "one", "test", "more"].into_iter().collect(); assert_eq!(format!("{:?}", ringbuf), "[\"just\", \"one\", \"test\", \"more\"]"); } @@ -1462,7 +1462,7 @@ fn test_rotate_left_random() { let n = 12; let mut v: VecDeque<_> = (0..n).collect(); let mut total_shift = 0; - for shift in shifts.iter().cloned() { + for shift in shifts { v.rotate_left(shift); total_shift += shift; for i in 0..n { @@ -1480,7 +1480,7 @@ fn test_rotate_right_random() { let n = 12; let mut v: VecDeque<_> = (0..n).collect(); let mut total_shift = 0; - for shift in shifts.iter().cloned() { + for shift in shifts { v.rotate_right(shift); total_shift += shift; for i in 0..n { @@ -1514,7 +1514,7 @@ fn test_try_fold_unit() { #[test] fn test_try_fold_unit_none() { - let v: std::collections::VecDeque<()> = [(); 10].iter().cloned().collect(); + let v: std::collections::VecDeque<()> = [(); 10].into_iter().collect(); let mut iter = v.into_iter(); assert!(iter.try_fold((), |_, _| None).is_none()); assert_eq!(iter.len(), 9); diff --git a/library/core/src/char/decode.rs b/library/core/src/char/decode.rs index 5e7784730e3c9..5ddd41ec7c401 100644 --- a/library/core/src/char/decode.rs +++ b/library/core/src/char/decode.rs @@ -38,7 +38,7 @@ pub struct DecodeUtf16Error { /// ]; /// /// assert_eq!( -/// decode_utf16(v.iter().cloned()) +/// decode_utf16(v) /// .map(|r| r.map_err(|e| e.unpaired_surrogate())) /// .collect::>(), /// vec![ @@ -62,7 +62,7 @@ pub struct DecodeUtf16Error { /// ]; /// /// assert_eq!( -/// decode_utf16(v.iter().cloned()) +/// decode_utf16(v) /// .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)) /// .collect::(), /// "𝄞mus�ic�" diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index dcab2cd2d9db1..2da3d6a72fb6f 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -58,7 +58,7 @@ impl char { /// ]; /// /// assert_eq!( - /// decode_utf16(v.iter().cloned()) + /// decode_utf16(v) /// .map(|r| r.map_err(|e| e.unpaired_surrogate())) /// .collect::>(), /// vec![ @@ -82,7 +82,7 @@ impl char { /// ]; /// /// assert_eq!( - /// decode_utf16(v.iter().cloned()) + /// decode_utf16(v) /// .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)) /// .collect::(), /// "𝄞mus�ic�" diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index e75a36477188c..54f7208df9388 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1749,14 +1749,14 @@ pub trait Iterator { /// ``` /// let results = [Ok(1), Err("nope"), Ok(3), Err("bad")]; /// - /// let result: Result, &str> = results.iter().cloned().collect(); + /// let result: Result, &str> = results.into_iter().collect(); /// /// // gives us the first error /// assert_eq!(Err("nope"), result); /// /// let results = [Ok(1), Ok(3)]; /// - /// let result: Result, &str> = results.iter().cloned().collect(); + /// let result: Result, &str> = results.into_iter().collect(); /// /// // gives us the list of answers /// assert_eq!(Ok(vec![1, 3]), result); @@ -2016,7 +2016,7 @@ pub trait Iterator { /// let res = data.iter().try_for_each(|x| writeln!(stdout(), "{}", x)); /// assert!(res.is_ok()); /// - /// let mut it = data.iter().cloned(); + /// let mut it = data.into_iter(); /// let res = it.try_for_each(|x| rename(x, Path::new(x).with_extension("old"))); /// assert!(res.is_err()); /// // It short-circuited, so the remaining items are still in the iterator: @@ -2774,7 +2774,7 @@ pub trait Iterator { /// ``` /// let a = [(1, 2), (3, 4)]; /// - /// let (left, right): (Vec<_>, Vec<_>) = a.iter().cloned().unzip(); + /// let (left, right): (Vec<_>, Vec<_>) = a.into_iter().unzip(); /// /// assert_eq!(left, [1, 3]); /// assert_eq!(right, [2, 4]); @@ -2853,7 +2853,7 @@ pub trait Iterator { /// ``` /// let a = [1, 2, 3]; /// - /// let v_cloned: Vec<_> = a.iter().cloned().collect(); + /// let v_cloned: Vec<_> = a.into_iter().collect(); /// /// // cloned is the same as .map(|&x| x), for integers /// let v_map: Vec<_> = a.iter().map(|&x| x).collect(); diff --git a/library/core/tests/iter/adapters/intersperse.rs b/library/core/tests/iter/adapters/intersperse.rs index b336c03b5adbe..dfef1cba55f19 100644 --- a/library/core/tests/iter/adapters/intersperse.rs +++ b/library/core/tests/iter/adapters/intersperse.rs @@ -132,7 +132,7 @@ fn test_intersperse_collect_string() { #[test] fn test_try_fold_specialization_intersperse_err() { - let orig_iter = ["a", "b"].iter().copied().intersperse("-"); + let orig_iter = ["a", "b"].into_iter().intersperse("-"); // Abort after the first item. let mut iter = orig_iter.clone(); diff --git a/library/core/tests/iter/adapters/peekable.rs b/library/core/tests/iter/adapters/peekable.rs index 390414d4aa213..5fb5d753a4da7 100644 --- a/library/core/tests/iter/adapters/peekable.rs +++ b/library/core/tests/iter/adapters/peekable.rs @@ -231,12 +231,12 @@ fn test_peek_try_folds() { assert_eq!(iter.peek(), Some(&1)); assert_eq!(iter.try_rfold(7, f), (1..20).try_rfold(7, f)); - let mut iter = [100, 20, 30, 40, 50, 60, 70].iter().cloned().peekable(); + let mut iter = [100, 20, 30, 40, 50, 60, 70].into_iter().peekable(); assert_eq!(iter.peek(), Some(&100)); assert_eq!(iter.try_fold(0, i8::checked_add), None); assert_eq!(iter.peek(), Some(&40)); - let mut iter = [100, 20, 30, 40, 50, 60, 70].iter().cloned().peekable(); + let mut iter = [100, 20, 30, 40, 50, 60, 70].into_iter().peekable(); assert_eq!(iter.peek(), Some(&100)); assert_eq!(iter.try_rfold(0, i8::checked_add), None); assert_eq!(iter.peek(), Some(&100)); diff --git a/library/core/tests/iter/adapters/zip.rs b/library/core/tests/iter/adapters/zip.rs index 000c15f72c886..cac3df7b68137 100644 --- a/library/core/tests/iter/adapters/zip.rs +++ b/library/core/tests/iter/adapters/zip.rs @@ -29,7 +29,7 @@ fn test_zip_nth_side_effects() { a.push(n); n * 10 }) - .zip([2, 3, 4, 5, 6, 7, 8].iter().cloned().map(|n| { + .zip([2, 3, 4, 5, 6, 7, 8].into_iter().map(|n| { b.push(n * 100); n * 1000 })) @@ -51,7 +51,7 @@ fn test_zip_next_back_side_effects() { a.push(n); n * 10 }) - .zip([2, 3, 4, 5, 6, 7, 8].iter().cloned().map(|n| { + .zip([2, 3, 4, 5, 6, 7, 8].into_iter().map(|n| { b.push(n * 100); n * 1000 })); @@ -77,7 +77,7 @@ fn test_zip_nth_back_side_effects() { a.push(n); n * 10 }) - .zip([2, 3, 4, 5, 6, 7, 8].iter().cloned().map(|n| { + .zip([2, 3, 4, 5, 6, 7, 8].into_iter().map(|n| { b.push(n * 100); n * 1000 })) @@ -98,7 +98,7 @@ fn test_zip_next_back_side_effects_exhausted() { a.push(n); n * 10 }) - .zip([2, 3, 4].iter().cloned().map(|n| { + .zip([2, 3, 4].into_iter().map(|n| { b.push(n * 100); n * 1000 })); @@ -198,7 +198,7 @@ fn test_zip_nth_back_side_effects_exhausted() { a.push(n); n * 10 }) - .zip([2, 3, 4].iter().cloned().map(|n| { + .zip([2, 3, 4].into_iter().map(|n| { b.push(n * 100); n * 1000 })); @@ -218,9 +218,9 @@ fn test_zip_trusted_random_access_composition() { let b = a; let c = a; - let a = a.iter().copied(); - let b = b.iter().copied(); - let mut c = c.iter().copied(); + let a = a.into_iter(); + let b = b.into_iter(); + let mut c = c.into_iter(); c.next(); let mut z1 = a.zip(b); @@ -236,8 +236,8 @@ fn test_zip_trusted_random_access_composition() { fn test_double_ended_zip() { let xs = [1, 2, 3, 4, 5, 6]; let ys = [1, 2, 3, 7]; - let a = xs.iter().cloned(); - let b = ys.iter().cloned(); + let a = xs.into_iter(); + let b = ys.into_iter(); let mut it = a.zip(b); assert_eq!(it.next(), Some((1, 1))); assert_eq!(it.next(), Some((2, 2))); diff --git a/library/core/tests/iter/traits/iterator.rs b/library/core/tests/iter/traits/iterator.rs index 422e389e38017..69a09b450523a 100644 --- a/library/core/tests/iter/traits/iterator.rs +++ b/library/core/tests/iter/traits/iterator.rs @@ -74,8 +74,8 @@ fn test_cmp_by() { use core::cmp::Ordering; let f = |x: i32, y: i32| (x * x).cmp(&y); - let xs = || [1, 2, 3, 4].iter().copied(); - let ys = || [1, 4, 16].iter().copied(); + let xs = || [1, 2, 3, 4].into_iter(); + let ys = || [1, 4, 16].into_iter(); assert_eq!(xs().cmp_by(ys(), f), Ordering::Less); assert_eq!(ys().cmp_by(xs(), f), Ordering::Greater); @@ -90,8 +90,8 @@ fn test_partial_cmp_by() { use core::cmp::Ordering; let f = |x: i32, y: i32| (x * x).partial_cmp(&y); - let xs = || [1, 2, 3, 4].iter().copied(); - let ys = || [1, 4, 16].iter().copied(); + let xs = || [1, 2, 3, 4].into_iter(); + let ys = || [1, 4, 16].into_iter(); assert_eq!(xs().partial_cmp_by(ys(), f), Some(Ordering::Less)); assert_eq!(ys().partial_cmp_by(xs(), f), Some(Ordering::Greater)); @@ -101,8 +101,8 @@ fn test_partial_cmp_by() { assert_eq!(xs().partial_cmp_by(ys().take(2), f), Some(Ordering::Greater)); let f = |x: f64, y: f64| (x * x).partial_cmp(&y); - let xs = || [1.0, 2.0, 3.0, 4.0].iter().copied(); - let ys = || [1.0, 4.0, f64::NAN, 16.0].iter().copied(); + let xs = || [1.0, 2.0, 3.0, 4.0].into_iter(); + let ys = || [1.0, 4.0, f64::NAN, 16.0].into_iter(); assert_eq!(xs().partial_cmp_by(ys(), f), None); assert_eq!(ys().partial_cmp_by(xs(), f), Some(Ordering::Greater)); @@ -111,8 +111,8 @@ fn test_partial_cmp_by() { #[test] fn test_eq_by() { let f = |x: i32, y: i32| x * x == y; - let xs = || [1, 2, 3, 4].iter().copied(); - let ys = || [1, 4, 9, 16].iter().copied(); + let xs = || [1, 2, 3, 4].into_iter(); + let ys = || [1, 4, 9, 16].into_iter(); assert!(xs().eq_by(ys(), f)); assert!(!ys().eq_by(xs(), f)); diff --git a/library/proc_macro/src/quote.rs b/library/proc_macro/src/quote.rs index 144e2d6bac43b..815a9c0327dc2 100644 --- a/library/proc_macro/src/quote.rs +++ b/library/proc_macro/src/quote.rs @@ -28,8 +28,7 @@ macro_rules! quote_ts { [ TokenTree::from(Punct::new(':', Spacing::Joint)), TokenTree::from(Punct::new(':', Spacing::Alone)), - ].iter() - .cloned() + ].into_iter() .map(|mut x| { x.set_span(Span::def_site()); x @@ -52,7 +51,7 @@ macro_rules! quote { ($($t:tt)*) => { [ $(TokenStream::from(quote_ts!($t)),)* - ].iter().cloned().collect::() + ].into_iter().collect::() }; } diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 3dcc5cd2b5911..7a0963f741afa 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -197,7 +197,8 @@ use crate::sys; /// use std::collections::HashMap; /// /// let timber_resources: HashMap<&str, i32> = [("Norway", 100), ("Denmark", 50), ("Iceland", 10)] -/// .iter().cloned().collect(); +/// .into_iter() +/// .collect(); /// // use the values stored in map /// ``` diff --git a/library/std/src/collections/hash/map/tests.rs b/library/std/src/collections/hash/map/tests.rs index 819be14222752..6b430d2228fc8 100644 --- a/library/std/src/collections/hash/map/tests.rs +++ b/library/std/src/collections/hash/map/tests.rs @@ -574,7 +574,7 @@ fn test_from_iter() { fn test_size_hint() { let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; - let map: HashMap<_, _> = xs.iter().cloned().collect(); + let map: HashMap<_, _> = xs.into_iter().collect(); let mut iter = map.iter(); @@ -587,7 +587,7 @@ fn test_size_hint() { fn test_iter_len() { let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; - let map: HashMap<_, _> = xs.iter().cloned().collect(); + let map: HashMap<_, _> = xs.into_iter().collect(); let mut iter = map.iter(); @@ -600,7 +600,7 @@ fn test_iter_len() { fn test_mut_size_hint() { let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; - let mut map: HashMap<_, _> = xs.iter().cloned().collect(); + let mut map: HashMap<_, _> = xs.into_iter().collect(); let mut iter = map.iter_mut(); @@ -613,7 +613,7 @@ fn test_mut_size_hint() { fn test_iter_mut_len() { let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; - let mut map: HashMap<_, _> = xs.iter().cloned().collect(); + let mut map: HashMap<_, _> = xs.into_iter().collect(); let mut iter = map.iter_mut(); @@ -649,7 +649,7 @@ fn test_index_nonexistent() { fn test_entry() { let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)]; - let mut map: HashMap<_, _> = xs.iter().cloned().collect(); + let mut map: HashMap<_, _> = xs.into_iter().collect(); // Existing key (insert) match map.entry(1) { @@ -838,7 +838,7 @@ fn test_raw_entry() { let xs = [(1i32, 10i32), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)]; - let mut map: HashMap<_, _> = xs.iter().cloned().collect(); + let mut map: HashMap<_, _> = xs.into_iter().collect(); let compute_hash = |map: &HashMap, k: i32| -> u64 { use core::hash::{BuildHasher, Hash, Hasher}; diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index 8c801b9f1285d..4c7b505144b03 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -101,7 +101,7 @@ use super::map::{map_try_reserve_error, RandomState}; /// use std::collections::HashSet; /// /// let viking_names: HashSet<&'static str> = -/// [ "Einar", "Olaf", "Harald" ].iter().cloned().collect(); +/// [ "Einar", "Olaf", "Harald" ].into_iter().collect(); /// // use the values stored in the set /// ``` /// @@ -234,7 +234,7 @@ impl HashSet { /// ``` /// use std::collections::HashSet; /// - /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let mut set: HashSet<_> = [1, 2, 3].into_iter().collect(); /// assert!(!set.is_empty()); /// /// // print 1, 2, 3 in an arbitrary order @@ -492,8 +492,8 @@ where /// /// ``` /// use std::collections::HashSet; - /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); - /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); + /// let a: HashSet<_> = [1, 2, 3].into_iter().collect(); + /// let b: HashSet<_> = [4, 2, 3, 4].into_iter().collect(); /// /// // Can be seen as `a - b`. /// for x in a.difference(&b) { @@ -521,8 +521,8 @@ where /// /// ``` /// use std::collections::HashSet; - /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); - /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); + /// let a: HashSet<_> = [1, 2, 3].into_iter().collect(); + /// let b: HashSet<_> = [4, 2, 3, 4].into_iter().collect(); /// /// // Print 1, 4 in arbitrary order. /// for x in a.symmetric_difference(&b) { @@ -551,8 +551,8 @@ where /// /// ``` /// use std::collections::HashSet; - /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); - /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); + /// let a: HashSet<_> = [1, 2, 3].into_iter().collect(); + /// let b: HashSet<_> = [4, 2, 3, 4].into_iter().collect(); /// /// // Print 2, 3 in arbitrary order. /// for x in a.intersection(&b) { @@ -579,8 +579,8 @@ where /// /// ``` /// use std::collections::HashSet; - /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); - /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); + /// let a: HashSet<_> = [1, 2, 3].into_iter().collect(); + /// let b: HashSet<_> = [4, 2, 3, 4].into_iter().collect(); /// /// // Print 1, 2, 3, 4 in arbitrary order. /// for x in a.union(&b) { @@ -611,7 +611,7 @@ where /// ``` /// use std::collections::HashSet; /// - /// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let set: HashSet<_> = [1, 2, 3].into_iter().collect(); /// assert_eq!(set.contains(&1), true); /// assert_eq!(set.contains(&4), false); /// ``` @@ -636,7 +636,7 @@ where /// ``` /// use std::collections::HashSet; /// - /// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let set: HashSet<_> = [1, 2, 3].into_iter().collect(); /// assert_eq!(set.get(&2), Some(&2)); /// assert_eq!(set.get(&4), None); /// ``` @@ -660,7 +660,7 @@ where /// /// use std::collections::HashSet; /// - /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let mut set: HashSet<_> = [1, 2, 3].into_iter().collect(); /// assert_eq!(set.len(), 3); /// assert_eq!(set.get_or_insert(2), &2); /// assert_eq!(set.get_or_insert(100), &100); @@ -747,7 +747,7 @@ where /// ``` /// use std::collections::HashSet; /// - /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let a: HashSet<_> = [1, 2, 3].into_iter().collect(); /// let mut b = HashSet::new(); /// /// assert_eq!(a.is_disjoint(&b), true); @@ -773,7 +773,7 @@ where /// ``` /// use std::collections::HashSet; /// - /// let sup: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let sup: HashSet<_> = [1, 2, 3].into_iter().collect(); /// let mut set = HashSet::new(); /// /// assert_eq!(set.is_subset(&sup), true); @@ -795,7 +795,7 @@ where /// ``` /// use std::collections::HashSet; /// - /// let sub: HashSet<_> = [1, 2].iter().cloned().collect(); + /// let sub: HashSet<_> = [1, 2].into_iter().collect(); /// let mut set = HashSet::new(); /// /// assert_eq!(set.is_superset(&sub), false); @@ -897,7 +897,7 @@ where /// ``` /// use std::collections::HashSet; /// - /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// let mut set: HashSet<_> = [1, 2, 3].into_iter().collect(); /// assert_eq!(set.take(&2), Some(2)); /// assert_eq!(set.take(&2), None); /// ``` @@ -921,7 +921,7 @@ where /// use std::collections::HashSet; /// /// let xs = [1, 2, 3, 4, 5, 6]; - /// let mut set: HashSet = xs.iter().cloned().collect(); + /// let mut set: HashSet = xs.into_iter().collect(); /// set.retain(|&k| k % 2 == 0); /// assert_eq!(set.len(), 3); /// ``` diff --git a/library/std/src/env/tests.rs b/library/std/src/env/tests.rs index 94cace03af64e..dc820cb00f793 100644 --- a/library/std/src/env/tests.rs +++ b/library/std/src/env/tests.rs @@ -70,7 +70,7 @@ fn join_paths_unix() { assert!(test_eq(&[], "")); assert!(test_eq(&["/bin", "/usr/bin", "/usr/local/bin"], "/bin:/usr/bin:/usr/local/bin")); assert!(test_eq(&["", "/bin", "", "", "/usr/bin", ""], ":/bin:::/usr/bin:")); - assert!(join_paths(["/te:st"].iter().cloned()).is_err()); + assert!(join_paths(["/te:st"]).is_err()); } #[test] @@ -86,7 +86,7 @@ fn join_paths_windows() { assert!(test_eq(&[r"c:\windows", r"c:\"], r"c:\windows;c:\")); assert!(test_eq(&["", r"c:\windows", "", "", r"c:\", ""], r";c:\windows;;;c:\;")); assert!(test_eq(&[r"c:\te;st", r"c:\"], r#""c:\te;st";c:\"#)); - assert!(join_paths([r#"c:\te"st"#].iter().cloned()).is_err()); + assert!(join_paths([r#"c:\te"st"#]).is_err()); } #[test]