Fix suggestions that need parens

This commit is contained in:
ThibsG 2021-01-31 21:43:35 +01:00
parent 5db215bca8
commit 7825bf36d8
3 changed files with 27 additions and 4 deletions

View file

@ -4094,14 +4094,19 @@ 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, &[]); if implements_trait(cx, ty, from_iter_id, &[]) && implements_trait(cx, arg_ty, iter_id, &[]);
then { then {
// `expr` implements `FromIterator` trait // `expr` implements `FromIterator` trait
let iter_expr = snippet(cx, args[0].span, ".."); let iter_expr = sugg::Sugg::hir(cx, &args[0], "..").maybe_par();
let sugg = if higher::range(&args[0]).is_some() {
format!("{}.collect::<{}>()", iter_expr, ty)
} else {
format!("{}.collect()", iter_expr)
};
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
FROM_ITER_INSTEAD_OF_COLLECT, FROM_ITER_INSTEAD_OF_COLLECT,
expr.span, expr.span,
"usage of `FromIterator::from_iter`", "usage of `FromIterator::from_iter`",
"use `.collect()` instead of `::from_iter()`", "use `.collect()` instead of `::from_iter()`",
format!("{}.collect()", iter_expr), sugg,
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} }

View file

@ -1,6 +1,6 @@
#![warn(clippy::from_iter_instead_of_collect)] #![warn(clippy::from_iter_instead_of_collect)]
use std::collections::HashMap; use std::collections::{HashMap, VecDeque};
use std::iter::FromIterator; use std::iter::FromIterator;
fn main() { fn main() {
@ -10,4 +10,10 @@ fn main() {
HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate()); HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());
Vec::from_iter(vec![42u32]); Vec::from_iter(vec![42u32]);
let a = vec![0, 1, 2];
assert_eq!(a, Vec::from_iter(0..3));
let mut b = VecDeque::from_iter(0..3);
b.push_back(4);
} }

View file

@ -12,5 +12,17 @@ error: usage of `FromIterator::from_iter`
LL | HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate()); 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()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `vec![5, 5, 5, 5].iter().enumerate().collect()`
error: aborting due to 2 previous errors error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:15:19
|
LL | assert_eq!(a, Vec::from_iter(0..3));
| ^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<std::vec::Vec<i32>>()`
error: usage of `FromIterator::from_iter`
--> $DIR/from_iter_instead_of_collect.rs:17:17
|
LL | let mut b = VecDeque::from_iter(0..3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(0..3).collect::<std::collections::VecDeque<i32>>()`
error: aborting due to 4 previous errors