Skip to content

Commit 8906040

Browse files
pitiK3Uflip1995
authored andcommitted
Improvements from PR feedback
1 parent a856706 commit 8906040

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,10 +1370,11 @@ declare_clippy_lint! {
13701370
}
13711371

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

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

38783879
if implements_trait(cx, ty, id, &[]) {
38793880
// `expr` implements `FromIterator` trait
38803881
let iter_expr = snippet(cx, args[0].span, "..");
3881-
span_lint_and_help(
3882+
span_lint_and_sugg(
38823883
cx,
38833884
FROM_ITER_INSTEAD_OF_COLLECT,
38843885
expr.span,
38853886
"use `.collect()` instead of `::from_iter()`",
3886-
None,
3887-
&format!("consider using `{}.collect()`", iter_expr),
3887+
"consider using",
3888+
format!("`{}.collect()`", iter_expr),
3889+
Applicability::MaybeIncorrect
38883890
);
38893891
}
38903892
}

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub const FN: [&str; 3] = ["core", "ops", "Fn"];
4444
pub const FN_MUT: [&str; 3] = ["core", "ops", "FnMut"];
4545
pub const FN_ONCE: [&str; 3] = ["core", "ops", "FnOnce"];
4646
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
47-
pub const FROM_ITERATOR_TRAIT: [&str; 3] = ["std", "iter", "FromIterator"];
47+
pub const FROM_ITERATOR: [&str; 3] = ["std", "iter", "FromIterator"];
4848
pub const FROM_TRAIT: [&str; 3] = ["core", "convert", "From"];
4949
pub const FUTURE_FROM_GENERATOR: [&str; 3] = ["core", "future", "from_generator"];
5050
pub const HASH: [&str; 3] = ["core", "hash", "Hash"];

tests/ui/from_iter_instead_of_collect.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ use std::collections::HashMap;
44
use std::iter::FromIterator;
55

66
fn main() {
7-
{
8-
let iter_expr = std::iter::repeat(5).take(5);
7+
let iter_expr = std::iter::repeat(5).take(5);
98

10-
Vec::from_iter(iter_expr);
11-
HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());
12-
//let v: Vec<i32> = iter_expr.collect();
13-
let a: Vec<i32> = Vec::new();
14-
}
9+
Vec::from_iter(iter_expr);
10+
HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());
1511
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
error: use `.collect()` instead of `::from_iter()`
2-
--> $DIR/from_iter_instead_of_collect.rs:10:9
2+
--> $DIR/from_iter_instead_of_collect.rs:9:5
33
|
4-
LL | Vec::from_iter(iter_expr);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | Vec::from_iter(iter_expr);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: ``iter_expr.collect()``
66
|
77
= note: `-D clippy::from-iter-instead-of-collect` implied by `-D warnings`
8-
= help: consider using `iter_expr.collect()`
98

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

1815
error: aborting due to 2 previous errors
1916

tests/ui/from_iter_instead_of_collect.stdout

Whitespace-only changes.

0 commit comments

Comments
 (0)