mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
Fix suggestions that need parens
This commit is contained in:
parent
5db215bca8
commit
7825bf36d8
3 changed files with 27 additions and 4 deletions
|
@ -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, &[]);
|
||||
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 sugg = if higher::range(&args[0]).is_some() {
|
||||
format!("{}.collect::<{}>()", iter_expr, ty)
|
||||
} else {
|
||||
format!("{}.collect()", iter_expr)
|
||||
};
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#![warn(clippy::from_iter_instead_of_collect)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::iter::FromIterator;
|
||||
|
||||
fn main() {
|
||||
|
@ -10,4 +10,10 @@ fn main() {
|
|||
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));
|
||||
|
||||
let mut b = VecDeque::from_iter(0..3);
|
||||
b.push_back(4);
|
||||
}
|
||||
|
|
|
@ -12,5 +12,17 @@ error: usage of `FromIterator::from_iter`
|
|||
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()`
|
||||
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue