Skip to content

Commit 67a886b

Browse files
authored
Rollup merge of rust-lang#124587 - reitermarkus:use-generic-nonzero, r=dtolnay
Generic `NonZero` post-stabilization changes. Tracking issue: rust-lang#120257 r? ``@dtolnay``
2 parents eef0828 + d326298 commit 67a886b

11 files changed

+149
-172
lines changed

clippy_lints/src/transmute/eager_transmute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub(super) fn check<'tcx>(
8787
&& is_normalizable(cx, cx.param_env, from_ty)
8888
&& is_normalizable(cx, cx.param_env, to_ty)
8989
// we only want to lint if the target type has a niche that is larger than the one of the source type
90-
// e.g. `u8` to `NonZeroU8` should lint, but `NonZeroU8` to `u8` should not
90+
// e.g. `u8` to `NonZero<u8>` should lint, but `NonZero<u8>` to `u8` should not
9191
&& let Ok(from_layout) = cx.tcx.layout_of(cx.param_env.and(from_ty))
9292
&& let Ok(to_layout) = cx.tcx.layout_of(cx.param_env.and(to_ty))
9393
&& match (from_layout.largest_niche, to_layout.largest_niche) {

clippy_lints/src/transmute/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ declare_clippy_lint! {
257257

258258
declare_clippy_lint! {
259259
/// ### What it does
260-
/// Checks for transmutes from integers to `NonZero*` types, and suggests their `new_unchecked`
260+
/// Checks for transmutes from `T` to `NonZero<T>`, and suggests the `new_unchecked`
261261
/// method instead.
262262
///
263263
/// ### Why is this bad?
@@ -266,13 +266,13 @@ declare_clippy_lint! {
266266
///
267267
/// ### Example
268268
/// ```no_run
269-
/// # use core::num::NonZeroU32;
270-
/// let _non_zero: NonZeroU32 = unsafe { std::mem::transmute(123) };
269+
/// # use core::num::NonZero;
270+
/// let _: NonZero<u32> = unsafe { std::mem::transmute(123) };
271271
/// ```
272272
/// Use instead:
273273
/// ```no_run
274-
/// # use core::num::NonZeroU32;
275-
/// let _non_zero = unsafe { NonZeroU32::new_unchecked(123) };
274+
/// # use core::num::NonZero;
275+
/// let _: NonZero<u32> = unsafe { NonZero::new_unchecked(123) };
276276
/// ```
277277
#[clippy::version = "1.69.0"]
278278
pub TRANSMUTE_INT_TO_NON_ZERO,

clippy_lints/src/transmute/transmute_int_to_non_zero.rs

+6-29
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,22 @@ pub(super) fn check<'tcx>(
2626
return false;
2727
};
2828

29-
// FIXME: This can be simplified once `NonZero<T>` is stable.
30-
let coercible_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) = coercible_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 {
54-
return false;
55-
};
29+
let int_ty = substs.type_at(0);
30+
if from_ty != int_ty {
31+
return false;
32+
}
5633

5734
span_lint_and_then(
5835
cx,
5936
TRANSMUTE_INT_TO_NON_ZERO,
6037
e.span,
61-
format!("transmute from a `{from_ty}` to a `{nonzero_alias}`"),
38+
format!("transmute from a `{from_ty}` to a `{}<{int_ty}>`", sym::NonZero),
6239
|diag| {
6340
let arg = sugg::Sugg::hir(cx, arg, "..");
6441
diag.span_suggestion(
6542
e.span,
6643
"consider using",
67-
format!("{nonzero_alias}::{}({arg})", sym::new_unchecked),
44+
format!("{}::{}({arg})", sym::NonZero, sym::new_unchecked),
6845
Applicability::Unspecified,
6946
);
7047
},

lintcheck/src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::Parser;
2-
use std::num::NonZeroUsize;
2+
use std::num::NonZero;
33
use std::path::PathBuf;
44

55
#[derive(Clone, Debug, Parser)]
@@ -61,7 +61,7 @@ impl LintcheckConfig {
6161
config.max_jobs = if config.fix || config.recursive {
6262
1
6363
} else {
64-
std::thread::available_parallelism().map_or(1, NonZeroUsize::get)
64+
std::thread::available_parallelism().map_or(1, NonZero::get)
6565
};
6666
};
6767

tests/ui/arithmetic_side_effects.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
extern crate proc_macro_derive;
1717

18-
use core::num::{NonZeroUsize, Saturating, Wrapping};
18+
use core::num::{NonZero, Saturating, Wrapping};
1919

2020
const ONE: i32 = 1;
2121
const ZERO: i32 = 0;
@@ -494,15 +494,15 @@ pub fn issue_11262() {
494494
}
495495

496496
pub fn issue_11392() {
497-
fn example_div(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize {
497+
fn example_div(unsigned: usize, nonzero_unsigned: NonZero<usize>) -> usize {
498498
unsigned / nonzero_unsigned
499499
}
500500

501-
fn example_rem(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize {
501+
fn example_rem(unsigned: usize, nonzero_unsigned: NonZero<usize>) -> usize {
502502
unsigned % nonzero_unsigned
503503
}
504504

505-
let (unsigned, nonzero_unsigned) = (0, NonZeroUsize::new(1).unwrap());
505+
let (unsigned, nonzero_unsigned) = (0, NonZero::new(1).unwrap());
506506
example_div(unsigned, nonzero_unsigned);
507507
example_rem(unsigned, nonzero_unsigned);
508508
}

tests/ui/eager_transmute.fixed

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![warn(clippy::eager_transmute)]
33
#![allow(clippy::transmute_int_to_non_zero, clippy::missing_transmute_annotations)]
44

5-
use std::num::NonZeroU8;
5+
use std::num::NonZero;
66

77
#[repr(u8)]
88
enum Opcode {
@@ -85,21 +85,21 @@ macro_rules! impls {
8585
}
8686
impls!(NonMaxU8, NonZeroNonMaxU8);
8787

88-
fn niche_tests(v1: u8, v2: NonZeroU8, v3: NonZeroNonMaxU8) {
89-
// u8 -> NonZeroU8, do lint
90-
let _: Option<NonZeroU8> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
88+
fn niche_tests(v1: u8, v2: NonZero<u8>, v3: NonZeroNonMaxU8) {
89+
// u8 -> NonZero<u8>, do lint
90+
let _: Option<NonZero<u8>> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
9191

92-
// NonZeroU8 -> u8, don't lint, target type has no niche and therefore a higher validity range
93-
let _: Option<u8> = (v2 > NonZeroU8::new(1).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
92+
// NonZero<u8> -> u8, don't lint, target type has no niche and therefore a higher validity range
93+
let _: Option<u8> = (v2 > NonZero::new(1u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
9494

95-
// NonZeroU8 -> NonMaxU8, do lint, different niche
96-
let _: Option<NonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
95+
// NonZero<u8> -> NonMaxU8, do lint, different niche
96+
let _: Option<NonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
9797

9898
// NonZeroNonMaxU8 -> NonMaxU8, don't lint, target type has more validity
9999
let _: Option<NonMaxU8> = (v3 < 255).then_some(unsafe { std::mem::transmute(v2) });
100100

101-
// NonZeroU8 -> NonZeroNonMaxU8, do lint, target type has less validity
102-
let _: Option<NonZeroNonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
101+
// NonZero<u8> -> NonZeroNonMaxU8, do lint, target type has less validity
102+
let _: Option<NonZeroNonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
103103
}
104104

105105
fn main() {}

tests/ui/eager_transmute.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![warn(clippy::eager_transmute)]
33
#![allow(clippy::transmute_int_to_non_zero, clippy::missing_transmute_annotations)]
44

5-
use std::num::NonZeroU8;
5+
use std::num::NonZero;
66

77
#[repr(u8)]
88
enum Opcode {
@@ -85,21 +85,21 @@ macro_rules! impls {
8585
}
8686
impls!(NonMaxU8, NonZeroNonMaxU8);
8787

88-
fn niche_tests(v1: u8, v2: NonZeroU8, v3: NonZeroNonMaxU8) {
89-
// u8 -> NonZeroU8, do lint
90-
let _: Option<NonZeroU8> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
88+
fn niche_tests(v1: u8, v2: NonZero<u8>, v3: NonZeroNonMaxU8) {
89+
// u8 -> NonZero<u8>, do lint
90+
let _: Option<NonZero<u8>> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
9191

92-
// NonZeroU8 -> u8, don't lint, target type has no niche and therefore a higher validity range
93-
let _: Option<u8> = (v2 > NonZeroU8::new(1).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
92+
// NonZero<u8> -> u8, don't lint, target type has no niche and therefore a higher validity range
93+
let _: Option<u8> = (v2 > NonZero::new(1u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
9494

95-
// NonZeroU8 -> NonMaxU8, do lint, different niche
96-
let _: Option<NonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
95+
// NonZero<u8> -> NonMaxU8, do lint, different niche
96+
let _: Option<NonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
9797

9898
// NonZeroNonMaxU8 -> NonMaxU8, don't lint, target type has more validity
9999
let _: Option<NonMaxU8> = (v3 < 255).then_some(unsafe { std::mem::transmute(v2) });
100100

101-
// NonZeroU8 -> NonZeroNonMaxU8, do lint, target type has less validity
102-
let _: Option<NonZeroNonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
101+
// NonZero<u8> -> NonZeroNonMaxU8, do lint, target type has less validity
102+
let _: Option<NonZeroNonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
103103
}
104104

105105
fn main() {}

tests/ui/eager_transmute.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -155,36 +155,36 @@ LL | (op < 4).then(|| std::mem::transmute::<_, Opcode>(op));
155155
| ~~~~ ++
156156

157157
error: this transmute is always evaluated eagerly, even if the condition is false
158-
--> tests/ui/eager_transmute.rs:90:60
158+
--> tests/ui/eager_transmute.rs:90:62
159159
|
160-
LL | let _: Option<NonZeroU8> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
161-
| ^^^^^^^^^^^^^^^^^^^^^^^
160+
LL | let _: Option<NonZero<u8>> = (v1 > 0).then_some(unsafe { std::mem::transmute(v1) });
161+
| ^^^^^^^^^^^^^^^^^^^^^^^
162162
|
163163
help: consider using `bool::then` to only transmute if the condition holds
164164
|
165-
LL | let _: Option<NonZeroU8> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
166-
| ~~~~ ++
165+
LL | let _: Option<NonZero<u8>> = (v1 > 0).then(|| unsafe { std::mem::transmute(v1) });
166+
| ~~~~ ++
167167

168168
error: this transmute is always evaluated eagerly, even if the condition is false
169169
--> tests/ui/eager_transmute.rs:96:86
170170
|
171-
LL | let _: Option<NonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
171+
LL | let _: Option<NonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
172172
| ^^^^^^^^^^^^^^^^^^^^^^^
173173
|
174174
help: consider using `bool::then` to only transmute if the condition holds
175175
|
176-
LL | let _: Option<NonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
176+
LL | let _: Option<NonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
177177
| ~~~~ ++
178178

179179
error: this transmute is always evaluated eagerly, even if the condition is false
180180
--> tests/ui/eager_transmute.rs:102:93
181181
|
182-
LL | let _: Option<NonZeroNonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
182+
LL | let _: Option<NonZeroNonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then_some(unsafe { std::mem::transmute(v2) });
183183
| ^^^^^^^^^^^^^^^^^^^^^^^
184184
|
185185
help: consider using `bool::then` to only transmute if the condition holds
186186
|
187-
LL | let _: Option<NonZeroNonMaxU8> = (v2 < NonZeroU8::new(255).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
187+
LL | let _: Option<NonZeroNonMaxU8> = (v2 < NonZero::new(255u8).unwrap()).then(|| unsafe { std::mem::transmute(v2) });
188188
| ~~~~ ++
189189

190190
error: aborting due to 17 previous errors
+31-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![warn(clippy::transmute_int_to_non_zero)]
22
#![allow(clippy::missing_transmute_annotations)]
33

4-
use core::num::*;
4+
use core::num::NonZero;
55

66
fn main() {
77
let int_u8: u8 = 1;
@@ -16,38 +16,38 @@ fn main() {
1616
let int_i64: i64 = 1;
1717
let int_i128: i128 = 1;
1818

19-
let _: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(int_u8) };
20-
//~^ ERROR: transmute from a `u8` to a `NonZeroU8`
19+
let _: NonZero<u8> = unsafe { NonZero::new_unchecked(int_u8) };
20+
//~^ ERROR: transmute from a `u8` to a `NonZero<u8>`
2121
//~| NOTE: `-D clippy::transmute-int-to-non-zero` implied by `-D warnings`
22-
let _: NonZeroU16 = unsafe { NonZeroU16::new_unchecked(int_u16) };
23-
//~^ ERROR: transmute from a `u16` to a `NonZeroU16`
24-
let _: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(int_u32) };
25-
//~^ ERROR: transmute from a `u32` to a `NonZeroU32`
26-
let _: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(int_u64) };
27-
//~^ ERROR: transmute from a `u64` to a `NonZeroU64`
28-
let _: NonZeroU128 = unsafe { NonZeroU128::new_unchecked(int_u128) };
29-
//~^ ERROR: transmute from a `u128` to a `NonZeroU128`
22+
let _: NonZero<u16> = unsafe { NonZero::new_unchecked(int_u16) };
23+
//~^ ERROR: transmute from a `u16` to a `NonZero<u16>`
24+
let _: NonZero<u32> = unsafe { NonZero::new_unchecked(int_u32) };
25+
//~^ ERROR: transmute from a `u32` to a `NonZero<u32>`
26+
let _: NonZero<u64> = unsafe { NonZero::new_unchecked(int_u64) };
27+
//~^ ERROR: transmute from a `u64` to a `NonZero<u64>`
28+
let _: NonZero<u128> = unsafe { NonZero::new_unchecked(int_u128) };
29+
//~^ ERROR: transmute from a `u128` to a `NonZero<u128>`
3030

31-
let _: NonZeroI8 = unsafe { NonZeroI8::new_unchecked(int_i8) };
32-
//~^ ERROR: transmute from a `i8` to a `NonZeroI8`
33-
let _: NonZeroI16 = unsafe { NonZeroI16::new_unchecked(int_i16) };
34-
//~^ ERROR: transmute from a `i16` to a `NonZeroI16`
35-
let _: NonZeroI32 = unsafe { NonZeroI32::new_unchecked(int_i32) };
36-
//~^ ERROR: transmute from a `i32` to a `NonZeroI32`
37-
let _: NonZeroI64 = unsafe { NonZeroI64::new_unchecked(int_i64) };
38-
//~^ ERROR: transmute from a `i64` to a `NonZeroI64`
39-
let _: NonZeroI128 = unsafe { NonZeroI128::new_unchecked(int_i128) };
40-
//~^ ERROR: transmute from a `i128` to a `NonZeroI128`
31+
let _: NonZero<i8> = unsafe { NonZero::new_unchecked(int_i8) };
32+
//~^ ERROR: transmute from a `i8` to a `NonZero<i8>`
33+
let _: NonZero<i16> = unsafe { NonZero::new_unchecked(int_i16) };
34+
//~^ ERROR: transmute from a `i16` to a `NonZero<i16>`
35+
let _: NonZero<i32> = unsafe { NonZero::new_unchecked(int_i32) };
36+
//~^ ERROR: transmute from a `i32` to a `NonZero<i32>`
37+
let _: NonZero<i64> = unsafe { NonZero::new_unchecked(int_i64) };
38+
//~^ ERROR: transmute from a `i64` to a `NonZero<i64>`
39+
let _: NonZero<i128> = unsafe { NonZero::new_unchecked(int_i128) };
40+
//~^ ERROR: transmute from a `i128` to a `NonZero<i128>`
4141

42-
let _: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(int_u8) };
43-
let _: NonZeroU16 = unsafe { NonZeroU16::new_unchecked(int_u16) };
44-
let _: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(int_u32) };
45-
let _: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(int_u64) };
46-
let _: NonZeroU128 = unsafe { NonZeroU128::new_unchecked(int_u128) };
42+
let _: NonZero<u8> = unsafe { NonZero::new_unchecked(int_u8) };
43+
let _: NonZero<u16> = unsafe { NonZero::new_unchecked(int_u16) };
44+
let _: NonZero<u32> = unsafe { NonZero::new_unchecked(int_u32) };
45+
let _: NonZero<u64> = unsafe { NonZero::new_unchecked(int_u64) };
46+
let _: NonZero<u128> = unsafe { NonZero::new_unchecked(int_u128) };
4747

48-
let _: NonZeroI8 = unsafe { NonZeroI8::new_unchecked(int_i8) };
49-
let _: NonZeroI16 = unsafe { NonZeroI16::new_unchecked(int_i16) };
50-
let _: NonZeroI32 = unsafe { NonZeroI32::new_unchecked(int_i32) };
51-
let _: NonZeroI64 = unsafe { NonZeroI64::new_unchecked(int_i64) };
52-
let _: NonZeroI128 = unsafe { NonZeroI128::new_unchecked(int_i128) };
48+
let _: NonZero<i8> = unsafe { NonZero::new_unchecked(int_i8) };
49+
let _: NonZero<i16> = unsafe { NonZero::new_unchecked(int_i16) };
50+
let _: NonZero<i32> = unsafe { NonZero::new_unchecked(int_i32) };
51+
let _: NonZero<i64> = unsafe { NonZero::new_unchecked(int_i64) };
52+
let _: NonZero<i128> = unsafe { NonZero::new_unchecked(int_i128) };
5353
}

0 commit comments

Comments
 (0)