Skip to content

iter conservation efforts: save the endangered .iter() and .into_iter() #3119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
if_not_else::IF_NOT_ELSE,
infinite_iter::MAYBE_INFINITE_ITER,
items_after_statements::ITEMS_AFTER_STATEMENTS,
loops::EXPLICIT_INTO_ITER_LOOP,
loops::EXPLICIT_ITER_LOOP,
matches::SINGLE_MATCH_ELSE,
methods::FILTER_MAP,
methods::OPTION_MAP_UNWRAP_OR,
Expand Down Expand Up @@ -546,8 +548,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
literal_representation::UNREADABLE_LITERAL,
loops::EMPTY_LOOP,
loops::EXPLICIT_COUNTER_LOOP,
loops::EXPLICIT_INTO_ITER_LOOP,
loops::EXPLICIT_ITER_LOOP,
loops::FOR_KV_MAP,
loops::FOR_LOOP_OVER_OPTION,
loops::FOR_LOOP_OVER_RESULT,
Expand Down Expand Up @@ -718,8 +718,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
literal_representation::LARGE_DIGIT_GROUPS,
literal_representation::UNREADABLE_LITERAL,
loops::EMPTY_LOOP,
loops::EXPLICIT_INTO_ITER_LOOP,
loops::EXPLICIT_ITER_LOOP,
loops::FOR_KV_MAP,
loops::NEEDLESS_RANGE_LOOP,
loops::WHILE_LET_ON_ITERATOR,
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ declare_clippy_lint! {
/// ```
declare_clippy_lint! {
pub EXPLICIT_ITER_LOOP,
style,
pedantic,
"for-looping over `_.iter()` or `_.iter_mut()` when `&_` or `&mut _` would do"
}

Expand All @@ -107,7 +107,7 @@ declare_clippy_lint! {
/// ```
declare_clippy_lint! {
pub EXPLICIT_INTO_ITER_LOOP,
style,
pedantic,
"for-looping over `_.into_iter()` when `_` would do"
}

Expand Down Expand Up @@ -1209,7 +1209,7 @@ fn lint_iter_method(cx: &LateContext<'_, '_>, args: &[Expr], arg: &Expr, method_
cx,
EXPLICIT_ITER_LOOP,
arg.span,
"it is more idiomatic to loop over references to containers instead of using explicit \
"it is more concise to loop over references to containers instead of using explicit \
iteration methods",
"to write this more concisely, try",
format!("&{}{}", muta, object),
Expand Down Expand Up @@ -1247,7 +1247,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat, arg: &Expr, expr: &Ex
cx,
EXPLICIT_INTO_ITER_LOOP,
arg.span,
"it is more idiomatic to loop over containers instead of using explicit \
"it is more concise to loop over containers instead of using explicit \
iteration methods`",
"to write this more concisely, try",
object.to_string(),
Expand Down
26 changes: 13 additions & 13 deletions tests/ui/for_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -264,83 +264,83 @@ error: this range is empty so this for loop will never run
193 | for i in (5 + 2)..(8 - 1) {
| ^^^^^^^^^^^^^^^^

error: it is more idiomatic to loop over references to containers instead of using explicit iteration methods
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop.rs:215:15
|
215 | for _v in vec.iter() {}
| ^^^^^^^^^^ help: to write this more concisely, try: `&vec`
|
= note: `-D clippy::explicit-iter-loop` implied by `-D warnings`

error: it is more idiomatic to loop over references to containers instead of using explicit iteration methods
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop.rs:217:15
|
217 | for _v in vec.iter_mut() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut vec`

error: it is more idiomatic to loop over containers instead of using explicit iteration methods`
error: it is more concise to loop over containers instead of using explicit iteration methods`
--> $DIR/for_loop.rs:220:15
|
220 | for _v in out_vec.into_iter() {}
| ^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `out_vec`
|
= note: `-D clippy::explicit-into-iter-loop` implied by `-D warnings`

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

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

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

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

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

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

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

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

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

error: it is more idiomatic to loop over references to containers instead of using explicit iteration methods
error: it is more concise to loop over references to containers instead of using explicit iteration methods
--> $DIR/for_loop.rs:255:15
|
255 | for _v in bs.iter() {}
Expand Down