Skip to content

Commit db5739a

Browse files
committed
Auto merge of #8610 - SabrinaJewson:transmute-int-to-char-const, r=xFrednet
Don't warn int-to-char transmutes in const contexts changelog: Don't warn ``[`transmute_int_to_char`]`` in const contexts fixes: #8379
2 parents c0a5693 + d6f05c6 commit db5739a

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,10 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
410410
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
411411
if cx.tcx.is_diagnostic_item(sym::transmute, def_id);
412412
then {
413-
// Avoid suggesting from/to bits and dereferencing raw pointers in const contexts.
414-
// See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`.
415-
// And see https://github.com/rust-lang/rust/issues/51911 for dereferencing raw pointers.
413+
// Avoid suggesting non-const operations in const contexts:
414+
// - from/to bits (https://github.com/rust-lang/rust/issues/73736)
415+
// - dereferencing raw pointers (https://github.com/rust-lang/rust/issues/51911)
416+
// - char conversions (https://github.com/rust-lang/rust/issues/89259)
416417
let const_context = in_constant(cx, e.hir_id);
417418

418419
let from_ty = cx.typeck_results().expr_ty_adjusted(arg);
@@ -427,7 +428,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
427428
let linted = wrong_transmute::check(cx, e, from_ty, to_ty)
428429
| crosspointer_transmute::check(cx, e, from_ty, to_ty)
429430
| transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, qpath)
430-
| transmute_int_to_char::check(cx, e, from_ty, to_ty, arg)
431+
| transmute_int_to_char::check(cx, e, from_ty, to_ty, arg, const_context)
431432
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)
432433
| transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg)
433434
| transmute_int_to_bool::check(cx, e, from_ty, to_ty, arg)

