Skip to content

Commit 21c829e

Browse files
committed
Simplify collect expr_ty
1 parent 6c1ba7c commit 21c829e

File tree

1 file changed

+3
-19
lines changed

1 file changed

+3
-19
lines changed

clippy_lints/src/loops/needless_collect.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::{is_trait_method, path_to_local_id};
77
use if_chain::if_chain;
88
use rustc_errors::Applicability;
99
use rustc_hir::intravisit::{walk_block, walk_expr, NestedVisitorMap, Visitor};
10-
use rustc_hir::{Block, Expr, ExprKind, GenericArg, GenericArgs, HirId, PatKind, StmtKind, Ty};
10+
use rustc_hir::{Block, Expr, ExprKind, HirId, PatKind, StmtKind};
1111
use rustc_lint::LateContext;
1212
use rustc_middle::hir::map::Map;
1313
use rustc_span::sym;
@@ -24,10 +24,8 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
2424
if let ExprKind::MethodCall(method, _, args, _) = expr.kind;
2525
if let ExprKind::MethodCall(chain_method, method0_span, _, _) = args[0].kind;
2626
if chain_method.ident.name == sym!(collect) && is_trait_method(cx, &args[0], sym::Iterator);
27-
if let Some(generic_args) = chain_method.args;
28-
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
29-
if let Some(ty) = cx.typeck_results().node_type_opt(ty.hir_id);
3027
then {
28+
let ty = cx.typeck_results().expr_ty(&args[0]);
3129
let mut applicability = Applicability::MachineApplicable;
3230
let is_empty_sugg = "next().is_none()".to_string();
3331
let method_name = &*method.ident.name.as_str();
@@ -72,19 +70,6 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
7270
}
7371

7472
fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) {
75-
fn get_hir_id<'tcx>(ty: Option<&Ty<'tcx>>, method_args: Option<&GenericArgs<'tcx>>) -> Option<HirId> {
76-
if let Some(ty) = ty {
77-
return Some(ty.hir_id);
78-
}
79-
80-
if let Some(generic_args) = method_args {
81-
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0) {
82-
return Some(ty.hir_id);
83-
}
84-
}
85-
86-
None
87-
}
8873
if let ExprKind::Block(block, _) = expr.kind {
8974
for stmt in block.stmts {
9075
if_chain! {
@@ -93,8 +78,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
9378
if let Some(init_expr) = local.init;
9479
if let ExprKind::MethodCall(method_name, collect_span, &[ref iter_source], ..) = init_expr.kind;
9580
if method_name.ident.name == sym!(collect) && is_trait_method(cx, init_expr, sym::Iterator);
96-
if let Some(hir_id) = get_hir_id(local.ty, method_name.args);
97-
if let Some(ty) = cx.typeck_results().node_type_opt(hir_id);
81+
let ty = cx.typeck_results().expr_ty(init_expr);
9882
if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
9983
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
10084
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) ||

0 commit comments

Comments
 (0)