Skip to content

Commit 27acfd8

Browse files
committed
Prefer if chain to let-else
1 parent 09c5d34 commit 27acfd8

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

clippy_lints/src/methods/double_ended_iterator_last.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,18 @@ use rustc_span::{Span, sym};
99
use super::DOUBLE_ENDED_ITERATOR_LAST;
1010

1111
pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, self_expr: &'_ Expr<'_>, call_span: Span) {
12-
let typeck = cx.typeck_results();
13-
14-
// Check if the current "last" method is that of the Iterator trait
15-
if !is_trait_method(cx, expr, sym::Iterator) {
16-
return;
17-
}
18-
19-
// Find id for DoubleEndedIterator trait
20-
let Some(deiter_id) = cx.tcx.get_diagnostic_item(sym::DoubleEndedIterator) else {
21-
return;
22-
};
23-
24-
// Find the type of self
25-
let self_type = typeck.expr_ty(self_expr).peel_refs();
26-
27-
// Check that the object implements the DoubleEndedIterator trait
28-
if !implements_trait(cx, self_type, deiter_id, &[]) {
29-
return;
12+
if is_trait_method(cx, expr, sym::Iterator)
13+
&& let Some(deiter_id) = cx.tcx.get_diagnostic_item(sym::DoubleEndedIterator)
14+
&& implements_trait(cx, cx.typeck_results().expr_ty(self_expr).peel_refs(), deiter_id, &[])
15+
{
16+
span_lint_and_sugg(
17+
cx,
18+
DOUBLE_ENDED_ITERATOR_LAST,
19+
call_span,
20+
"called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator",
21+
"try",
22+
"next_back()".to_string(),
23+
Applicability::MachineApplicable,
24+
);
3025
}
31-
32-
// Emit lint
33-
span_lint_and_sugg(
34-
cx,
35-
DOUBLE_ENDED_ITERATOR_LAST,
36-
call_span,
37-
"called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator",
38-
"try",
39-
"next_back()".to_string(),
40-
Applicability::MachineApplicable,
41-
);
4226
}

0 commit comments

Comments
 (0)