Skip to content

Commit cc87ae8

Browse files
authored
Rollup merge of #140431 - bend-n:dont_handle_bool_transmute, r=Nadrieril
dont handle bool transmute removes `transmute(u8) -> bool` suggestion due to ambiguity, leave it for clippy elaboration on ambiguity in question: `transmute::<u8, bool>(x)` will codegen to an `assume(u8 < 2)`; `_ == 1` or `_ != 0` or `_ % 2 == 0` would remove that assumption `match _ { x @ (0 | 1) => x == 1, _ => std::hint::unreachable_unchecked() }` is very verbose `@rustbot` label L-unnecessary_transmutes
2 parents 5df0f72 + de8e864 commit cc87ae8

File tree

4 files changed

+7
-14
lines changed

4 files changed

+7
-14
lines changed

compiler/rustc_mir_transform/src/check_unnecessary_transmutes.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::errors::UnnecessaryTransmute as Error;
99

1010
/// Check for transmutes that overlap with stdlib methods.
1111
/// For example, transmuting `[u8; 4]` to `u32`.
12+
/// We chose not to lint u8 -> bool transmutes, see #140431
1213
pub(super) struct CheckUnnecessaryTransmutes;
1314

1415
impl<'tcx> crate::MirLint<'tcx> for CheckUnnecessaryTransmutes {
@@ -98,8 +99,6 @@ impl<'a, 'tcx> UnnecessaryTransmuteChecker<'a, 'tcx> {
9899
(Uint(_), Float(ty)) => err(format!("{}::from_bits({arg})", ty.name_str())),
99100
// bool → { x8 }
100101
(Bool, Int(..) | Uint(..)) => err(format!("({arg}) as {}", fn_sig.output())),
101-
// u8 → bool
102-
(Uint(_), Bool) => err(format!("({arg} == 1)")),
103102
_ => return None,
104103
})
105104
}

tests/ui/transmute/unnecessary-transmutation.fixed

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ fn main() {
8181
let y: i64 = f64::to_bits(1f64).cast_signed();
8282
//~^ ERROR
8383

84-
let z: bool = (1u8 == 1);
85-
//~^ ERROR
84+
let z: bool = transmute(1u8);
85+
// clippy
8686
let z: u8 = (z) as u8;
8787
//~^ ERROR
8888

8989
let z: bool = transmute(1i8);
90-
// no error!
90+
// clippy
9191
let z: i8 = (z) as i8;
9292
//~^ ERROR
9393
}

tests/ui/transmute/unnecessary-transmutation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ fn main() {
8282
//~^ ERROR
8383

8484
let z: bool = transmute(1u8);
85-
//~^ ERROR
85+
// clippy
8686
let z: u8 = transmute(z);
8787
//~^ ERROR
8888

8989
let z: bool = transmute(1i8);
90-
// no error!
90+
// clippy
9191
let z: i8 = transmute(z);
9292
//~^ ERROR
9393
}

tests/ui/transmute/unnecessary-transmutation.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,6 @@ error: unnecessary transmute
239239
LL | let y: i64 = transmute(1f64);
240240
| ^^^^^^^^^^^^^^^ help: replace this with: `f64::to_bits(1f64).cast_signed()`
241241

242-
error: unnecessary transmute
243-
--> $DIR/unnecessary-transmutation.rs:84:23
244-
|
245-
LL | let z: bool = transmute(1u8);
246-
| ^^^^^^^^^^^^^^ help: replace this with: `(1u8 == 1)`
247-
248242
error: unnecessary transmute
249243
--> $DIR/unnecessary-transmutation.rs:86:21
250244
|
@@ -257,5 +251,5 @@ error: unnecessary transmute
257251
LL | let z: i8 = transmute(z);
258252
| ^^^^^^^^^^^^ help: replace this with: `(z) as i8`
259253

260-
error: aborting due to 36 previous errors
254+
error: aborting due to 35 previous errors
261255

0 commit comments

Comments
 (0)