about summary refs log tree commit diff
diff options
context:
space:
mode:
authorQuentin Santos <qsantos@qsantos.fr>2025-01-01 20:33:58 +0100
committerQuentin Santos <qsantos@qsantos.fr>2025-01-01 20:33:58 +0100
commit27acfd8a5b9dd1eb91e044e07201b4a9044a2bbd (patch)
treea7f005bf718ca5173d50a05e6769b014ff3ff22b
parent09c5d34f98f53d4f0b4181404608d4c1bcede4d5 (diff)
downloadrust-27acfd8a5b9dd1eb91e044e07201b4a9044a2bbd.tar.gz
rust-27acfd8a5b9dd1eb91e044e07201b4a9044a2bbd.zip
Prefer if chain to let-else
-rw-r--r--clippy_lints/src/methods/double_ended_iterator_last.rs42
1 files 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 1322108e6fe..dec59b1c34e 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;
-    }
-
-    // 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;
+    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,
+        );
     }
-
-    // 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,
-    );
 }