Skip to content

Commit

Permalink
Deprecate into_iter_on_array lint
Browse files Browse the repository at this point in the history
This lint was uplifted/reimplemented by rustc.
Rustup to rust-lang/rust#66017
  • Loading branch information
flip1995 committed Nov 7, 2019
1 parent e917b01 commit da650c4
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 130 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.

[There are 332 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are 331 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)

We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:

Expand Down
9 changes: 9 additions & 0 deletions clippy_lints/src/deprecated_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@ declare_deprecated_lint! {
pub UNUSED_COLLECT,
"`collect` has been marked as #[must_use] in rustc and that covers all cases of this lint"
}

/// **What it does:** Nothing. This lint has been deprecated.
///
/// **Deprecation reason:** This lint has been uplifted to rustc and is now called
/// `array_into_iter`.
declare_deprecated_lint! {
pub INTO_ITER_ON_ARRAY,
"this lint has been uplifted to rustc and is now called `array_into_iter`"
}
7 changes: 4 additions & 3 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
"clippy::unused_collect",
"`collect` has been marked as #[must_use] in rustc and that covers all cases of this lint",
);
store.register_removed(
"clippy::into_iter_on_array",
"this lint has been uplifted to rustc and is now called `array_into_iter`",
);
// end deprecated lints, do not remove this comment, it’s used in `update_lints`

// begin register lints, do not remove this comment, it’s used in `update_lints`
Expand Down Expand Up @@ -584,7 +588,6 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
&methods::FLAT_MAP_IDENTITY,
&methods::GET_UNWRAP,
&methods::INEFFICIENT_TO_STRING,
&methods::INTO_ITER_ON_ARRAY,
&methods::INTO_ITER_ON_REF,
&methods::ITER_CLONED_COLLECT,
&methods::ITER_NTH,
Expand Down Expand Up @@ -1142,7 +1145,6 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
LintId::of(&methods::FILTER_NEXT),
LintId::of(&methods::FLAT_MAP_IDENTITY),
LintId::of(&methods::INEFFICIENT_TO_STRING),
LintId::of(&methods::INTO_ITER_ON_ARRAY),
LintId::of(&methods::INTO_ITER_ON_REF),
LintId::of(&methods::ITER_CLONED_COLLECT),
LintId::of(&methods::ITER_NTH),
Expand Down Expand Up @@ -1481,7 +1483,6 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
LintId::of(&mem_discriminant::MEM_DISCRIMINANT_NON_ENUM),
LintId::of(&mem_replace::MEM_REPLACE_WITH_UNINIT),
LintId::of(&methods::CLONE_DOUBLE_REF),
LintId::of(&methods::INTO_ITER_ON_ARRAY),
LintId::of(&methods::TEMPORARY_CSTRING_AS_PTR),
LintId::of(&methods::UNINIT_ASSUMED_INIT),
LintId::of(&minmax::MIN_MAX),
Expand Down
45 changes: 4 additions & 41 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,34 +968,6 @@ declare_clippy_lint! {
"using `filter_map` when a more succinct alternative exists"
}

declare_clippy_lint! {
/// **What it does:** Checks for `into_iter` calls on types which should be replaced by `iter` or
/// `iter_mut`.
///
/// **Why is this bad?** Arrays and `PathBuf` do not yet have an `into_iter` method which move out
/// their content into an iterator. Auto-referencing resolves the `into_iter` call to its reference
/// instead, like `<&[T; N] as IntoIterator>::into_iter`, which just iterates over item references
/// like calling `iter` would. Furthermore, when the standard library actually
/// [implements the `into_iter` method](https://github.com/rust-lang/rust/issues/25725) which moves
/// the content out of the array, the original use of `into_iter` got inferred with the wrong type
/// and the code will be broken.
///
/// **Known problems:** None
///
/// **Example:**
///
/// ```rust
/// let _ = [1, 2, 3].into_iter().map(|x| *x).collect::<Vec<u32>>();
/// ```
/// Could be written as:
/// ```rust
/// let _ = [1, 2, 3].iter().map(|x| *x).collect::<Vec<u32>>();
/// ```
pub INTO_ITER_ON_ARRAY,
correctness,
"using `.into_iter()` on an array"
}

