From 51617b83a18b2a0b1f0f2cae8c43e42043a86b76 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 25 Feb 2021 23:06:50 +0900 Subject: [PATCH 1/9] new lint: iter_count --- CHANGELOG.md | 1 + clippy_lints/src/lib.rs | 3 ++ clippy_lints/src/methods/mod.rs | 62 ++++++++++++++++++++++++++++ tests/ui/auxiliary/option_helpers.rs | 4 ++ tests/ui/iter_count.fixed | 58 ++++++++++++++++++++++++++ tests/ui/iter_count.rs | 58 ++++++++++++++++++++++++++ tests/ui/iter_count.stderr | 52 +++++++++++++++++++++++ 7 files changed, 238 insertions(+) create mode 100644 tests/ui/iter_count.fixed create mode 100644 tests/ui/iter_count.rs create mode 100644 tests/ui/iter_count.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index 687f74472020..41c334c68169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2135,6 +2135,7 @@ Released 2018-09-13 [`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters [`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements [`iter_cloned_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_cloned_collect +[`iter_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_count [`iter_next_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop [`iter_next_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_slice [`iter_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index fa8f03eb4453..1ace4c8a10c9 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -775,6 +775,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &methods::INTO_ITER_ON_REF, &methods::ITERATOR_STEP_BY_ZERO, &methods::ITER_CLONED_COLLECT, + &methods::ITER_COUNT, &methods::ITER_NEXT_SLICE, &methods::ITER_NTH, &methods::ITER_NTH_ZERO, @@ -1577,6 +1578,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&methods::INTO_ITER_ON_REF), LintId::of(&methods::ITERATOR_STEP_BY_ZERO), LintId::of(&methods::ITER_CLONED_COLLECT), + LintId::of(&methods::ITER_COUNT), LintId::of(&methods::ITER_NEXT_SLICE), LintId::of(&methods::ITER_NTH), LintId::of(&methods::ITER_NTH_ZERO), @@ -1881,6 +1883,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&methods::FILTER_NEXT), LintId::of(&methods::FLAT_MAP_IDENTITY), LintId::of(&methods::INSPECT_FOR_EACH), + LintId::of(&methods::ITER_COUNT), LintId::of(&methods::MANUAL_FILTER_MAP), LintId::of(&methods::MANUAL_FIND_MAP), LintId::of(&methods::OPTION_AS_REF_DEREF), diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 6f491144435a..17e7f6f662db 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1540,6 +1540,32 @@ declare_clippy_lint! { "implicitly cloning a value by invoking a function on its dereferenced type" } +declare_clippy_lint! { + /// **What it does:** Checks for the use of `.iter().count()`. + /// + /// **Why is this bad?** `.len()` is more efficient and more + /// readable. + /// + /// **Known problems:** None. + /// + /// **Example:** + /// + /// ```rust + /// // Bad + /// let some_vec = vec![0, 1, 2, 3]; + /// let _ = some_vec.iter().count(); + /// let _ = &some_vec[..].iter().count(); + /// + /// // Good + /// let some_vec = vec![0, 1, 2, 3]; + /// let _ = some_vec.len(); + /// let _ = &some_vec[..].len(); + /// ``` + pub ITER_COUNT, + complexity, + "replace `.iter().count()` with `.len()`" +} + pub struct Methods { msrv: Option, } @@ -1585,6 +1611,7 @@ impl_lint_pass!(Methods => [ MAP_FLATTEN, ITERATOR_STEP_BY_ZERO, ITER_NEXT_SLICE, + ITER_COUNT, ITER_NTH, ITER_NTH_ZERO, BYTES_NTH, @@ -1664,6 +1691,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods { lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0], method_spans[1]) }, ["extend", ..] => lint_extend(cx, expr, arg_lists[0]), + ["count", "iter"] => lint_iter_count(cx, expr, &arg_lists[1], false), + ["count", "iter_mut"] => lint_iter_count(cx, expr, &arg_lists[1], true), ["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false), ["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true), ["nth", "bytes"] => bytes_nth::lints(cx, expr, &arg_lists[1]), @@ -2632,6 +2661,39 @@ fn lint_iter_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, iter_ } } +fn lint_iter_count<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) { + let mut_str = if is_mut { "_mut" } else { "" }; + if_chain! { + let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() { + Some("slice") + } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) { + Some("Vec") + } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) { + Some("VecDeque") + } else if match_trait_method(cx, expr, &paths::ITERATOR) { + Some("std::iter::Iterator") + } else { + None + }; + if let Some(caller_type) = caller_type; + then { + let mut applicability = Applicability::MachineApplicable; + span_lint_and_sugg( + cx, + ITER_COUNT, + expr.span, + &format!("called `.iter{}().count()` on a `{}`", mut_str, caller_type), + "try", + format!( + "{}.len()", + snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability), + ), + applicability, + ); + } + } +} + fn lint_iter_nth<'tcx>( cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, diff --git a/tests/ui/auxiliary/option_helpers.rs b/tests/ui/auxiliary/option_helpers.rs index ed11c41e21c1..7dc3f4ebd4d4 100644 --- a/tests/ui/auxiliary/option_helpers.rs +++ b/tests/ui/auxiliary/option_helpers.rs @@ -48,4 +48,8 @@ impl IteratorFalsePositives { pub fn skip_while(self) -> IteratorFalsePositives { self } + + pub fn count(self) -> usize { + self.foo as usize + } } diff --git a/tests/ui/iter_count.fixed b/tests/ui/iter_count.fixed new file mode 100644 index 000000000000..14cae1cd73e4 --- /dev/null +++ b/tests/ui/iter_count.fixed @@ -0,0 +1,58 @@ +// run-rustfix +// aux-build:option_helpers.rs + +#![warn(clippy::iter_count)] +#![allow(unused_variables)] +#![allow(unused_mut)] + +extern crate option_helpers; + +use option_helpers::IteratorFalsePositives; +use std::collections::{HashSet, VecDeque}; + +/// Struct to generate false positives for things with `.iter()`. +#[derive(Copy, Clone)] +struct HasIter; + +impl HasIter { + fn iter(self) -> IteratorFalsePositives { + IteratorFalsePositives { foo: 0 } + } + + fn iter_mut(self) -> IteratorFalsePositives { + IteratorFalsePositives { foo: 0 } + } +} + +fn main() { + let mut some_vec = vec![0, 1, 2, 3]; + let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); + let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect(); + let mut some_hash_set = HashSet::new(); + some_hash_set.insert(1); + + { + // Make sure we lint `.iter()` for relevant types. + let bad_vec = some_vec.len(); + let bad_slice = &some_vec[..].len(); + let bad_boxed_slice = boxed_slice.len(); + let bad_vec_deque = some_vec_deque.len(); + let bad_hash_set = some_hash_set.len(); + } + + { + // Make sure we lint `.iter_mut()` for relevant types. + let bad_vec = some_vec.len(); + } + { + let bad_slice = &some_vec[..].len(); + } + { + let bad_vec_deque = some_vec_deque.len(); + } + + // Make sure we don't lint for non-relevant types. + let false_positive = HasIter; + let ok = false_positive.iter().count(); + let ok_mut = false_positive.iter_mut().count(); +} diff --git a/tests/ui/iter_count.rs b/tests/ui/iter_count.rs new file mode 100644 index 000000000000..dfe02e5d53e2 --- /dev/null +++ b/tests/ui/iter_count.rs @@ -0,0 +1,58 @@ +// run-rustfix +// aux-build:option_helpers.rs + +#![warn(clippy::iter_count)] +#![allow(unused_variables)] +#![allow(unused_mut)] + +extern crate option_helpers; + +use option_helpers::IteratorFalsePositives; +use std::collections::{HashSet, VecDeque}; + +/// Struct to generate false positives for things with `.iter()`. +#[derive(Copy, Clone)] +struct HasIter; + +impl HasIter { + fn iter(self) -> IteratorFalsePositives { + IteratorFalsePositives { foo: 0 } + } + + fn iter_mut(self) -> IteratorFalsePositives { + IteratorFalsePositives { foo: 0 } + } +} + +fn main() { + let mut some_vec = vec![0, 1, 2, 3]; + let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); + let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect(); + let mut some_hash_set = HashSet::new(); + some_hash_set.insert(1); + + { + // Make sure we lint `.iter()` for relevant types. + let bad_vec = some_vec.iter().count(); + let bad_slice = &some_vec[..].iter().count(); + let bad_boxed_slice = boxed_slice.iter().count(); + let bad_vec_deque = some_vec_deque.iter().count(); + let bad_hash_set = some_hash_set.iter().count(); + } + + { + // Make sure we lint `.iter_mut()` for relevant types. + let bad_vec = some_vec.iter_mut().count(); + } + { + let bad_slice = &some_vec[..].iter_mut().count(); + } + { + let bad_vec_deque = some_vec_deque.iter_mut().count(); + } + + // Make sure we don't lint for non-relevant types. + let false_positive = HasIter; + let ok = false_positive.iter().count(); + let ok_mut = false_positive.iter_mut().count(); +} diff --git a/tests/ui/iter_count.stderr b/tests/ui/iter_count.stderr new file mode 100644 index 000000000000..a465d2219945 --- /dev/null +++ b/tests/ui/iter_count.stderr @@ -0,0 +1,52 @@ +error: called `.iter().count()` on a `Vec` + --> $DIR/iter_count.rs:36:23 + | +LL | let bad_vec = some_vec.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec.len()` + | + = note: `-D clippy::iter-count` implied by `-D warnings` + +error: called `.iter().count()` on a `slice` + --> $DIR/iter_count.rs:37:26 + | +LL | let bad_slice = &some_vec[..].iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec[..].len()` + +error: called `.iter().count()` on a `slice` + --> $DIR/iter_count.rs:38:31 + | +LL | let bad_boxed_slice = boxed_slice.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `boxed_slice.len()` + +error: called `.iter().count()` on a `VecDeque` + --> $DIR/iter_count.rs:39:29 + | +LL | let bad_vec_deque = some_vec_deque.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec_deque.len()` + +error: called `.iter().count()` on a `std::iter::Iterator` + --> $DIR/iter_count.rs:40:28 + | +LL | let bad_hash_set = some_hash_set.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_hash_set.len()` + +error: called `.iter_mut().count()` on a `Vec` + --> $DIR/iter_count.rs:45:23 + | +LL | let bad_vec = some_vec.iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec.len()` + +error: called `.iter_mut().count()` on a `slice` + --> $DIR/iter_count.rs:48:26 + | +LL | let bad_slice = &some_vec[..].iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec[..].len()` + +error: called `.iter_mut().count()` on a `VecDeque` + --> $DIR/iter_count.rs:51:29 + | +LL | let bad_vec_deque = some_vec_deque.iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec_deque.len()` + +error: aborting due to 8 previous errors + From 7223ee6590bb74e2a7ad02361b51cfa58178c740 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 25 Feb 2021 23:07:15 +0900 Subject: [PATCH 2/9] allow clippy::iter_count --- tests/ui/needless_collect.fixed | 2 +- tests/ui/needless_collect.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ui/needless_collect.fixed b/tests/ui/needless_collect.fixed index 7f2fcf02f6b5..af6c7bf15ea6 100644 --- a/tests/ui/needless_collect.fixed +++ b/tests/ui/needless_collect.fixed @@ -1,6 +1,6 @@ // run-rustfix -#![allow(unused, clippy::suspicious_map)] +#![allow(unused, clippy::suspicious_map, clippy::iter_count)] use std::collections::{BTreeSet, HashMap, HashSet}; diff --git a/tests/ui/needless_collect.rs b/tests/ui/needless_collect.rs index 788a9eb3264e..6ae14f370b14 100644 --- a/tests/ui/needless_collect.rs +++ b/tests/ui/needless_collect.rs @@ -1,6 +1,6 @@ // run-rustfix -#![allow(unused, clippy::suspicious_map)] +#![allow(unused, clippy::suspicious_map, clippy::iter_count)] use std::collections::{BTreeSet, HashMap, HashSet}; From 204b27937c488eb7c1b81c31d56779861ff488c3 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 26 Feb 2021 01:54:51 +0900 Subject: [PATCH 3/9] lint for `into_iter().count()` --- clippy_lints/src/methods/mod.rs | 11 ++++- tests/ui/iter_count.fixed | 58 +++++++++++++------------- tests/ui/iter_count.rs | 58 +++++++++++++------------- tests/ui/iter_count.stderr | 72 ++++++++++++++++++++------------- 4 files changed, 116 insertions(+), 83 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 17e7f6f662db..e5509ee2c4e0 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1691,7 +1691,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0], method_spans[1]) }, ["extend", ..] => lint_extend(cx, expr, arg_lists[0]), - ["count", "iter"] => lint_iter_count(cx, expr, &arg_lists[1], false), + ["count", "into_iter" | "iter"] => lint_iter_count(cx, expr, &arg_lists[1], false), ["count", "iter_mut"] => lint_iter_count(cx, expr, &arg_lists[1], true), ["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false), ["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true), @@ -2663,6 +2663,13 @@ fn lint_iter_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, iter_ fn lint_iter_count<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) { let mut_str = if is_mut { "_mut" } else { "" }; + let iter_method = if method_chain_args(expr, &[format!("iter{}", mut_str).as_str(), "count"]).is_some() { + "iter" + } else if method_chain_args(expr, &["into_iter", "count"]).is_some() { + "into_iter" + } else { + return; + }; if_chain! { let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() { Some("slice") @@ -2682,7 +2689,7 @@ fn lint_iter_count<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'t cx, ITER_COUNT, expr.span, - &format!("called `.iter{}().count()` on a `{}`", mut_str, caller_type), + &format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type), "try", format!( "{}.len()", diff --git a/tests/ui/iter_count.fixed b/tests/ui/iter_count.fixed index 14cae1cd73e4..c8f896408479 100644 --- a/tests/ui/iter_count.fixed +++ b/tests/ui/iter_count.fixed @@ -2,8 +2,13 @@ // aux-build:option_helpers.rs #![warn(clippy::iter_count)] -#![allow(unused_variables)] -#![allow(unused_mut)] +#![allow( + unused_variables, + array_into_iter, + unused_mut, + clippy::into_iter_on_ref, + clippy::unnecessary_operation +)] extern crate option_helpers; @@ -22,37 +27,36 @@ impl HasIter { fn iter_mut(self) -> IteratorFalsePositives { IteratorFalsePositives { foo: 0 } } + + fn into_iter(self) -> IteratorFalsePositives { + IteratorFalsePositives { foo: 0 } + } } fn main() { - let mut some_vec = vec![0, 1, 2, 3]; + let mut vec = vec![0, 1, 2, 3]; let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); - let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect(); - let mut some_hash_set = HashSet::new(); - some_hash_set.insert(1); - - { - // Make sure we lint `.iter()` for relevant types. - let bad_vec = some_vec.len(); - let bad_slice = &some_vec[..].len(); - let bad_boxed_slice = boxed_slice.len(); - let bad_vec_deque = some_vec_deque.len(); - let bad_hash_set = some_hash_set.len(); - } + let mut vec_deque: VecDeque<_> = vec.iter().cloned().collect(); + let mut hash_set = HashSet::new(); + hash_set.insert(1); - { - // Make sure we lint `.iter_mut()` for relevant types. - let bad_vec = some_vec.len(); - } - { - let bad_slice = &some_vec[..].len(); - } - { - let bad_vec_deque = some_vec_deque.len(); - } + &vec[..].len(); + vec.len(); + boxed_slice.len(); + vec_deque.len(); + hash_set.len(); + + vec.len(); + &vec[..].len(); + vec_deque.len(); + + &vec[..].len(); + vec.len(); + vec_deque.len(); // Make sure we don't lint for non-relevant types. let false_positive = HasIter; - let ok = false_positive.iter().count(); - let ok_mut = false_positive.iter_mut().count(); + false_positive.iter().count(); + false_positive.iter_mut().count(); + false_positive.into_iter().count(); } diff --git a/tests/ui/iter_count.rs b/tests/ui/iter_count.rs index dfe02e5d53e2..8ea17ef34fa6 100644 --- a/tests/ui/iter_count.rs +++ b/tests/ui/iter_count.rs @@ -2,8 +2,13 @@ // aux-build:option_helpers.rs #![warn(clippy::iter_count)] -#![allow(unused_variables)] -#![allow(unused_mut)] +#![allow( + unused_variables, + array_into_iter, + unused_mut, + clippy::into_iter_on_ref, + clippy::unnecessary_operation +)] extern crate option_helpers; @@ -22,37 +27,36 @@ impl HasIter { fn iter_mut(self) -> IteratorFalsePositives { IteratorFalsePositives { foo: 0 } } + + fn into_iter(self) -> IteratorFalsePositives { + IteratorFalsePositives { foo: 0 } + } } fn main() { - let mut some_vec = vec![0, 1, 2, 3]; + let mut vec = vec![0, 1, 2, 3]; let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); - let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect(); - let mut some_hash_set = HashSet::new(); - some_hash_set.insert(1); - - { - // Make sure we lint `.iter()` for relevant types. - let bad_vec = some_vec.iter().count(); - let bad_slice = &some_vec[..].iter().count(); - let bad_boxed_slice = boxed_slice.iter().count(); - let bad_vec_deque = some_vec_deque.iter().count(); - let bad_hash_set = some_hash_set.iter().count(); - } + let mut vec_deque: VecDeque<_> = vec.iter().cloned().collect(); + let mut hash_set = HashSet::new(); + hash_set.insert(1); - { - // Make sure we lint `.iter_mut()` for relevant types. - let bad_vec = some_vec.iter_mut().count(); - } - { - let bad_slice = &some_vec[..].iter_mut().count(); - } - { - let bad_vec_deque = some_vec_deque.iter_mut().count(); - } + &vec[..].iter().count(); + vec.iter().count(); + boxed_slice.iter().count(); + vec_deque.iter().count(); + hash_set.iter().count(); + + vec.iter_mut().count(); + &vec[..].iter_mut().count(); + vec_deque.iter_mut().count(); + + &vec[..].into_iter().count(); + vec.into_iter().count(); + vec_deque.into_iter().count(); // Make sure we don't lint for non-relevant types. let false_positive = HasIter; - let ok = false_positive.iter().count(); - let ok_mut = false_positive.iter_mut().count(); + false_positive.iter().count(); + false_positive.iter_mut().count(); + false_positive.into_iter().count(); } diff --git a/tests/ui/iter_count.stderr b/tests/ui/iter_count.stderr index a465d2219945..0820c0014434 100644 --- a/tests/ui/iter_count.stderr +++ b/tests/ui/iter_count.stderr @@ -1,52 +1,70 @@ -error: called `.iter().count()` on a `Vec` - --> $DIR/iter_count.rs:36:23 +error: called `.iter().count()` on a `slice` + --> $DIR/iter_count.rs:43:6 | -LL | let bad_vec = some_vec.iter().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec.len()` +LL | &vec[..].iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` | = note: `-D clippy::iter-count` implied by `-D warnings` -error: called `.iter().count()` on a `slice` - --> $DIR/iter_count.rs:37:26 +error: called `.iter().count()` on a `Vec` + --> $DIR/iter_count.rs:44:5 | -LL | let bad_slice = &some_vec[..].iter().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec[..].len()` +LL | vec.iter().count(); + | ^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` error: called `.iter().count()` on a `slice` - --> $DIR/iter_count.rs:38:31 + --> $DIR/iter_count.rs:45:5 | -LL | let bad_boxed_slice = boxed_slice.iter().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `boxed_slice.len()` +LL | boxed_slice.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `boxed_slice.len()` error: called `.iter().count()` on a `VecDeque` - --> $DIR/iter_count.rs:39:29 + --> $DIR/iter_count.rs:46:5 | -LL | let bad_vec_deque = some_vec_deque.iter().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec_deque.len()` +LL | vec_deque.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` error: called `.iter().count()` on a `std::iter::Iterator` - --> $DIR/iter_count.rs:40:28 + --> $DIR/iter_count.rs:47:5 | -LL | let bad_hash_set = some_hash_set.iter().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_hash_set.len()` +LL | hash_set.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_set.len()` error: called `.iter_mut().count()` on a `Vec` - --> $DIR/iter_count.rs:45:23 + --> $DIR/iter_count.rs:49:5 | -LL | let bad_vec = some_vec.iter_mut().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec.len()` +LL | vec.iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` error: called `.iter_mut().count()` on a `slice` - --> $DIR/iter_count.rs:48:26 + --> $DIR/iter_count.rs:50:6 | -LL | let bad_slice = &some_vec[..].iter_mut().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec[..].len()` +LL | &vec[..].iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` error: called `.iter_mut().count()` on a `VecDeque` - --> $DIR/iter_count.rs:51:29 + --> $DIR/iter_count.rs:51:5 + | +LL | vec_deque.iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` + +error: called `.into_iter().count()` on a `slice` + --> $DIR/iter_count.rs:53:6 + | +LL | &vec[..].into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` + +error: called `.into_iter().count()` on a `Vec` + --> $DIR/iter_count.rs:54:5 + | +LL | vec.into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` + +error: called `.into_iter().count()` on a `VecDeque` + --> $DIR/iter_count.rs:55:5 | -LL | let bad_vec_deque = some_vec_deque.iter_mut().count(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec_deque.len()` +LL | vec_deque.into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` -error: aborting due to 8 previous errors +error: aborting due to 11 previous errors From 9958af4229d4bee06ee65b921ac11b1fe498e831 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 26 Feb 2021 02:18:52 +0900 Subject: [PATCH 4/9] move `lints()` to `iter_count.rs` --- clippy_lints/src/methods/iter_count.rs | 51 ++++++++++++++ clippy_lints/src/methods/mod.rs | 97 +++----------------------- clippy_utils/src/lib.rs | 40 +++++++++++ 3 files changed, 100 insertions(+), 88 deletions(-) create mode 100644 clippy_lints/src/methods/iter_count.rs diff --git a/clippy_lints/src/methods/iter_count.rs b/clippy_lints/src/methods/iter_count.rs new file mode 100644 index 000000000000..ca8723cec949 --- /dev/null +++ b/clippy_lints/src/methods/iter_count.rs @@ -0,0 +1,51 @@ +use crate::utils::{ + derefs_to_slice, is_type_diagnostic_item, match_trait_method, method_chain_args, paths, snippet_with_applicability, + span_lint_and_sugg, +}; +use if_chain::if_chain; +use rustc_errors::Applicability; +use rustc_hir::Expr; +use rustc_lint::LateContext; +use rustc_span::sym; + +use super::ITER_COUNT; + +pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) { + let mut_str = if is_mut { "_mut" } else { "" }; + let iter_method = if method_chain_args(expr, &[format!("iter{}", mut_str).as_str(), "count"]).is_some() { + "iter" + } else if method_chain_args(expr, &["into_iter", "count"]).is_some() { + "into_iter" + } else { + return; + }; + if_chain! { + let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() { + Some("slice") + } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) { + Some("Vec") + } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) { + Some("VecDeque") + } else if match_trait_method(cx, expr, &paths::ITERATOR) { + Some("std::iter::Iterator") + } else { + None + }; + if let Some(caller_type) = caller_type; + then { + let mut applicability = Applicability::MachineApplicable; + span_lint_and_sugg( + cx, + ITER_COUNT, + expr.span, + &format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type), + "try", + format!( + "{}.len()", + snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability), + ), + applicability, + ); + } + } +} diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index e5509ee2c4e0..d2a926d0944f 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -4,6 +4,7 @@ mod filter_map_identity; mod implicit_clone; mod inefficient_to_string; mod inspect_for_each; +mod iter_count; mod manual_saturating_arithmetic; mod option_map_unwrap_or; mod unnecessary_filter_map; @@ -32,12 +33,12 @@ use crate::consts::{constant, Constant}; use crate::utils::eager_or_lazy::is_lazyness_candidate; use crate::utils::usage::mutated_variables; use crate::utils::{ - contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, - in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, - match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths, - remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, - span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, sugg, walk_ptrs_ty_depth, - SpanlessEq, + contains_return, contains_ty, derefs_to_slice, get_parent_expr, get_trait_def_id, has_iter_method, higher, + implements_trait, in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, + match_def_path, match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, + path_to_local_id, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, + snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, + sugg, walk_ptrs_ty_depth, SpanlessEq, }; declare_clippy_lint! { @@ -1691,8 +1692,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods { lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0], method_spans[1]) }, ["extend", ..] => lint_extend(cx, expr, arg_lists[0]), - ["count", "into_iter" | "iter"] => lint_iter_count(cx, expr, &arg_lists[1], false), - ["count", "iter_mut"] => lint_iter_count(cx, expr, &arg_lists[1], true), + ["count", "into_iter" | "iter"] => iter_count::lints(cx, expr, &arg_lists[1], false), + ["count", "iter_mut"] => iter_count::lints(cx, expr, &arg_lists[1], true), ["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false), ["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true), ["nth", "bytes"] => bytes_nth::lints(cx, expr, &arg_lists[1]), @@ -2661,46 +2662,6 @@ fn lint_iter_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, iter_ } } -fn lint_iter_count<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) { - let mut_str = if is_mut { "_mut" } else { "" }; - let iter_method = if method_chain_args(expr, &[format!("iter{}", mut_str).as_str(), "count"]).is_some() { - "iter" - } else if method_chain_args(expr, &["into_iter", "count"]).is_some() { - "into_iter" - } else { - return; - }; - if_chain! { - let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() { - Some("slice") - } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) { - Some("Vec") - } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) { - Some("VecDeque") - } else if match_trait_method(cx, expr, &paths::ITERATOR) { - Some("std::iter::Iterator") - } else { - None - }; - if let Some(caller_type) = caller_type; - then { - let mut applicability = Applicability::MachineApplicable; - span_lint_and_sugg( - cx, - ITER_COUNT, - expr.span, - &format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type), - "try", - format!( - "{}.len()", - snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability), - ), - applicability, - ); - } - } -} - fn lint_iter_nth<'tcx>( cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, @@ -2841,46 +2802,6 @@ fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[ } } -fn derefs_to_slice<'tcx>( - cx: &LateContext<'tcx>, - expr: &'tcx hir::Expr<'tcx>, - ty: Ty<'tcx>, -) -> Option<&'tcx hir::Expr<'tcx>> { - fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool { - match ty.kind() { - ty::Slice(_) => true, - ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()), - ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type), - ty::Array(_, size) => size - .try_eval_usize(cx.tcx, cx.param_env) - .map_or(false, |size| size < 32), - ty::Ref(_, inner, _) => may_slice(cx, inner), - _ => false, - } - } - - if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind { - if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) { - Some(&args[0]) - } else { - None - } - } else { - match ty.kind() { - ty::Slice(_) => Some(expr), - ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr), - ty::Ref(_, inner, _) => { - if may_slice(cx, inner) { - Some(expr) - } else { - None - } - }, - _ => None, - } - } -} - /// lint use of `unwrap()` for `Option`s and `Result`s fn lint_unwrap(cx: &LateContext<'_>, expr: &hir::Expr<'_>, unwrap_args: &[hir::Expr<'_>]) { let obj_ty = cx.typeck_results().expr_ty(&unwrap_args[0]).peel_refs(); diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 2380ea4c7bfa..ec1b189bf77e 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -1802,6 +1802,46 @@ pub fn is_some_ctor(cx: &LateContext<'_>, res: Res) -> bool { false } +pub fn derefs_to_slice<'tcx>( + cx: &LateContext<'tcx>, + expr: &'tcx hir::Expr<'tcx>, + ty: Ty<'tcx>, +) -> Option<&'tcx hir::Expr<'tcx>> { + fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool { + match ty.kind() { + ty::Slice(_) => true, + ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()), + ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type), + ty::Array(_, size) => size + .try_eval_usize(cx.tcx, cx.param_env) + .map_or(false, |size| size < 32), + ty::Ref(_, inner, _) => may_slice(cx, inner), + _ => false, + } + } + + if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind { + if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) { + Some(&args[0]) + } else { + None + } + } else { + match ty.kind() { + ty::Slice(_) => Some(expr), + ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr), + ty::Ref(_, inner, _) => { + if may_slice(cx, inner) { + Some(expr) + } else { + None + } + }, + _ => None, + } + } +} + #[cfg(test)] mod test { use super::{reindent_multiline, without_block_comments}; From cc2b00055ccc18f4b345a1f6d50865ada9093e88 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 26 Feb 2021 04:11:01 +0900 Subject: [PATCH 5/9] return when the ty doesn't have `len()` --- clippy_lints/src/methods/iter_count.rs | 33 +++++--- tests/ui/iter_count.fixed | 26 +++++- tests/ui/iter_count.rs | 26 +++++- tests/ui/iter_count.stderr | 110 ++++++++++++++++++++++--- 4 files changed, 169 insertions(+), 26 deletions(-) diff --git a/clippy_lints/src/methods/iter_count.rs b/clippy_lints/src/methods/iter_count.rs index ca8723cec949..1bcdb57ad29e 100644 --- a/clippy_lints/src/methods/iter_count.rs +++ b/clippy_lints/src/methods/iter_count.rs @@ -1,7 +1,8 @@ use crate::utils::{ - derefs_to_slice, is_type_diagnostic_item, match_trait_method, method_chain_args, paths, snippet_with_applicability, + derefs_to_slice, is_type_diagnostic_item, match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_sugg, }; + use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::Expr; @@ -19,19 +20,29 @@ pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &' } else { return; }; + let ty = cx.typeck_results().expr_ty(&iter_args[0]); if_chain! { - let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() { - Some("slice") - } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) { - Some("Vec") - } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) { - Some("VecDeque") - } else if match_trait_method(cx, expr, &paths::ITERATOR) { - Some("std::iter::Iterator") + let caller_type = if derefs_to_slice(cx, &iter_args[0], ty).is_some() { + "slice" + } else if is_type_diagnostic_item(cx, ty, sym::vec_type) { + "Vec" + } else if is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) { + "VecDeque" + } else if is_type_diagnostic_item(cx, ty, sym!(hashset_type)) { + "HashSet" + } else if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) { + "HashMap" + } else if match_type(cx, ty, &paths::BTREEMAP) { + "BTreeMap" + } else if match_type(cx, ty, &paths::BTREESET) { + "BTreeSet" + } else if match_type(cx, ty, &paths::LINKED_LIST) { + "LinkedList" + } else if match_type(cx, ty, &paths::BINARY_HEAP) { + "BinaryHeap" } else { - None + return }; - if let Some(caller_type) = caller_type; then { let mut applicability = Applicability::MachineApplicable; span_lint_and_sugg( diff --git a/tests/ui/iter_count.fixed b/tests/ui/iter_count.fixed index c8f896408479..b11dadda6c24 100644 --- a/tests/ui/iter_count.fixed +++ b/tests/ui/iter_count.fixed @@ -13,7 +13,7 @@ extern crate option_helpers; use option_helpers::IteratorFalsePositives; -use std::collections::{HashSet, VecDeque}; +use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; /// Struct to generate false positives for things with `.iter()`. #[derive(Copy, Clone)] @@ -38,21 +38,45 @@ fn main() { let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); let mut vec_deque: VecDeque<_> = vec.iter().cloned().collect(); let mut hash_set = HashSet::new(); + let mut hash_map = HashMap::new(); + let mut b_tree_map = BTreeMap::new(); + let mut b_tree_set = BTreeSet::new(); + let mut linked_list = LinkedList::new(); + let mut binary_heap = BinaryHeap::new(); hash_set.insert(1); + hash_map.insert(1, 2); + b_tree_map.insert(1, 2); + b_tree_set.insert(1); + linked_list.push_back(1); + binary_heap.push(1); &vec[..].len(); vec.len(); boxed_slice.len(); vec_deque.len(); hash_set.len(); + hash_map.len(); + b_tree_map.len(); + b_tree_set.len(); + linked_list.len(); + binary_heap.len(); vec.len(); &vec[..].len(); vec_deque.len(); + hash_map.len(); + b_tree_map.len(); + linked_list.len(); &vec[..].len(); vec.len(); vec_deque.len(); + hash_set.len(); + hash_map.len(); + b_tree_map.len(); + b_tree_set.len(); + linked_list.len(); + binary_heap.len(); // Make sure we don't lint for non-relevant types. let false_positive = HasIter; diff --git a/tests/ui/iter_count.rs b/tests/ui/iter_count.rs index 8ea17ef34fa6..7d49c6a3dbbb 100644 --- a/tests/ui/iter_count.rs +++ b/tests/ui/iter_count.rs @@ -13,7 +13,7 @@ extern crate option_helpers; use option_helpers::IteratorFalsePositives; -use std::collections::{HashSet, VecDeque}; +use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; /// Struct to generate false positives for things with `.iter()`. #[derive(Copy, Clone)] @@ -38,21 +38,45 @@ fn main() { let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); let mut vec_deque: VecDeque<_> = vec.iter().cloned().collect(); let mut hash_set = HashSet::new(); + let mut hash_map = HashMap::new(); + let mut b_tree_map = BTreeMap::new(); + let mut b_tree_set = BTreeSet::new(); + let mut linked_list = LinkedList::new(); + let mut binary_heap = BinaryHeap::new(); hash_set.insert(1); + hash_map.insert(1, 2); + b_tree_map.insert(1, 2); + b_tree_set.insert(1); + linked_list.push_back(1); + binary_heap.push(1); &vec[..].iter().count(); vec.iter().count(); boxed_slice.iter().count(); vec_deque.iter().count(); hash_set.iter().count(); + hash_map.iter().count(); + b_tree_map.iter().count(); + b_tree_set.iter().count(); + linked_list.iter().count(); + binary_heap.iter().count(); vec.iter_mut().count(); &vec[..].iter_mut().count(); vec_deque.iter_mut().count(); + hash_map.iter_mut().count(); + b_tree_map.iter_mut().count(); + linked_list.iter_mut().count(); &vec[..].into_iter().count(); vec.into_iter().count(); vec_deque.into_iter().count(); + hash_set.into_iter().count(); + hash_map.into_iter().count(); + b_tree_map.into_iter().count(); + b_tree_set.into_iter().count(); + linked_list.into_iter().count(); + binary_heap.into_iter().count(); // Make sure we don't lint for non-relevant types. let false_positive = HasIter; diff --git a/tests/ui/iter_count.stderr b/tests/ui/iter_count.stderr index 0820c0014434..f3fb98e65b99 100644 --- a/tests/ui/iter_count.stderr +++ b/tests/ui/iter_count.stderr @@ -1,5 +1,5 @@ error: called `.iter().count()` on a `slice` - --> $DIR/iter_count.rs:43:6 + --> $DIR/iter_count.rs:53:6 | LL | &vec[..].iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` @@ -7,64 +7,148 @@ LL | &vec[..].iter().count(); = note: `-D clippy::iter-count` implied by `-D warnings` error: called `.iter().count()` on a `Vec` - --> $DIR/iter_count.rs:44:5 + --> $DIR/iter_count.rs:54:5 | LL | vec.iter().count(); | ^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` error: called `.iter().count()` on a `slice` - --> $DIR/iter_count.rs:45:5 + --> $DIR/iter_count.rs:55:5 | LL | boxed_slice.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `boxed_slice.len()` error: called `.iter().count()` on a `VecDeque` - --> $DIR/iter_count.rs:46:5 + --> $DIR/iter_count.rs:56:5 | LL | vec_deque.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` -error: called `.iter().count()` on a `std::iter::Iterator` - --> $DIR/iter_count.rs:47:5 +error: called `.iter().count()` on a `HashSet` + --> $DIR/iter_count.rs:57:5 | LL | hash_set.iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_set.len()` +error: called `.iter().count()` on a `HashMap` + --> $DIR/iter_count.rs:58:5 + | +LL | hash_map.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()` + +error: called `.iter().count()` on a `BTreeMap` + --> $DIR/iter_count.rs:59:5 + | +LL | b_tree_map.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()` + +error: called `.iter().count()` on a `BTreeSet` + --> $DIR/iter_count.rs:60:5 + | +LL | b_tree_set.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_set.len()` + +error: called `.iter().count()` on a `LinkedList` + --> $DIR/iter_count.rs:61:5 + | +LL | linked_list.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()` + +error: called `.iter().count()` on a `BinaryHeap` + --> $DIR/iter_count.rs:62:5 + | +LL | binary_heap.iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `binary_heap.len()` + error: called `.iter_mut().count()` on a `Vec` - --> $DIR/iter_count.rs:49:5 + --> $DIR/iter_count.rs:64:5 | LL | vec.iter_mut().count(); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` error: called `.iter_mut().count()` on a `slice` - --> $DIR/iter_count.rs:50:6 + --> $DIR/iter_count.rs:65:6 | LL | &vec[..].iter_mut().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` error: called `.iter_mut().count()` on a `VecDeque` - --> $DIR/iter_count.rs:51:5 + --> $DIR/iter_count.rs:66:5 | LL | vec_deque.iter_mut().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` +error: called `.iter_mut().count()` on a `HashMap` + --> $DIR/iter_count.rs:67:5 + | +LL | hash_map.iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()` + +error: called `.iter_mut().count()` on a `BTreeMap` + --> $DIR/iter_count.rs:68:5 + | +LL | b_tree_map.iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()` + +error: called `.iter_mut().count()` on a `LinkedList` + --> $DIR/iter_count.rs:69:5 + | +LL | linked_list.iter_mut().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()` + error: called `.into_iter().count()` on a `slice` - --> $DIR/iter_count.rs:53:6 + --> $DIR/iter_count.rs:71:6 | LL | &vec[..].into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()` error: called `.into_iter().count()` on a `Vec` - --> $DIR/iter_count.rs:54:5 + --> $DIR/iter_count.rs:72:5 | LL | vec.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()` error: called `.into_iter().count()` on a `VecDeque` - --> $DIR/iter_count.rs:55:5 + --> $DIR/iter_count.rs:73:5 | LL | vec_deque.into_iter().count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()` -error: aborting due to 11 previous errors +error: called `.into_iter().count()` on a `HashSet` + --> $DIR/iter_count.rs:74:5 + | +LL | hash_set.into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_set.len()` + +error: called `.into_iter().count()` on a `HashMap` + --> $DIR/iter_count.rs:75:5 + | +LL | hash_map.into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()` + +error: called `.into_iter().count()` on a `BTreeMap` + --> $DIR/iter_count.rs:76:5 + | +LL | b_tree_map.into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()` + +error: called `.into_iter().count()` on a `BTreeSet` + --> $DIR/iter_count.rs:77:5 + | +LL | b_tree_set.into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_set.len()` + +error: called `.into_iter().count()` on a `LinkedList` + --> $DIR/iter_count.rs:78:5 + | +LL | linked_list.into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()` + +error: called `.into_iter().count()` on a `BinaryHeap` + --> $DIR/iter_count.rs:79:5 + | +LL | binary_heap.into_iter().count(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `binary_heap.len()` + +error: aborting due to 25 previous errors From 8bae2797067650071b69273ecfbf55240190db07 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 26 Feb 2021 15:49:03 +0900 Subject: [PATCH 6/9] remove if_chain --- clippy_lints/src/methods/iter_count.rs | 72 ++++++++++++-------------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/clippy_lints/src/methods/iter_count.rs b/clippy_lints/src/methods/iter_count.rs index 1bcdb57ad29e..06ce06126e62 100644 --- a/clippy_lints/src/methods/iter_count.rs +++ b/clippy_lints/src/methods/iter_count.rs @@ -21,42 +21,38 @@ pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &' return; }; let ty = cx.typeck_results().expr_ty(&iter_args[0]); - if_chain! { - let caller_type = if derefs_to_slice(cx, &iter_args[0], ty).is_some() { - "slice" - } else if is_type_diagnostic_item(cx, ty, sym::vec_type) { - "Vec" - } else if is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) { - "VecDeque" - } else if is_type_diagnostic_item(cx, ty, sym!(hashset_type)) { - "HashSet" - } else if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) { - "HashMap" - } else if match_type(cx, ty, &paths::BTREEMAP) { - "BTreeMap" - } else if match_type(cx, ty, &paths::BTREESET) { - "BTreeSet" - } else if match_type(cx, ty, &paths::LINKED_LIST) { - "LinkedList" - } else if match_type(cx, ty, &paths::BINARY_HEAP) { - "BinaryHeap" - } else { - return - }; - then { - let mut applicability = Applicability::MachineApplicable; - span_lint_and_sugg( - cx, - ITER_COUNT, - expr.span, - &format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type), - "try", - format!( - "{}.len()", - snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability), - ), - applicability, - ); - } - } + let caller_type = if derefs_to_slice(cx, &iter_args[0], ty).is_some() { + "slice" + } else if is_type_diagnostic_item(cx, ty, sym::vec_type) { + "Vec" + } else if is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) { + "VecDeque" + } else if is_type_diagnostic_item(cx, ty, sym!(hashset_type)) { + "HashSet" + } else if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) { + "HashMap" + } else if match_type(cx, ty, &paths::BTREEMAP) { + "BTreeMap" + } else if match_type(cx, ty, &paths::BTREESET) { + "BTreeSet" + } else if match_type(cx, ty, &paths::LINKED_LIST) { + "LinkedList" + } else if match_type(cx, ty, &paths::BINARY_HEAP) { + "BinaryHeap" + } else { + return; + }; + let mut applicability = Applicability::MachineApplicable; + span_lint_and_sugg( + cx, + ITER_COUNT, + expr.span, + &format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type), + "try", + format!( + "{}.len()", + snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability), + ), + applicability, + ); } From 77907e6dab42dd0acc23c0cf1d540d2aa3e32bb8 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 26 Feb 2021 16:21:43 +0900 Subject: [PATCH 7/9] receive iter method name as an argument --- clippy_lints/src/methods/iter_count.rs | 16 +++------------- clippy_lints/src/methods/mod.rs | 5 +++-- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/methods/iter_count.rs b/clippy_lints/src/methods/iter_count.rs index 06ce06126e62..ff3aa346e2d6 100644 --- a/clippy_lints/src/methods/iter_count.rs +++ b/clippy_lints/src/methods/iter_count.rs @@ -1,9 +1,7 @@ use crate::utils::{ - derefs_to_slice, is_type_diagnostic_item, match_type, method_chain_args, paths, snippet_with_applicability, - span_lint_and_sugg, + derefs_to_slice, is_type_diagnostic_item, match_type, paths, snippet_with_applicability, span_lint_and_sugg, }; -use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; @@ -11,15 +9,7 @@ use rustc_span::sym; use super::ITER_COUNT; -pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) { - let mut_str = if is_mut { "_mut" } else { "" }; - let iter_method = if method_chain_args(expr, &[format!("iter{}", mut_str).as_str(), "count"]).is_some() { - "iter" - } else if method_chain_args(expr, &["into_iter", "count"]).is_some() { - "into_iter" - } else { - return; - }; +pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], iter_method: &str) { let ty = cx.typeck_results().expr_ty(&iter_args[0]); let caller_type = if derefs_to_slice(cx, &iter_args[0], ty).is_some() { "slice" @@ -47,7 +37,7 @@ pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &' cx, ITER_COUNT, expr.span, - &format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type), + &format!("called `.{}().count()` on a `{}`", iter_method, caller_type), "try", format!( "{}.len()", diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d2a926d0944f..f394e6673d48 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1692,8 +1692,9 @@ impl<'tcx> LateLintPass<'tcx> for Methods { lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0], method_spans[1]) }, ["extend", ..] => lint_extend(cx, expr, arg_lists[0]), - ["count", "into_iter" | "iter"] => iter_count::lints(cx, expr, &arg_lists[1], false), - ["count", "iter_mut"] => iter_count::lints(cx, expr, &arg_lists[1], true), + ["count", "into_iter"] => iter_count::lints(cx, expr, &arg_lists[1], "into_iter"), + ["count", "iter"] => iter_count::lints(cx, expr, &arg_lists[1], "iter"), + ["count", "iter_mut"] => iter_count::lints(cx, expr, &arg_lists[1], "iter_mut"), ["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false), ["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true), ["nth", "bytes"] => bytes_nth::lints(cx, expr, &arg_lists[1]), From c297174adf9468234d0eebad608514879a52b82b Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 26 Feb 2021 17:07:00 +0900 Subject: [PATCH 8/9] export `derefs_to_slice` from methods module --- clippy_lints/src/methods/iter_count.rs | 5 +-- clippy_lints/src/methods/mod.rs | 52 +++++++++++++++++++++++--- clippy_utils/src/lib.rs | 40 -------------------- 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/clippy_lints/src/methods/iter_count.rs b/clippy_lints/src/methods/iter_count.rs index ff3aa346e2d6..1b99bacc3f1c 100644 --- a/clippy_lints/src/methods/iter_count.rs +++ b/clippy_lints/src/methods/iter_count.rs @@ -1,6 +1,5 @@ -use crate::utils::{ - derefs_to_slice, is_type_diagnostic_item, match_type, paths, snippet_with_applicability, span_lint_and_sugg, -}; +use crate::methods::derefs_to_slice; +use crate::utils::{is_type_diagnostic_item, match_type, paths, snippet_with_applicability, span_lint_and_sugg}; use rustc_errors::Applicability; use rustc_hir::Expr; diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index f394e6673d48..30830fb0af65 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -33,12 +33,12 @@ use crate::consts::{constant, Constant}; use crate::utils::eager_or_lazy::is_lazyness_candidate; use crate::utils::usage::mutated_variables; use crate::utils::{ - contains_return, contains_ty, derefs_to_slice, get_parent_expr, get_trait_def_id, has_iter_method, higher, - implements_trait, in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, - match_def_path, match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, - path_to_local_id, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, - snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, - sugg, walk_ptrs_ty_depth, SpanlessEq, + contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, + in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, + match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths, + remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, + span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, sugg, walk_ptrs_ty_depth, + SpanlessEq, }; declare_clippy_lint! { @@ -2803,6 +2803,46 @@ fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[ } } +pub(crate) fn derefs_to_slice<'tcx>( + cx: &LateContext<'tcx>, + expr: &'tcx hir::Expr<'tcx>, + ty: Ty<'tcx>, +) -> Option<&'tcx hir::Expr<'tcx>> { + fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool { + match ty.kind() { + ty::Slice(_) => true, + ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()), + ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type), + ty::Array(_, size) => size + .try_eval_usize(cx.tcx, cx.param_env) + .map_or(false, |size| size < 32), + ty::Ref(_, inner, _) => may_slice(cx, inner), + _ => false, + } + } + + if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind { + if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) { + Some(&args[0]) + } else { + None + } + } else { + match ty.kind() { + ty::Slice(_) => Some(expr), + ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr), + ty::Ref(_, inner, _) => { + if may_slice(cx, inner) { + Some(expr) + } else { + None + } + }, + _ => None, + } + } +} + /// lint use of `unwrap()` for `Option`s and `Result`s fn lint_unwrap(cx: &LateContext<'_>, expr: &hir::Expr<'_>, unwrap_args: &[hir::Expr<'_>]) { let obj_ty = cx.typeck_results().expr_ty(&unwrap_args[0]).peel_refs(); diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index ec1b189bf77e..2380ea4c7bfa 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -1802,46 +1802,6 @@ pub fn is_some_ctor(cx: &LateContext<'_>, res: Res) -> bool { false } -pub fn derefs_to_slice<'tcx>( - cx: &LateContext<'tcx>, - expr: &'tcx hir::Expr<'tcx>, - ty: Ty<'tcx>, -) -> Option<&'tcx hir::Expr<'tcx>> { - fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool { - match ty.kind() { - ty::Slice(_) => true, - ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()), - ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type), - ty::Array(_, size) => size - .try_eval_usize(cx.tcx, cx.param_env) - .map_or(false, |size| size < 32), - ty::Ref(_, inner, _) => may_slice(cx, inner), - _ => false, - } - } - - if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind { - if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) { - Some(&args[0]) - } else { - None - } - } else { - match ty.kind() { - ty::Slice(_) => Some(expr), - ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr), - ty::Ref(_, inner, _) => { - if may_slice(cx, inner) { - Some(expr) - } else { - None - } - }, - _ => None, - } - } -} - #[cfg(test)] mod test { use super::{reindent_multiline, without_block_comments}; From 6041365f4bae6482467210ea1b60e18101ba6fd0 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 26 Feb 2021 17:38:21 +0900 Subject: [PATCH 9/9] remove pub(crate) --- clippy_lints/src/methods/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 30830fb0af65..4a63de3cf48d 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2803,7 +2803,7 @@ fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[ } } -pub(crate) fn derefs_to_slice<'tcx>( +fn derefs_to_slice<'tcx>( cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>, ty: Ty<'tcx>,