Skip to content

Commit 65bac18

Browse files
clean-up transmute_int_to_non_zero (#15521)
changelog: none
2 parents 35fb26f + a50c339 commit 65bac18

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed
Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::TRANSMUTE_INT_TO_NON_ZERO;
2-
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::sugg;
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::sugg::Sugg;
44
use rustc_errors::Applicability;
55
use rustc_hir::Expr;
66
use rustc_lint::LateContext;
@@ -16,35 +16,24 @@ pub(super) fn check<'tcx>(
1616
to_ty: Ty<'tcx>,
1717
arg: &'tcx Expr<'_>,
1818
) -> bool {
19-
let tcx = cx.tcx;
20-
21-
let (ty::Int(_) | ty::Uint(_), ty::Adt(adt, substs)) = (&from_ty.kind(), to_ty.kind()) else {
22-
return false;
23-
};
24-
25-
if !tcx.is_diagnostic_item(sym::NonZero, adt.did()) {
26-
return false;
19+
if let ty::Int(_) | ty::Uint(_) = from_ty.kind()
20+
&& let ty::Adt(adt, substs) = to_ty.kind()
21+
&& cx.tcx.is_diagnostic_item(sym::NonZero, adt.did())
22+
&& let int_ty = substs.type_at(0)
23+
&& from_ty == int_ty
24+
{
25+
let arg = Sugg::hir(cx, arg, "..");
26+
span_lint_and_sugg(
27+
cx,
28+
TRANSMUTE_INT_TO_NON_ZERO,
29+
e.span,
30+
format!("transmute from a `{from_ty}` to a `{}<{int_ty}>`", sym::NonZero),
31+
"consider using",
32+
format!("{}::{}({arg})", sym::NonZero, sym::new_unchecked),
33+
Applicability::Unspecified,
34+
);
35+
true
36+
} else {
37+
false
2738
}
28-
29-
let int_ty = substs.type_at(0);
30-
if from_ty != int_ty {
31-
return false;
32-
}
33-
34-
span_lint_and_then(
35-
cx,
36-
TRANSMUTE_INT_TO_NON_ZERO,
37-
e.span,
38-
format!("transmute from a `{from_ty}` to a `{}<{int_ty}>`", sym::NonZero),
39-
|diag| {
40-
let arg = sugg::Sugg::hir(cx, arg, "..");
41-
diag.span_suggestion(
42-
e.span,
43-
"consider using",
44-
format!("{}::{}({arg})", sym::NonZero, sym::new_unchecked),
45-
Applicability::Unspecified,
46-
);
47-
},
48-
);
49-
true
5039
}

0 commit comments

Comments
 (0)