declare_clippy_lint! {
/// **What it does:** Checks for `into_iter` calls on references which should be replaced by `iter`
/// or `iter_mut`.
Expand Down Expand Up @@ -1133,7 +1105,6 @@ declare_lint_pass!(Methods => [
USELESS_ASREF,
UNNECESSARY_FOLD,
UNNECESSARY_FILTER_MAP,
INTO_ITER_ON_ARRAY,
INTO_ITER_ON_REF,
SUSPICIOUS_MAP,
UNINIT_ASSUMED_INIT,
Expand Down Expand Up @@ -2786,16 +2757,8 @@ fn lint_asref(cx: &LateContext<'_, '_>, expr: &hir::Expr, call_name: &str, as_re
}
}

fn ty_has_iter_method(
cx: &LateContext<'_, '_>,
self_ref_ty: Ty<'_>,
) -> Option<(&'static Lint, &'static str, &'static str)> {
fn ty_has_iter_method(cx: &LateContext<'_, '_>, self_ref_ty: Ty<'_>) -> Option<(&'static str, &'static str)> {
has_iter_method(cx, self_ref_ty).map(|ty_name| {
let lint = if ty_name == "array" || ty_name == "PathBuf" {
INTO_ITER_ON_ARRAY
} else {
INTO_ITER_ON_REF
};
let mutbl = match self_ref_ty.kind {
ty::Ref(_, _, mutbl) => mutbl,
_ => unreachable!(),
Expand All @@ -2804,18 +2767,18 @@ fn ty_has_iter_method(
hir::MutImmutable => "iter",
hir::MutMutable => "iter_mut",
};
(lint, ty_name, method_name)
(ty_name, method_name)
})
}

fn lint_into_iter(cx: &LateContext<'_, '_>, expr: &hir::Expr, self_ref_ty: Ty<'_>, method_span: Span) {
if !match_trait_method(cx, expr, &paths::INTO_ITERATOR) {
return;
}
if let Some((lint, kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) {
if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) {
span_lint_and_sugg(
cx,
lint,
INTO_ITER_ON_REF,
method_span,
&format!(
"this .into_iter() call is equivalent to .{}() and will not move the {}",
Expand Down
9 changes: 1 addition & 8 deletions src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS;

// begin lint list, do not remove this comment, it’s used in `update_lints`
pub const ALL_LINTS: [Lint; 332] = [
pub const ALL_LINTS: [Lint; 331] = [
Lint {
name: "absurd_extreme_comparisons",
group: "correctness",
Expand Down Expand Up @@ -812,13 +812,6 @@ pub const ALL_LINTS: [Lint; 332] = [
deprecation: None,
module: "integer_division",
},
Lint {
name: "into_iter_on_array",
group: "correctness",
desc: "using `.into_iter()` on an array",
deprecation: None,
module: "methods",
},
Lint {
name: "into_iter_on_ref",
group: "style",
Expand Down
1 change: 1 addition & 0 deletions tests/ui/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#[warn(clippy::misaligned_transmute)]
#[warn(clippy::unused_collect)]
#[warn(clippy::invalid_ref)]
#[warn(clippy::into_iter_on_array)]

fn main() {}
8 changes: 7 additions & 1 deletion tests/ui/deprecated.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ error: lint `clippy::invalid_ref` has been removed: `superseded by rustc lint `i
LL | #[warn(clippy::invalid_ref)]
| ^^^^^^^^^^^^^^^^^^^

error: lint `clippy::into_iter_on_array` has been removed: `this lint has been uplifted to rustc and is now called `array_into_iter``
--> $DIR/deprecated.rs:8:8
|
LL | #[warn(clippy::into_iter_on_array)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: lint `clippy::str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
--> $DIR/deprecated.rs:1:8
|
LL | #[warn(clippy::str_to_string)]
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors
error: aborting due to 9 previous errors

5 changes: 1 addition & 4 deletions tests/ui/for_loop_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Unrelated {
clippy::cognitive_complexity,
clippy::similar_names
)]
#[allow(clippy::many_single_char_names, unused_variables, clippy::into_iter_on_array)]
#[allow(clippy::many_single_char_names, unused_variables)]
fn main() {
const MAX_LEN: usize = 42;
let mut vec = vec![1, 2, 3, 4];
Expand Down Expand Up @@ -102,9 +102,6 @@ fn main() {
let out_vec = vec![1, 2, 3];
for _v in out_vec {}

let array = [1, 2, 3];
for _v in &array {}

for _v in &vec {} // these are fine
for _v in &mut vec {} // these are fine

Expand Down
5 changes: 1 addition & 4 deletions tests/ui/for_loop_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Unrelated {
clippy::cognitive_complexity,
clippy::similar_names
)]
#[allow(clippy::many_single_char_names, unused_variables, clippy::into_iter_on_array)]
#[allow(clippy::many_single_char_names, unused_variables)]
fn main() {
const MAX_LEN: usize = 42;
let mut vec = vec![1, 2, 3, 4];
Expand Down Expand Up @@ -102,9 +102,6 @@ fn main() {
let out_vec = vec![1, 2, 3];
for _v in out_vec.into_iter() {}

let array = [1, 2, 3];
for _v in array.into_iter() {}

for _v in &vec {} // these are fine
for _v in &mut vec {} // these are fine

Expand Down
26 changes: 10 additions & 16 deletions tests/ui/for_loop_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -77,64 +77,58 @@ LL | for _v in out_vec.into_iter() {}
= note: `-D clippy::explicit-into-iter-loop` implied by `-D warnings`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:106:15
|
LL | for _v in array.into_iter() {}
| ^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&array`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:111:15
--> $DIR/for_loop_fixable.rs:108:15
|
LL | for _v in [1, 2, 3].iter() {}
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[1, 2, 3]`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:115:15
--> $DIR/for_loop_fixable.rs:112:15
|
LL | for _v in [0; 32].iter() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 32]`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:120:15
--> $DIR/for_loop_fixable.rs:117:15
|
LL | for _v in ll.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&ll`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:123:15
--> $DIR/for_loop_fixable.rs:120:15
|
LL | for _v in vd.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&vd`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:126:15
--> $DIR/for_loop_fixable.rs:123:15
|
LL | for _v in bh.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bh`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:129:15
--> $DIR/for_loop_fixable.rs:126:15
|
LL | for _v in hm.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&hm`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:132:15
--> $DIR/for_loop_fixable.rs:129:15
|
LL | for _v in bt.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bt`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:135:15
--> $DIR/for_loop_fixable.rs:132:15
|
LL | for _v in hs.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&hs`

error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop_fixable.rs:138:15
--> $DIR/for_loop_fixable.rs:135:15
|
LL | for _v in bs.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bs`

error: aborting due to 18 previous errors
error: aborting due to 17 previous errors

2 changes: 1 addition & 1 deletion tests/ui/for_loop_unfixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
unused,
dead_code
)]
#[allow(clippy::many_single_char_names, unused_variables, clippy::into_iter_on_array)]
#[allow(clippy::many_single_char_names, unused_variables)]
fn main() {
for i in 5..5 {
println!("{}", i);
Expand Down
3 changes: 0 additions & 3 deletions tests/ui/into_iter_on_ref.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// run-rustfix
#![allow(clippy::useless_vec)]
#![warn(clippy::into_iter_on_ref)]
#![deny(clippy::into_iter_on_array)]

struct X;
use std::collections::*;
Expand All @@ -10,9 +9,7 @@ fn main() {
for _ in &[1, 2, 3] {}
for _ in vec![X, X] {}
for _ in &vec![X, X] {}
for _ in [1, 2, 3].iter() {} //~ ERROR equivalent to .iter()

let _ = [1, 2, 3].iter(); //~ ERROR equivalent to .iter()
let _ = vec![1, 2, 3].into_iter();
let _ = (&vec![1, 2, 3]).iter(); //~ WARN equivalent to .iter()
let _ = vec![1, 2, 3].into_boxed_slice().iter(); //~ WARN equivalent to .iter()
Expand Down
3 changes: 0 additions & 3 deletions tests/ui/into_iter_on_ref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// run-rustfix
#![allow(clippy::useless_vec)]
#![warn(clippy::into_iter_on_ref)]
#![deny(clippy::into_iter_on_array)]

struct X;
use std::collections::*;
Expand All @@ -10,9 +9,7 @@ fn main() {
for _ in &[1, 2, 3] {}
for _ in vec![X, X] {}
for _ in &vec![X, X] {}
for _ in [1, 2, 3].into_iter() {} //~ ERROR equivalent to .iter()

let _ = [1, 2, 3].into_iter(); //~ ERROR equivalent to .iter()
let _ = vec![1, 2, 3].into_iter();
let _ = (&vec![1, 2, 3]).into_iter(); //~ WARN equivalent to .iter()
let _ = vec![1, 2, 3].into_boxed_slice().into_iter(); //~ WARN equivalent to .iter()
Expand Down
Loading

0 comments on commit da650c4

Please sign in to comment.