From 27acfd8a5b9dd1eb91e044e07201b4a9044a2bbd Mon Sep 17 00:00:00 2001 From: Quentin Santos Date: Wed, 1 Jan 2025 20:33:58 +0100 Subject: [PATCH] Prefer if chain to let-else --- .../src/methods/double_ended_iterator_last.rs | 42 ++++++------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/clippy_lints/src/methods/double_ended_iterator_last.rs b/clippy_lints/src/methods/double_ended_iterator_last.rs index 1322108e6fef..dec59b1c34ef 100644 --- a/clippy_lints/src/methods/double_ended_iterator_last.rs +++ b/clippy_lints/src/methods/double_ended_iterator_last.rs @@ -9,34 +9,18 @@ use rustc_span::{Span, sym}; use super::DOUBLE_ENDED_ITERATOR_LAST; pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, self_expr: &'_ Expr<'_>, call_span: Span) { - let typeck = cx.typeck_results(); - - // Check if the current "last" method is that of the Iterator trait - if !is_trait_method(cx, expr, sym::Iterator) { - return; + if is_trait_method(cx, expr, sym::Iterator) + && let Some(deiter_id) = cx.tcx.get_diagnostic_item(sym::DoubleEndedIterator) + && implements_trait(cx, cx.typeck_results().expr_ty(self_expr).peel_refs(), deiter_id, &[]) + { + span_lint_and_sugg( + cx, + DOUBLE_ENDED_ITERATOR_LAST, + call_span, + "called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator", + "try", + "next_back()".to_string(), + Applicability::MachineApplicable, + ); } - - // Find id for DoubleEndedIterator trait - let Some(deiter_id) = cx.tcx.get_diagnostic_item(sym::DoubleEndedIterator) else { - return; - }; - - // Find the type of self - let self_type = typeck.expr_ty(self_expr).peel_refs(); - - // Check that the object implements the DoubleEndedIterator trait - if !implements_trait(cx, self_type, deiter_id, &[]) { - return; - } - - // Emit lint - span_lint_and_sugg( - cx, - DOUBLE_ENDED_ITERATOR_LAST, - call_span, - "called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator", - "try", - "next_back()".to_string(), - Applicability::MachineApplicable, - ); }