Skip to content

Commit c015622

Browse files
committed
Auto merge of #6304 - matthiaskrgr:crash_6302, r=llogiq
FROM_ITER_INSTEAD_OF_COLLECT: avoid unwrapping unconditionally Fixes #6302 changelog: fix unwrap of None when checking libcore with clippy
2 parents 5effc99 + 5253595 commit c015622

File tree

1 file changed

+17
-14
lines changed
  • clippy_lints/src/methods

1 file changed

+17
-14
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,21 +3901,24 @@ fn lint_from_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<
39013901
let ty = cx.typeck_results().expr_ty(expr);
39023902
let arg_ty = cx.typeck_results().expr_ty(&args[0]);
39033903

3904-
let from_iter_id = get_trait_def_id(cx, &paths::FROM_ITERATOR).unwrap();
3905-
let iter_id = get_trait_def_id(cx, &paths::ITERATOR).unwrap();
3904+
if_chain! {
3905+
if let Some(from_iter_id) = get_trait_def_id(cx, &paths::FROM_ITERATOR);
3906+
if let Some(iter_id) = get_trait_def_id(cx, &paths::ITERATOR);
39063907

3907-
if implements_trait(cx, ty, from_iter_id, &[]) && implements_trait(cx, arg_ty, iter_id, &[]) {
3908-
// `expr` implements `FromIterator` trait
3909-
let iter_expr = snippet(cx, args[0].span, "..");
3910-
span_lint_and_sugg(
3911-
cx,
3912-
FROM_ITER_INSTEAD_OF_COLLECT,
3913-
expr.span,
3914-
"usage of `FromIterator::from_iter`",
3915-
"use `.collect()` instead of `::from_iter()`",
3916-
format!("{}.collect()", iter_expr),
3917-
Applicability::MaybeIncorrect,
3918-
);
3908+
if implements_trait(cx, ty, from_iter_id, &[]) && implements_trait(cx, arg_ty, iter_id, &[]);
3909+
then {
3910+
// `expr` implements `FromIterator` trait
3911+
let iter_expr = snippet(cx, args[0].span, "..");
3912+
span_lint_and_sugg(
3913+
cx,
3914+
FROM_ITER_INSTEAD_OF_COLLECT,
3915+
expr.span,
3916+
"usage of `FromIterator::from_iter`",
3917+
"use `.collect()` instead of `::from_iter()`",
3918+
format!("{}.collect()", iter_expr),
3919+
Applicability::MaybeIncorrect,
3920+
);
3921+
}
39193922
}
39203923
}
39213924

0 commit comments

Comments
 (0)