about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAda Alakbarova <ada.alakbarova@proton.me>2025-08-23 13:54:22 +0200
committerAda Alakbarova <ada.alakbarova@proton.me>2025-08-24 12:45:04 +0200
commit852e552be4753043644294ed5cc71daec0f8eedd (patch)
treed3084ebc77e791b484528a73a2464afabcc1f218
parent5f80080bd86aac4bcba2e62a8dc1d819e903790f (diff)
downloadrust-852e552be4753043644294ed5cc71daec0f8eedd.tar.gz
rust-852e552be4753043644294ed5cc71daec0f8eedd.zip
get rid of confusing nested `map_or`s
Their default branches are even the same, which means that one of the
`map_or`s could've been replaced with an `and_then`, but since we have
access to let-chains, why not use that

Additionally:
- use `with_source_text` to avoid constructing a `SourceText` object
- use `BytePos::from_usize` to avoid `allow`ing the lint
-rw-r--r--clippy_lints/src/unused_unit.rs22
1 files changed, 10 insertions, 12 deletions
diff --git a/clippy_lints/src/unused_unit.rs b/clippy_lints/src/unused_unit.rs
index 3811f0fe6b5..68d1bbe6b85 100644
--- a/clippy_lints/src/unused_unit.rs
+++ b/clippy_lints/src/unused_unit.rs
@@ -12,7 +12,7 @@ use rustc_hir::{
 use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
 use rustc_session::declare_lint_pass;
 use rustc_span::edition::Edition;
-use rustc_span::{BytePos, Span, sym};
+use rustc_span::{BytePos, Pos as _, Span, sym};
 
 declare_clippy_lint! {
     /// ### What it does
@@ -160,17 +160,15 @@ fn get_def(span: Span) -> Option<Span> {
 
 fn lint_unneeded_unit_return(cx: &LateContext<'_>, ty_span: Span, span: Span) {
     let (ret_span, appl) =
-        span.with_hi(ty_span.hi())
-            .get_source_text(cx)
-            .map_or((ty_span, Applicability::MaybeIncorrect), |src| {
-                position_before_rarrow(&src).map_or((ty_span, Applicability::MaybeIncorrect), |rpos| {
-                    (
-                        #[expect(clippy::cast_possible_truncation)]
-                        ty_span.with_lo(BytePos(span.lo().0 + rpos as u32)),
-                        Applicability::MachineApplicable,
-                    )
-                })
-            });
+        if let Some(Some(rpos)) = span.with_hi(ty_span.hi()).with_source_text(cx, position_before_rarrow) {
+            (
+                ty_span.with_lo(span.lo() + BytePos::from_usize(rpos)),
+                Applicability::MachineApplicable,
+            )
+        } else {
+            (ty_span, Applicability::MaybeIncorrect)
+        };
+
     span_lint_and_sugg(
         cx,
         UNUSED_UNIT,