Skip to content

Commit d326298

Browse files
committed
Use generic NonZero.
1 parent 7ad336f commit d326298

File tree

6 files changed

+36
-36
lines changed

6 files changed

+36
-36
lines changed

clippy_lints/src/transmute/eager_transmute.rs

Lines changed: 1 addition & 1 deletion
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) {

lintcheck/src/config.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 10 additions & 10 deletions
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

Lines changed: 10 additions & 10 deletions
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

Lines changed: 9 additions & 9 deletions
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

0 commit comments

Comments
 (0)