Skip to content

Commit b6343a5

Browse files
unused_unit: small clean-up (#15550)
changelog: none
2 parents 5f80080 + e185efe commit b6343a5

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

clippy_lints/src/unused_unit.rs

Lines changed: 16 additions & 21 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
@@ -97,16 +97,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedUnit {
9797
}
9898

9999
fn check_poly_trait_ref(&mut self, cx: &LateContext<'tcx>, poly: &'tcx PolyTraitRef<'tcx>) {
100-
let segments = &poly.trait_ref.path.segments;
101-
102-
if segments.len() == 1
103-
&& matches!(segments[0].ident.name, sym::Fn | sym::FnMut | sym::FnOnce)
104-
&& let Some(args) = segments[0].args
100+
if let [segment] = &poly.trait_ref.path.segments
101+
&& matches!(segment.ident.name, sym::Fn | sym::FnMut | sym::FnOnce)
102+
&& let Some(args) = segment.args
105103
&& args.parenthesized == GenericArgsParentheses::ParenSugar
106-
&& let constraints = &args.constraints
107-
&& constraints.len() == 1
108-
&& constraints[0].ident.name == sym::Output
109-
&& let AssocItemConstraintKind::Equality { term: Term::Ty(hir_ty) } = constraints[0].kind
104+
&& let [constraint] = &args.constraints
105+
&& constraint.ident.name == sym::Output
106+
&& let AssocItemConstraintKind::Equality { term: Term::Ty(hir_ty) } = constraint.kind
110107
&& args.span_ext.hi() != poly.span.hi()
111108
&& !hir_ty.span.from_expansion()
112109
&& args.span_ext.hi() != hir_ty.span.hi()
@@ -160,17 +157,15 @@ fn get_def(span: Span) -> Option<Span> {
160157

161158
fn lint_unneeded_unit_return(cx: &LateContext<'_>, ty_span: Span, span: Span) {
162159
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-
});
160+
if let Some(Some(rpos)) = span.with_hi(ty_span.hi()).with_source_text(cx, position_before_rarrow) {
161+
(
162+
ty_span.with_lo(span.lo() + BytePos::from_usize(rpos)),
163+
Applicability::MachineApplicable,
164+
)
165+
} else {
166+
(ty_span, Applicability::MaybeIncorrect)
167+
};
168+
174169
span_lint_and_sugg(
175170
cx,
176171
UNUSED_UNIT,

0 commit comments

Comments
 (0)