clippy_lints/src/transmute/transmute_int_to_char.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ pub(super) fn check<'tcx>(
1515
from_ty: Ty<'tcx>,
1616
to_ty: Ty<'tcx>,
1717
arg: &'tcx Expr<'_>,
18+
const_context: bool,
1819
) -> bool {
1920
match (&from_ty.kind(), &to_ty.kind()) {
20-
(ty::Int(ty::IntTy::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) => {
21+
(ty::Int(ty::IntTy::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) if !const_context => {
2122
span_lint_and_then(
2223
cx,
2324
TRANSMUTE_INT_TO_CHAR,

tests/ui/transmute.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ fn crosspointer() {
7373
fn int_to_char() {
7474
let _: char = unsafe { std::mem::transmute(0_u32) };
7575
let _: char = unsafe { std::mem::transmute(0_i32) };
76+
77+
// These shouldn't warn
78+
const _: char = unsafe { std::mem::transmute(0_u32) };
79+
const _: char = unsafe { std::mem::transmute(0_i32) };
7680
}
7781

7882
#[warn(clippy::transmute_int_to_bool)]

tests/ui/transmute.stderr

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,135 +107,135 @@ LL | let _: char = unsafe { std::mem::transmute(0_i32) };
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::char::from_u32(0_i32 as u32).unwrap()`
108108

109109
error: transmute from a `u8` to a `bool`
110-
--> $DIR/transmute.rs:80:28
110+
--> $DIR/transmute.rs:84:28
111111
|
112112
LL | let _: bool = unsafe { std::mem::transmute(0_u8) };
113113
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `0_u8 != 0`
114114
|
115115
= note: `-D clippy::transmute-int-to-bool` implied by `-D warnings`
116116

117117
error: transmute from a `u32` to a `f32`
118-
--> $DIR/transmute.rs:86:31
118+
--> $DIR/transmute.rs:90:31
119119
|
120120
LL | let _: f32 = unsafe { std::mem::transmute(0_u32) };
121121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_u32)`
122122
|
123123
= note: `-D clippy::transmute-int-to-float` implied by `-D warnings`
124124

125125
error: transmute from a `i32` to a `f32`
126-
--> $DIR/transmute.rs:87:31
126+
--> $DIR/transmute.rs:91:31
127127
|
128128
LL | let _: f32 = unsafe { std::mem::transmute(0_i32) };
129129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_i32 as u32)`
130130

131131
error: transmute from a `u64` to a `f64`
132-
--> $DIR/transmute.rs:88:31
132+
--> $DIR/transmute.rs:92:31
133133
|
134134
LL | let _: f64 = unsafe { std::mem::transmute(0_u64) };
135135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f64::from_bits(0_u64)`
136136

137137
error: transmute from a `i64` to a `f64`
138-
--> $DIR/transmute.rs:89:31
138+
--> $DIR/transmute.rs:93:31
139139
|
140140
LL | let _: f64 = unsafe { std::mem::transmute(0_i64) };
141141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f64::from_bits(0_i64 as u64)`
142142

143143
error: transmute from a `u8` to a `[u8; 1]`
144-
--> $DIR/transmute.rs:109:30
144+
--> $DIR/transmute.rs:113:30
145145
|
146146
LL | let _: [u8; 1] = std::mem::transmute(0u8);
147147
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u8.to_ne_bytes()`
148148
|
149149
= note: `-D clippy::transmute-num-to-bytes` implied by `-D warnings`
150150

151151
error: transmute from a `u32` to a `[u8; 4]`
152-
--> $DIR/transmute.rs:110:30
152+
--> $DIR/transmute.rs:114:30
153153
|
154154
LL | let _: [u8; 4] = std::mem::transmute(0u32);
155155
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u32.to_ne_bytes()`
156156

157157
error: transmute from a `u128` to a `[u8; 16]`
158-
--> $DIR/transmute.rs:111:31
158+
--> $DIR/transmute.rs:115:31
159159
|
160160
LL | let _: [u8; 16] = std::mem::transmute(0u128);
161161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u128.to_ne_bytes()`
162162

163163
error: transmute from a `i8` to a `[u8; 1]`
164-
--> $DIR/transmute.rs:112:30
164+
--> $DIR/transmute.rs:116:30
165165
|
166166
LL | let _: [u8; 1] = std::mem::transmute(0i8);
167167
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i8.to_ne_bytes()`
168168

169169
error: transmute from a `i32` to a `[u8; 4]`
170-
--> $DIR/transmute.rs:113:30
170+
--> $DIR/transmute.rs:117:30
171171
|
172172
LL | let _: [u8; 4] = std::mem::transmute(0i32);
173173
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i32.to_ne_bytes()`
174174

175175
error: transmute from a `i128` to a `[u8; 16]`
176-
--> $DIR/transmute.rs:114:31
176+
--> $DIR/transmute.rs:118:31
177177
|
178178
LL | let _: [u8; 16] = std::mem::transmute(0i128);
179179
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i128.to_ne_bytes()`
180180

181181
error: transmute from a `f32` to a `[u8; 4]`
182-
--> $DIR/transmute.rs:115:30
182+
--> $DIR/transmute.rs:119:30
183183
|
184184
LL | let _: [u8; 4] = std::mem::transmute(0.0f32);
185185
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f32.to_ne_bytes()`
186186

187187
error: transmute from a `f64` to a `[u8; 8]`
188-
--> $DIR/transmute.rs:116:30
188+
--> $DIR/transmute.rs:120:30
189189
|
190190
LL | let _: [u8; 8] = std::mem::transmute(0.0f64);
191191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0.0f64.to_ne_bytes()`
192192

193193
error: transmute from a `u8` to a `[u8; 1]`
194-
--> $DIR/transmute.rs:121:30
194+
--> $DIR/transmute.rs:125:30
195195
|
196196
LL | let _: [u8; 1] = std::mem::transmute(0u8);
197197
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u8.to_ne_bytes()`
198198

199199
error: transmute from a `u32` to a `[u8; 4]`
200-
--> $DIR/transmute.rs:122:30
200+
--> $DIR/transmute.rs:126:30
201201
|
202202
LL | let _: [u8; 4] = std::mem::transmute(0u32);
203203
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u32.to_ne_bytes()`
204204

205205
error: transmute from a `u128` to a `[u8; 16]`
206-
--> $DIR/transmute.rs:123:31
206+
--> $DIR/transmute.rs:127:31
207207
|
208208
LL | let _: [u8; 16] = std::mem::transmute(0u128);
209209
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0u128.to_ne_bytes()`
210210

211211
error: transmute from a `i8` to a `[u8; 1]`
212-
--> $DIR/transmute.rs:124:30
212+
--> $DIR/transmute.rs:128:30
213213
|
214214
LL | let _: [u8; 1] = std::mem::transmute(0i8);
215215
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i8.to_ne_bytes()`
216216

217217
error: transmute from a `i32` to a `[u8; 4]`
218-
--> $DIR/transmute.rs:125:30
218+
--> $DIR/transmute.rs:129:30
219219
|
220220
LL | let _: [u8; 4] = std::mem::transmute(0i32);
221221
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i32.to_ne_bytes()`
222222

223223
error: transmute from a `i128` to a `[u8; 16]`
224-
--> $DIR/transmute.rs:126:31
224+
--> $DIR/transmute.rs:130:31
225225
|
226226
LL | let _: [u8; 16] = std::mem::transmute(0i128);
227227
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `to_ne_bytes()`: `0i128.to_ne_bytes()`
228228

229229
error: transmute from a `&[u8]` to a `&str`
230-
--> $DIR/transmute.rs:134:28
230+
--> $DIR/transmute.rs:138:28
231231
|
232232
LL | let _: &str = unsafe { std::mem::transmute(b) };
233233
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(b).unwrap()`
234234
|
235235
= note: `-D clippy::transmute-bytes-to-str` implied by `-D warnings`
236236

237237
error: transmute from a `&mut [u8]` to a `&mut str`
238-
--> $DIR/transmute.rs:135:32
238+
--> $DIR/transmute.rs:139:32
239239
|
240240
LL | let _: &mut str = unsafe { std::mem::transmute(mb) };
241241
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`

0 commit comments

Comments
 (0)