Skip to content

Commit ca2ec7a

Browse files
committed
Fix NonZero clippy lints.
1 parent 554b0f7 commit ca2ec7a

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

src/tools/clippy/clippy_lints/src/operators/arithmetic_side_effects.rs

+31-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use super::ARITHMETIC_SIDE_EFFECTS;
22
use clippy_utils::consts::{constant, constant_simple, Constant};
33
use clippy_utils::diagnostics::span_lint;
4-
use clippy_utils::ty::type_diagnostic_name;
4+
use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::{expr_or_init, is_from_proc_macro, is_lint_allowed, peel_hir_expr_refs, peel_hir_expr_unary};
66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_middle::ty::Ty;
8+
use rustc_middle::ty::{self, Ty};
99
use rustc_session::impl_lint_pass;
1010
use rustc_span::source_map::Spanned;
1111
use rustc_span::symbol::sym;
@@ -88,37 +88,44 @@ impl ArithmeticSideEffects {
8888
}
8989

9090
/// Verifies built-in types that have specific allowed operations
91-
fn has_specific_allowed_type_and_operation(
92-
cx: &LateContext<'_>,
93-
lhs_ty: Ty<'_>,
91+
fn has_specific_allowed_type_and_operation<'tcx>(
92+
cx: &LateContext<'tcx>,
93+
lhs_ty: Ty<'tcx>,
9494
op: &Spanned<hir::BinOpKind>,
95-
rhs_ty: Ty<'_>,
95+
rhs_ty: Ty<'tcx>,
9696
) -> bool {
9797
let is_div_or_rem = matches!(op.node, hir::BinOpKind::Div | hir::BinOpKind::Rem);
98-
let is_non_zero_u = |symbol: Option<Symbol>| {
99-
matches!(
100-
symbol,
101-
Some(
102-
sym::NonZeroU128
103-
| sym::NonZeroU16
104-
| sym::NonZeroU32
105-
| sym::NonZeroU64
106-
| sym::NonZeroU8
107-
| sym::NonZeroUsize
108-
)
109-
)
98+
let is_non_zero_u = |cx: &LateContext<'tcx>, ty: Ty<'tcx>| {
99+
let tcx = cx.tcx;
100+
101+
let ty::Adt(adt, substs) = ty.kind() else { return false };
102+
103+
if !tcx.is_diagnostic_item(sym::NonZero, adt.did()) {
104+
return false;
105+
};
106+
107+
let int_type = substs.type_at(0);
108+
let unsigned_int_types = [
109+
tcx.types.u8,
110+
tcx.types.u16,
111+
tcx.types.u32,
112+
tcx.types.u64,
113+
tcx.types.u128,
114+
tcx.types.usize,
115+
];
116+
117+
unsigned_int_types.contains(&int_type)
110118
};
111119
let is_sat_or_wrap = |ty: Ty<'_>| {
112-
let is_sat = type_diagnostic_name(cx, ty) == Some(sym::Saturating);
113-
let is_wrap = type_diagnostic_name(cx, ty) == Some(sym::Wrapping);
114-
is_sat || is_wrap
120+
is_type_diagnostic_item(cx, ty, sym::Saturating) || is_type_diagnostic_item(cx, ty, sym::Wrapping)
115121
};
116122

117-
// If the RHS is NonZeroU*, then division or module by zero will never occur
118-
if is_non_zero_u(type_diagnostic_name(cx, rhs_ty)) && is_div_or_rem {
123+
// If the RHS is `NonZero<u*>`, then division or module by zero will never occur.
124+
if is_non_zero_u(cx, rhs_ty) && is_div_or_rem {
119125
return true;
120126
}
121-
// `Saturation` and `Wrapping` can overflow if the RHS is zero in a division or module
127+
128+
// `Saturation` and `Wrapping` can overflow if the RHS is zero in a division or module.
122129
if is_sat_or_wrap(lhs_ty) {
123130
return !is_div_or_rem;
124131
}

src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_non_zero.rs

+33-18
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,55 @@ pub(super) fn check<'tcx>(
1616
to_ty: Ty<'tcx>,
1717
arg: &'tcx Expr<'_>,
1818
) -> bool {
19-
let (ty::Int(_) | ty::Uint(_), Some(to_ty_adt)) = (&from_ty.kind(), to_ty.ty_adt_def()) else {
19+
let tcx = cx.tcx;
20+
21+
let (ty::Int(_) | ty::Uint(_), ty::Adt(adt, substs)) = (&from_ty.kind(), to_ty.kind()) else {
2022
return false;
2123
};
22-
let Some(to_type_sym) = cx.tcx.get_diagnostic_name(to_ty_adt.did()) else {
24+
25+
if !tcx.is_diagnostic_item(sym::NonZero, adt.did()) {
2326
return false;
2427
};
2528

26-
if !matches!(
27-
to_type_sym,
28-
sym::NonZeroU8
29-
| sym::NonZeroU16
30-
| sym::NonZeroU32
31-
| sym::NonZeroU64
32-
| sym::NonZeroU128
33-
| sym::NonZeroI8
34-
| sym::NonZeroI16
35-
| sym::NonZeroI32
36-
| sym::NonZeroI64
37-
| sym::NonZeroI128
38-
) {
29+
// FIXME: This can be simplified once `NonZero<T>` is stable.
30+
let coercable_types = [
31+
("NonZeroU8", tcx.types.u8),
32+
("NonZeroU16", tcx.types.u16),
33+
("NonZeroU32", tcx.types.u32),
34+
("NonZeroU64", tcx.types.u64),
35+
("NonZeroU128", tcx.types.u128),
36+
("NonZeroUsize", tcx.types.usize),
37+
("NonZeroI8", tcx.types.i8),
38+
("NonZeroI16", tcx.types.i16),
39+
("NonZeroI32", tcx.types.i32),
40+
("NonZeroI64", tcx.types.i64),
41+
("NonZeroI128", tcx.types.i128),
42+
("NonZeroIsize", tcx.types.isize),
43+
];
44+
45+
let int_type = substs.type_at(0);
46+
47+
let Some(nonzero_alias) = coercable_types.iter().find_map(|(nonzero_alias, t)| {
48+
if *t == int_type && *t == from_ty {
49+
Some(nonzero_alias)
50+
} else {
51+
None
52+
}
53+
}) else {
3954
return false;
40-
}
55+
};
4156

4257
span_lint_and_then(
4358
cx,
4459
TRANSMUTE_INT_TO_NON_ZERO,
4560
e.span,
46-
&format!("transmute from a `{from_ty}` to a `{to_type_sym}`"),
61+
&format!("transmute from a `{from_ty}` to a `{nonzero_alias}`"),
4762
|diag| {
4863
let arg = sugg::Sugg::hir(cx, arg, "..");
4964
diag.span_suggestion(
5065
e.span,
5166
"consider using",
52-
format!("{to_type_sym}::{}({arg})", sym::new_unchecked),
67+
format!("{nonzero_alias}::{}({arg})", sym::new_unchecked),
5368
Applicability::Unspecified,
5469
);
5570
},

0 commit comments

Comments
 (0)