Skip to content

Fix suggestions that need parens in from_iter_instead_of_collect lint #6657

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
merged 3 commits into from
Feb 10, 2021
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
38 changes: 36 additions & 2 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4094,20 +4094,54 @@ fn lint_from_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<
if implements_trait(cx, ty, from_iter_id, &[]) && implements_trait(cx, arg_ty, iter_id, &[]);
then {
// `expr` implements `FromIterator` trait
let iter_expr = snippet(cx, args[0].span, "..");
let iter_expr = sugg::Sugg::hir(cx, &args[0], "..").maybe_par();
let turbofish = extract_turbofish(cx, expr, ty);
let sugg = format!("{}.collect::<{}>()", iter_expr, turbofish);
span_lint_and_sugg(
cx,
FROM_ITER_INSTEAD_OF_COLLECT,
expr.span,
"usage of `FromIterator::from_iter`",
"use `.collect()` instead of `::from_iter()`",
format!("{}.collect()", iter_expr),
sugg,
Applicability::MaybeIncorrect,
);
}
}
}

fn extract_turbofish(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ty: Ty<'tcx>) -> String {
if_chain! {
let call_site = expr.span.source_callsite();
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(call_site);
let snippet_split = snippet.split("::").collect::<Vec<_>>();
if let Some((_, elements)) = snippet_split.split_last();

then {
// is there a type specifier? (i.e.: like `<u32>` in `collections::BTreeSet::<u32>::`)
if let Some(type_specifier) = snippet_split.iter().find(|e| e.starts_with('<') && e.ends_with('>')) {
// remove the type specifier from the path elements
let without_ts = elements.iter().filter_map(|e| {
if e == type_specifier { None } else { Some((*e).to_string()) }
}).collect::<Vec<_>>();
// join and add the type specifier at the end (i.e.: `collections::BTreeSet<u32>`)
format!("{}{}", without_ts.join("::"), type_specifier)
} else {
// type is not explicitly specified so wildcards are needed
// i.e.: 2 wildcards in `std::collections::BTreeMap<&i32, &char>`
let ty_str = ty.to_string();
let start = ty_str.find('<').unwrap_or(0);
let end = ty_str.find('>').unwrap_or_else(|| ty_str.len());
let nb_wildcard = ty_str[start..end].split(',').count();
let wildcards = format!("_{}", ", _".repeat(nb_wildcard - 1));
format!("{}<{}>", elements.join("::"), wildcards)
}
} else {
ty.to_string()
}
}
}

fn fn_header_equals(expected: hir::FnHeader, actual: hir::FnHeader) -> bool {
expected.constness == actual.constness
&& expected.unsafety == actual.unsafety
Expand Down
48 changes: 48 additions & 0 deletions tests/ui/from_iter_instead_of_collect.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// run-rustfix

#![warn(clippy::from_iter_instead_of_collect)]
#![allow(unused_imports)]

use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::iter::FromIterator;

fn main() {
let iter_expr = std::iter::repeat(5).take(5);
let _ = iter_expr.collect::<Vec<_>>();

let _ = vec![5, 5, 5, 5].iter().enumerate().collect::<HashMap<usize, &i8>>();

Vec::from_iter(vec![42u32]);

let a = vec![0, 1, 2];
assert_eq!(a, (0..3).collect::<Vec<_>>());
assert_eq!(a, (0..3).collect::<Vec<i32>>());

let mut b = (0..3).collect::<VecDeque<_>>();
b.push_back(4);

let mut b = (0..3).collect::<VecDeque<i32>>();
b.push_back(4);

{
use std::collections;
let mut b = (0..3).collect::<collections::VecDeque<i32>>();
b.push_back(4);
}

let values = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')];
let bm = values.iter().cloned().collect::<BTreeMap<_, _>>();
let mut bar = bm.range(0..2).collect::<BTreeMap<_, _>>();
bar.insert(&4, &'e');

let mut bts = (0..3).collect::<BTreeSet<_>>();
bts.insert(2);
{
use std::collections;
let _ = (0..3).collect::<collections::BTreeSet<_>>();
let _ = (0..3).collect::<collections::BTreeSet<u32>>();
}

for _i in [1, 2, 3].iter().collect::<Vec<_>>() {}
for _i in [1, 2, 3].iter().collect::<Vec<&i32>>() {}
}
41 changes: 38 additions & 3 deletions tests/ui/from_iter_instead_of_collect.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
// run-rustfix

#![warn(clippy::from_iter_instead_of_collect)]
#![allow(unused_imports)]

use std::collections::HashMap;
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::iter::FromIterator;

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

HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());
let _ = HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());

