Skip to content

Enhance needless_collect: lint in method/function arguments that take an IntoIterator #10777

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 1 commit into from
May 19, 2023
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
68 changes: 66 additions & 2 deletions clippy_lints/src/methods/needless_collect.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::NEEDLESS_COLLECT;
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::higher;
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{is_type_diagnostic_item, make_normalized_projection, make_projection};
use clippy_utils::{
can_move_expr_to_closure, get_enclosing_block, get_parent_node, is_trait_method, path_to_local, path_to_local_id,
CaptureKind,
};
use clippy_utils::{fn_def_id, higher};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, MultiSpan};
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
Expand All @@ -16,7 +16,7 @@ use rustc_hir::{
};
use rustc_lint::LateContext;
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{self, AssocKind, EarlyBinder, GenericArg, GenericArgKind, Ty};
use rustc_middle::ty::{self, AssocKind, Clause, EarlyBinder, GenericArg, GenericArgKind, PredicateKind, Ty};
use rustc_span::symbol::Ident;
use rustc_span::{sym, Span, Symbol};

Expand All @@ -32,6 +32,8 @@ pub(super) fn check<'tcx>(
if let Some(parent) = get_parent_node(cx.tcx, collect_expr.hir_id) {
match parent {
Node::Expr(parent) => {
check_collect_into_intoiterator(cx, parent, collect_expr, call_span, iter_expr);

if let ExprKind::MethodCall(name, _, args @ ([] | [_]), _) = parent.kind {
let mut app = Applicability::MachineApplicable;
let name = name.ident.as_str();
Expand Down Expand Up @@ -134,6 +136,68 @@ pub(super) fn check<'tcx>(
}
}

/// checks for for collecting into a (generic) method or function argument
/// taking an `IntoIterator`
fn check_collect_into_intoiterator<'tcx>(
cx: &LateContext<'tcx>,
parent: &'tcx Expr<'tcx>,
collect_expr: &'tcx Expr<'tcx>,
call_span: Span,
iter_expr: &'tcx Expr<'tcx>,
) {
if let Some(id) = fn_def_id(cx, parent) {
let args = match parent.kind {
ExprKind::Call(_, args) | ExprKind::MethodCall(_, _, args, _) => args,
_ => &[],
};
// find the argument index of the `collect_expr` in the
// function / method call
if let Some(arg_idx) = args.iter().position(|e| e.hir_id == collect_expr.hir_id).map(|i| {
if matches!(parent.kind, ExprKind::MethodCall(_, _, _, _)) {
i + 1
} else {
i
}
}) {
// extract the input types of the function/method call
// that contains `collect_expr`
let inputs = cx
.tcx
.liberate_late_bound_regions(id, cx.tcx.fn_sig(id).subst_identity())
.inputs();

// map IntoIterator generic bounds to their signature
// types and check whether the argument type is an
// `IntoIterator`
if cx
.tcx
.param_env(id)
.caller_bounds()
.into_iter()
.filter_map(|p| {
if let PredicateKind::Clause(Clause::Trait(t)) = p.kind().skip_binder()
&& cx.tcx.is_diagnostic_item(sym::IntoIterator,t.trait_ref.def_id) {
Some(t.self_ty())
} else {
None
}
})
.any(|ty| ty == inputs[arg_idx])
{
span_lint_and_sugg(
cx,
NEEDLESS_COLLECT,
call_span.with_lo(iter_expr.span.hi()),
NEEDLESS_COLLECT_MSG,
"remove this call",
String::new(),
Applicability::MachineApplicable,
);
}
}
}
}

/// Checks if the given method call matches the expected signature of `([&[mut]] self) -> bool`
fn is_is_empty_sig(cx: &LateContext<'_>, call_id: HirId) -> bool {
cx.typeck_results().type_dependent_def_id(call_id).map_or(false, |id| {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/needless_collect.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,16 @@ fn main() {

let _ = sample.iter().next().is_none();
let _ = sample.iter().any(|x| x == &0);

#[allow(clippy::double_parens)]
{
Vec::<u8>::new().extend((0..10));
foo((0..10));
bar((0..10).collect::<Vec<_>>(), (0..10));
baz((0..10), (), ('a'..='z'))
}
}

fn foo(_: impl IntoIterator<Item = usize>) {}
fn bar<I: IntoIterator<Item = usize>>(_: Vec<usize>, _: I) {}
fn baz<I: IntoIterator<Item = usize>>(_: I, _: (), _: impl IntoIterator<Item = char>) {}
12 changes: 12 additions & 0 deletions tests/ui/needless_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,16 @@ fn main() {

let _ = sample.iter().collect::<VecWrapper<_>>().is_empty();
let _ = sample.iter().collect::<VecWrapper<_>>().contains(&&0);

#[allow(clippy::double_parens)]
{
Vec::<u8>::new().extend((0..10).collect::<Vec<_>>());
foo((0..10).collect::<Vec<_>>());
bar((0..10).collect::<Vec<_>>(), (0..10).collect::<Vec<_>>());
baz((0..10), (), ('a'..='z').collect::<Vec<_>>())
}
}

fn foo(_: impl IntoIterator<Item = usize>) {}
fn bar<I: IntoIterator<Item = usize>>(_: Vec<usize>, _: I) {}
fn baz<I: IntoIterator<Item = usize>>(_: I, _: (), _: impl IntoIterator<Item = char>) {}
26 changes: 25 additions & 1 deletion tests/ui/needless_collect.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,29 @@ error: avoid using `collect()` when not needed
LL | let _ = sample.iter().collect::<VecWrapper<_>>().contains(&&0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &0)`

error: aborting due to 15 previous errors
error: avoid using `collect()` when not needed
--> $DIR/needless_collect.rs:68:40
|
LL | Vec::<u8>::new().extend((0..10).collect::<Vec<_>>());
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call

error: avoid using `collect()` when not needed
--> $DIR/needless_collect.rs:69:20
|
LL | foo((0..10).collect::<Vec<_>>());
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call

error: avoid using `collect()` when not needed
--> $DIR/needless_collect.rs:70:49
|
LL | bar((0..10).collect::<Vec<_>>(), (0..10).collect::<Vec<_>>());
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call

error: avoid using `collect()` when not needed
--> $DIR/needless_collect.rs:71:37
|
LL | baz((0..10), (), ('a'..='z').collect::<Vec<_>>())
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call

error: aborting due to 19 previous errors