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

Make explicit_iter_loop and explicit_into_iter_loop allow-by-default, so
that people can turn them on if they want to enforce that style; avoid
presenting them as *the* idiomatic Rust style, rather than just *a* style.
This commit is contained in:
Josh Triplett 2018-09-03 00:19:59 -07:00
parent 2b7a5304f6
commit 779988303a
3 changed files with 19 additions and 21 deletions

View file

@ -449,6 +449,8 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
if_not_else::IF_NOT_ELSE, if_not_else::IF_NOT_ELSE,
infinite_iter::MAYBE_INFINITE_ITER, infinite_iter::MAYBE_INFINITE_ITER,
items_after_statements::ITEMS_AFTER_STATEMENTS, items_after_statements::ITEMS_AFTER_STATEMENTS,
loops::EXPLICIT_INTO_ITER_LOOP,
loops::EXPLICIT_ITER_LOOP,
matches::SINGLE_MATCH_ELSE, matches::SINGLE_MATCH_ELSE,
methods::FILTER_MAP, methods::FILTER_MAP,
methods::OPTION_MAP_UNWRAP_OR, methods::OPTION_MAP_UNWRAP_OR,
@ -546,8 +548,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
literal_representation::UNREADABLE_LITERAL, literal_representation::UNREADABLE_LITERAL,
loops::EMPTY_LOOP, loops::EMPTY_LOOP,
loops::EXPLICIT_COUNTER_LOOP, loops::EXPLICIT_COUNTER_LOOP,
loops::EXPLICIT_INTO_ITER_LOOP,
loops::EXPLICIT_ITER_LOOP,
loops::FOR_KV_MAP, loops::FOR_KV_MAP,
loops::FOR_LOOP_OVER_OPTION, loops::FOR_LOOP_OVER_OPTION,
loops::FOR_LOOP_OVER_RESULT, loops::FOR_LOOP_OVER_RESULT,
@ -718,8 +718,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
literal_representation::LARGE_DIGIT_GROUPS, literal_representation::LARGE_DIGIT_GROUPS,
literal_representation::UNREADABLE_LITERAL, literal_representation::UNREADABLE_LITERAL,
loops::EMPTY_LOOP, loops::EMPTY_LOOP,
loops::EXPLICIT_INTO_ITER_LOOP,
loops::EXPLICIT_ITER_LOOP,
loops::FOR_KV_MAP, loops::FOR_KV_MAP,
loops::NEEDLESS_RANGE_LOOP, loops::NEEDLESS_RANGE_LOOP,
loops::WHILE_LET_ON_ITERATOR, loops::WHILE_LET_ON_ITERATOR,

View file

@ -85,7 +85,7 @@ declare_clippy_lint! {
/// ``` /// ```
declare_clippy_lint! { declare_clippy_lint! {
pub EXPLICIT_ITER_LOOP, pub EXPLICIT_ITER_LOOP,
style, pedantic,
"for-looping over `_.iter()` or `_.iter_mut()` when `&_` or `&mut _` would do" "for-looping over `_.iter()` or `_.iter_mut()` when `&_` or `&mut _` would do"
} }
@ -107,7 +107,7 @@ declare_clippy_lint! {
/// ``` /// ```
declare_clippy_lint! { declare_clippy_lint! {
pub EXPLICIT_INTO_ITER_LOOP, pub EXPLICIT_INTO_ITER_LOOP,
style, pedantic,
"for-looping over `_.into_iter()` when `_` would do" "for-looping over `_.into_iter()` when `_` would do"
} }
@ -1209,7 +1209,7 @@ fn lint_iter_method(cx: &LateContext<'_, '_>, args: &[Expr], arg: &Expr, method_
cx, cx,
EXPLICIT_ITER_LOOP, EXPLICIT_ITER_LOOP,
arg.span, 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", iteration methods",
"to write this more concisely, try", "to write this more concisely, try",
format!("&{}{}", muta, object), format!("&{}{}", muta, object),
@ -1247,7 +1247,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat, arg: &Expr, expr: &Ex
cx, cx,
EXPLICIT_INTO_ITER_LOOP, EXPLICIT_INTO_ITER_LOOP,
arg.span, 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`", iteration methods`",
"to write this more concisely, try", "to write this more concisely, try",
object.to_string(), object.to_string(),

View file

@ -264,7 +264,7 @@ error: this range is empty so this for loop will never run
193 | for i in (5 + 2)..(8 - 1) { 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 --> $DIR/for_loop.rs:215:15
| |
215 | for _v in vec.iter() {} 215 | for _v in vec.iter() {}
@ -272,13 +272,13 @@ error: it is more idiomatic to loop over references to containers instead of usi
| |
= note: `-D clippy::explicit-iter-loop` implied by `-D warnings` = 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 --> $DIR/for_loop.rs:217:15
| |
217 | for _v in vec.iter_mut() {} 217 | for _v in vec.iter_mut() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut vec` | ^^^^^^^^^^^^^^ 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 --> $DIR/for_loop.rs:220:15
| |
220 | for _v in out_vec.into_iter() {} 220 | for _v in out_vec.into_iter() {}
@ -286,61 +286,61 @@ error: it is more idiomatic to loop over containers instead of using explicit it
| |
= note: `-D clippy::explicit-into-iter-loop` implied by `-D warnings` = 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 --> $DIR/for_loop.rs:223:15
| |
223 | for _v in array.into_iter() {} 223 | for _v in array.into_iter() {}
| ^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&array` | ^^^^^^^^^^^^^^^^^ 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 --> $DIR/for_loop.rs:228:15
| |
228 | for _v in [1, 2, 3].iter() {} 228 | for _v in [1, 2, 3].iter() {}
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[1, 2, 3]` | ^^^^^^^^^^^^^^^^ 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 --> $DIR/for_loop.rs:232:15
| |
232 | for _v in [0; 32].iter() {} 232 | for _v in [0; 32].iter() {}
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 32]` | ^^^^^^^^^^^^^^ 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 --> $DIR/for_loop.rs:237:15
| |
237 | for _v in ll.iter() {} 237 | for _v in ll.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&ll` | ^^^^^^^^^ 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 --> $DIR/for_loop.rs:240:15
| |
240 | for _v in vd.iter() {} 240 | for _v in vd.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&vd` | ^^^^^^^^^ 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 --> $DIR/for_loop.rs:243:15
| |
243 | for _v in bh.iter() {} 243 | for _v in bh.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bh` | ^^^^^^^^^ 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 --> $DIR/for_loop.rs:246:15
| |
246 | for _v in hm.iter() {} 246 | for _v in hm.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&hm` | ^^^^^^^^^ 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 --> $DIR/for_loop.rs:249:15
| |
249 | for _v in bt.iter() {} 249 | for _v in bt.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&bt` | ^^^^^^^^^ 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 --> $DIR/for_loop.rs:252:15
| |
252 | for _v in hs.iter() {} 252 | for _v in hs.iter() {}
| ^^^^^^^^^ help: to write this more concisely, try: `&hs` | ^^^^^^^^^ 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 --> $DIR/for_loop.rs:255:15
| |
255 | for _v in bs.iter() {} 255 | for _v in bs.iter() {}