Skip to content

Commit 852e552

Browse files
committed
get rid of confusing nested map_ors
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
1 parent 5f80080 commit 852e552

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

clippy_lints/src/unused_unit.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::{
1212
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
1313
use rustc_session::declare_lint_pass;
1414
use rustc_span::edition::Edition;
15-
use rustc_span::{BytePos, Span, sym};
15+
use rustc_span::{BytePos, Pos as _, Span, sym};
1616

1717
declare_clippy_lint! {
1818
/// ### What it does
@@ -160,17 +160,15 @@ fn get_def(span: Span) -> Option<Span> {
160160

161161
fn lint_unneeded_unit_return(cx: &LateContext<'_>, ty_span: Span, span: Span) {
162162
let (ret_span, appl) =
163-
span.with_hi(ty_span.hi())
164-
.get_source_text(cx)
165-
.map_or((ty_span, Applicability::MaybeIncorrect), |src| {
166-
position_before_rarrow(&src).map_or((ty_span, Applicability::MaybeIncorrect), |rpos| {
167-
(
168-
#[expect(clippy::cast_possible_truncation)]
169-
ty_span.with_lo(BytePos(span.lo().0 + rpos as u32)),
170-
Applicability::MachineApplicable,
171-
)
172-
})
173-
});
163+
if let Some(Some(rpos)) = span.with_hi(ty_span.hi()).with_source_text(cx, position_before_rarrow) {
164+
(
165+
ty_span.with_lo(span.lo() + BytePos::from_usize(rpos)),
166+
Applicability::MachineApplicable,
167+
)
168+
} else {
169+
(ty_span, Applicability::MaybeIncorrect)
170+
};
171+
174172
span_lint_and_sugg(
175173
cx,
176174
UNUSED_UNIT,

0 commit comments

Comments
 (0)