Skip to content

Commit

Permalink
Improvements from PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
pitiK3U authored and flip1995 committed Nov 3, 2020
1 parent a856706 commit 8906040
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
14 changes: 8 additions & 6 deletions clippy_lints/src/methods/mod.rs
Expand Up @@ -1370,10 +1370,11 @@ declare_clippy_lint! {
}

declare_clippy_lint! {
/// **What it does:** Checks for `from_iter()` function calls that implements `FromIterator`
/// **What it does:** Checks for `from_iter()` function calls on types that implement the `FromIterator`
/// trait.
///
/// **Why is this bad?** Makes code less readable especially in method chaining.
/// **Why is this bad?** It is recommended style to use collect. See
/// [FromIterator documentation](https://doc.rust-lang.org/std/iter/trait.FromIterator.html)
///
/// **Known problems:** None.
///
Expand Down Expand Up @@ -3873,18 +3874,19 @@ fn lint_filetype_is_file(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir

fn lint_from_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let ty = cx.typeck_results().expr_ty(expr);
let id = get_trait_def_id(cx, &paths::FROM_ITERATOR_TRAIT).unwrap();
let id = get_trait_def_id(cx, &paths::FROM_ITERATOR).unwrap();

if implements_trait(cx, ty, id, &[]) {
// `expr` implements `FromIterator` trait
let iter_expr = snippet(cx, args[0].span, "..");
span_lint_and_help(
span_lint_and_sugg(
cx,
FROM_ITER_INSTEAD_OF_COLLECT,
expr.span,
"use `.collect()` instead of `::from_iter()`",
None,
&format!("consider using `{}.collect()`", iter_expr),
"consider using",
format!("`{}.collect()`", iter_expr),
Applicability::MaybeIncorrect
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/paths.rs
Expand Up @@ -44,7 +44,7 @@ pub const FN: [&str; 3] = ["core", "ops", "Fn"];
pub const FN_MUT: [&str; 3] = ["core", "ops", "FnMut"];
pub const FN_ONCE: [&str; 3] = ["core", "ops", "FnOnce"];
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
pub const FROM_ITERATOR_TRAIT: [&str; 3] = ["std", "iter", "FromIterator"];
pub const FROM_ITERATOR: [&str; 3] = ["std", "iter", "FromIterator"];
pub const FROM_TRAIT: [&str; 3] = ["core", "convert", "From"];
pub const FUTURE_FROM_GENERATOR: [&str; 3] = ["core", "future", "from_generator"];
pub const HASH: [&str; 3] = ["core", "hash", "Hash"];
Expand Down
10 changes: 3 additions & 7 deletions tests/ui/from_iter_instead_of_collect.rs
Expand Up @@ -4,12 +4,8 @@ use std::collections::HashMap;
use std::iter::FromIterator;

fn main() {
{
let iter_expr = std::iter::repeat(5).take(5);
let iter_expr = std::iter::repeat(5).take(5);

Vec::from_iter(iter_expr);
HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());
//let v: Vec<i32> = iter_expr.collect();
let a: Vec<i32> = Vec::new();
}
Vec::from_iter(iter_expr);
HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());
}
15 changes: 6 additions & 9 deletions tests/ui/from_iter_instead_of_collect.stderr
@@ -1,19 +1,16 @@
error: use `.collect()` instead of `::from_iter()`
--> $DIR/from_iter_instead_of_collect.rs:10:9
--> $DIR/from_iter_instead_of_collect.rs:9:5
|
LL | Vec::from_iter(iter_expr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
LL | Vec::from_iter(iter_expr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: ``iter_expr.collect()``
|
= note: `-D clippy::from-iter-instead-of-collect` implied by `-D warnings`
= help: consider using `iter_expr.collect()`

error: use `.collect()` instead of `::from_iter()`
--> $DIR/from_iter_instead_of_collect.rs:11:9
--> $DIR/from_iter_instead_of_collect.rs:10:5
|
LL | HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using `vec![5, 5, 5, 5].iter().enumerate().collect()`
LL | HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: ``vec![5, 5, 5, 5].iter().enumerate().collect()``

error: aborting due to 2 previous errors

Empty file.

0 comments on commit 8906040

Please sign in to comment.