Vec::from_iter(vec![42u32]);

let a = vec![0, 1, 2];
assert_eq!(a, Vec::from_iter(0..3));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this case? The suggested code seems not to work.

    let mut b = VecDeque::from_iter(0..3);
    // let mut b = `(0..3).collect::<Vec<_>>()` // the suggested code
    b.push_back(4);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed, turbofish should be customized to match the type implementing from_iter (here VecDeque)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better now.
I wanted to shorten the suggestion from (0..3).collect::<std::collections::VecDeque<i32>>() to (0..3).collect::<VecDeque<i32>>() by removing unneeded std::collections but I am not sure this is even possible so I need to dig more into this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find any util method. IMHO it might be good to just convert ty to String and use .split("::").last().unwrap() if .contains("::").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I thought about this also, but it sounds a bit too hacky 😄
Ultimately I will consider it if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, and yes it's a little hacky. FYI similar codes already exist here

if !self.collected.contains(&call_site) {
let name = if name.contains("::") {
name.split("::").last().unwrap().to_string()
} else {
name.to_string()
};
self.mac_refs.push(MacroRefData::new(name, callee.def_site, cx));
self.collected.insert(call_site);
}
.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, using items in scope to shorten the paths seems like a worthwile improvement. However, I don't think that should block this PR.

So I'd r+ this unless you want to continue improving it, @ThibsG.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to give a try to improve this. I should be able to work on this really soon, and I keep you posted 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try cx.tcx.trimmed_def_paths(LOCAL_CRATE).get(&def_id)

assert_eq!(a, Vec::<i32>::from_iter(0..3));

let mut b = VecDeque::from_iter(0..3);
b.push_back(4);

let mut b = VecDeque::<i32>::from_iter(0..3);
b.push_back(4);

{
use std::collections;
let mut b = collections::VecDeque::<i32>::from_iter(0..3);
b.push_back(4);
}

let values = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')];
let bm = BTreeMap::from_iter(values.iter().cloned());
let mut bar = BTreeMap::from_iter(bm.range(0..2));
bar.insert(&4, &'e');

let mut bts = BTreeSet::from_iter(0..3);
bts.insert(2);
{
use std::collections;
let _ = collections::BTreeSet::from_iter(0..3);
let _ = collections::BTreeSet::<u32>::from_iter(0..3);
}

for _i in Vec::from_iter([1, 2, 3].iter()) {}
for _i in Vec::<&i32>::from_iter([1, 2, 3].iter()) {}
}
86 changes: 79 additions & 7 deletions tests/ui/from_iter_instead_of_collect.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,88 @@
error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:8:5
--> $DIR/from_iter_instead_of_collect.rs:11:13
|
LL | Vec::from_iter(iter_expr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `iter_expr.collect()`
LL | let _ = Vec::from_iter(iter_expr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `iter_expr.collect::<Vec<_>>()`
|
= note: `-D clippy::from-iter-instead-of-collect` implied by `-D warnings`

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

error: aborting due to 2 previous errors
error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:18:19
|
LL | assert_eq!(a, Vec::from_iter(0..3));
| ^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<Vec<_>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:19:19
|
LL | assert_eq!(a, Vec::<i32>::from_iter(0..3));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<Vec<i32>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:21:17
|
LL | let mut b = VecDeque::from_iter(0..3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<VecDeque<_>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:24:17
|
LL | let mut b = VecDeque::<i32>::from_iter(0..3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<VecDeque<i32>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:29:21
|
LL | let mut b = collections::VecDeque::<i32>::from_iter(0..3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<collections::VecDeque<i32>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:34:14
|
LL | let bm = BTreeMap::from_iter(values.iter().cloned());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `values.iter().cloned().collect::<BTreeMap<_, _>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:35:19
|
LL | let mut bar = BTreeMap::from_iter(bm.range(0..2));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `bm.range(0..2).collect::<BTreeMap<_, _>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:38:19
|
LL | let mut bts = BTreeSet::from_iter(0..3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<BTreeSet<_>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:42:17
|
LL | let _ = collections::BTreeSet::from_iter(0..3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<collections::BTreeSet<_>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:43:17
|
LL | let _ = collections::BTreeSet::<u32>::from_iter(0..3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<collections::BTreeSet<u32>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:46:15
|
LL | for _i in Vec::from_iter([1, 2, 3].iter()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `[1, 2, 3].iter().collect::<Vec<_>>()`

error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:47:15
|
LL | for _i in Vec::<&i32>::from_iter([1, 2, 3].iter()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `[1, 2, 3].iter().collect::<Vec<&i32>>()`

error: aborting due to 14 previous errors