]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/transmute.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / transmute.rs
CommitLineData
ea8adc8c
XL
1use rustc::lint::*;
2use rustc::ty::{self, Ty};
3use rustc::hir::*;
abe05a73
XL
4use std::borrow::Cow;
5use syntax::ast;
2c00a5a8
XL
6use utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then,
7 alignment};
abe05a73 8use utils::{opt_def_id, sugg};
ea8adc8c
XL
9
10/// **What it does:** Checks for transmutes that can't ever be correct on any
11/// architecture.
12///
13/// **Why is this bad?** It's basically guaranteed to be undefined behaviour.
14///
15/// **Known problems:** When accessing C, users might want to store pointer
16/// sized objects in `extradata` arguments to save an allocation.
17///
18/// **Example:**
19/// ```rust
20/// let ptr: *const T = core::intrinsics::transmute('x')`
21/// ```
22declare_lint! {
23 pub WRONG_TRANSMUTE,
24 Warn,
25 "transmutes that are confusing at best, undefined behaviour at worst and always useless"
26}
27
28/// **What it does:** Checks for transmutes to the original type of the object
29/// and transmutes that could be a cast.
30///
31/// **Why is this bad?** Readability. The code tricks people into thinking that
32/// something complex is going on.
33///
34/// **Known problems:** None.
35///
36/// **Example:**
37/// ```rust
38/// core::intrinsics::transmute(t) // where the result type is the same as `t`'s
39/// ```
40declare_lint! {
41 pub USELESS_TRANSMUTE,
42 Warn,
43 "transmutes that have the same to and from types or could be a cast/coercion"
44}
45
46/// **What it does:** Checks for transmutes between a type `T` and `*T`.
47///
48/// **Why is this bad?** It's easy to mistakenly transmute between a type and a
49/// pointer to that type.
50///
51/// **Known problems:** None.
52///
53/// **Example:**
54/// ```rust
55/// core::intrinsics::transmute(t)` // where the result type is the same as
56/// `*t` or `&t`'s
57/// ```
58declare_lint! {
59 pub CROSSPOINTER_TRANSMUTE,
60 Warn,
61 "transmutes that have to or from types that are a pointer to the other"
62}
63
64/// **What it does:** Checks for transmutes from a pointer to a reference.
65///
66/// **Why is this bad?** This can always be rewritten with `&` and `*`.
67///
68/// **Known problems:** None.
69///
70/// **Example:**
71/// ```rust
72/// let _: &T = std::mem::transmute(p); // where p: *const T
73/// // can be written:
74/// let _: &T = &*p;
75/// ```
76declare_lint! {
77 pub TRANSMUTE_PTR_TO_REF,
78 Warn,
79 "transmutes from a pointer to a reference type"
80}
81
abe05a73
XL
82/// **What it does:** Checks for transmutes from an integer to a `char`.
83///
84/// **Why is this bad?** Not every integer is a Unicode scalar value.
85///
86/// **Known problems:**
87/// - [`from_u32`] which this lint suggests using is slower than `transmute`
88/// as it needs to validate the input.
89/// If you are certain that the input is always a valid Unicode scalar value,
90/// use [`from_u32_unchecked`] which is as fast as `transmute`
91/// but has a semantically meaningful name.
92/// - You might want to handle `None` returned from [`from_u32`] instead of calling `unwrap`.
93///
94/// [`from_u32`]: https://doc.rust-lang.org/std/char/fn.from_u32.html
95/// [`from_u32_unchecked`]: https://doc.rust-lang.org/std/char/fn.from_u32_unchecked.html
96///
97/// **Example:**
98/// ```rust
99/// let _: char = std::mem::transmute(x); // where x: u32
100/// // should be:
101/// let _ = std::char::from_u32(x).unwrap();
102/// ```
103declare_lint! {
104 pub TRANSMUTE_INT_TO_CHAR,
105 Warn,
106 "transmutes from an integer to a `char`"
107}
108
109/// **What it does:** Checks for transmutes from a `&[u8]` to a `&str`.
110///
111/// **Why is this bad?** Not every byte slice is a valid UTF-8 string.
112///
113/// **Known problems:**
114/// - [`from_utf8`] which this lint suggests using is slower than `transmute`
115/// as it needs to validate the input.
116/// If you are certain that the input is always a valid UTF-8,
117/// use [`from_utf8_unchecked`] which is as fast as `transmute`
118/// but has a semantically meaningful name.
119/// - You might want to handle errors returned from [`from_utf8`] instead of calling `unwrap`.
120///
121/// [`from_utf8`]: https://doc.rust-lang.org/std/str/fn.from_utf8.html
122/// [`from_utf8_unchecked`]: https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html
123///
124/// **Example:**
125/// ```rust
126/// let _: &str = std::mem::transmute(b); // where b: &[u8]
127/// // should be:
128/// let _ = std::str::from_utf8(b).unwrap();
129/// ```
130declare_lint! {
131 pub TRANSMUTE_BYTES_TO_STR,
132 Warn,
133 "transmutes from a `&[u8]` to a `&str`"
134}
135
136/// **What it does:** Checks for transmutes from an integer to a `bool`.
137///
138/// **Why is this bad?** This might result in an invalid in-memory representation of a `bool`.
139///
140/// **Known problems:** None.
141///
142/// **Example:**
143/// ```rust
144/// let _: bool = std::mem::transmute(x); // where x: u8
145/// // should be:
146/// let _: bool = x != 0;
147/// ```
148declare_lint! {
149 pub TRANSMUTE_INT_TO_BOOL,
150 Warn,
151 "transmutes from an integer to a `bool`"
152}
153
154/// **What it does:** Checks for transmutes from an integer to a float.
155///
156/// **Why is this bad?** This might result in an invalid in-memory representation of a float.
157///
158/// **Known problems:** None.
159///
160/// **Example:**
161/// ```rust
162/// let _: f32 = std::mem::transmute(x); // where x: u32
163/// // should be:
164/// let _: f32 = f32::from_bits(x);
165/// ```
166declare_lint! {
167 pub TRANSMUTE_INT_TO_FLOAT,
168 Warn,
169 "transmutes from an integer to a float"
170}
171
2c00a5a8
XL
172/// **What it does:** Checks for transmutes to a potentially less-aligned type.
173///
174/// **Why is this bad?** This might result in undefined behavior.
175///
176/// **Known problems:** None.
177///
178/// **Example:**
179/// ```rust
180/// // u32 is 32-bit aligned; u8 is 8-bit aligned
181/// let _: u32 = unsafe { std::mem::transmute([0u8; 4]) };
182/// ```
183declare_lint! {
184 pub MISALIGNED_TRANSMUTE,
185 Warn,
186 "transmutes to a potentially less-aligned type"
187}
188
ea8adc8c
XL
189pub struct Transmute;
190
191impl LintPass for Transmute {
192 fn get_lints(&self) -> LintArray {
abe05a73
XL
193 lint_array!(
194 CROSSPOINTER_TRANSMUTE,
195 TRANSMUTE_PTR_TO_REF,
196 USELESS_TRANSMUTE,
197 WRONG_TRANSMUTE,
198 TRANSMUTE_INT_TO_CHAR,
199 TRANSMUTE_BYTES_TO_STR,
200 TRANSMUTE_INT_TO_BOOL,
2c00a5a8
XL
201 TRANSMUTE_INT_TO_FLOAT,
202 MISALIGNED_TRANSMUTE
abe05a73 203 )
ea8adc8c
XL
204 }
205}
206
207impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
208 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
209 if let ExprCall(ref path_expr, ref args) = e.node {
210 if let ExprPath(ref qpath) = path_expr.node {
abe05a73
XL
211 if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path_expr.hir_id)) {
212 if match_def_path(cx.tcx, def_id, &paths::TRANSMUTE) {
213 let from_ty = cx.tables.expr_ty(&args[0]);
214 let to_ty = cx.tables.expr_ty(e);
ea8adc8c 215
abe05a73
XL
216 match (&from_ty.sty, &to_ty.sty) {
217 _ if from_ty == to_ty => span_lint(
ea8adc8c
XL
218 cx,
219 USELESS_TRANSMUTE,
220 e.span,
221 &format!("transmute from a type (`{}`) to itself", from_ty),
abe05a73 222 ),
2c00a5a8
XL
223 _ if alignment(cx, from_ty).map(|a| a.abi())
224 < alignment(cx, to_ty).map(|a| a.abi())
225 => span_lint(
226 cx,
227 MISALIGNED_TRANSMUTE,
228 e.span,
229 &format!(
230 "transmute from `{}` to a less-aligned type (`{}`)",
231 from_ty,
232 to_ty,
233 )
234 ),
abe05a73 235 (&ty::TyRef(_, rty), &ty::TyRawPtr(ptr_ty)) => span_lint_and_then(
ea8adc8c
XL
236 cx,
237 USELESS_TRANSMUTE,
238 e.span,
239 "transmute from a reference to a pointer",
240 |db| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
241 let sugg = if ptr_ty == rty {
242 arg.as_ty(to_ty)
243 } else {
244 arg.as_ty(cx.tcx.mk_ptr(rty)).as_ty(to_ty)
245 };
246
247 db.span_suggestion(e.span, "try", sugg.to_string());
248 },
abe05a73
XL
249 ),
250 (&ty::TyInt(_), &ty::TyRawPtr(_)) | (&ty::TyUint(_), &ty::TyRawPtr(_)) => {
251 span_lint_and_then(
252 cx,
253 USELESS_TRANSMUTE,
254 e.span,
255 "transmute from an integer to a pointer",
256 |db| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
257 db.span_suggestion(e.span, "try", arg.as_ty(&to_ty.to_string()).to_string());
258 },
259 )
260 },
261 (&ty::TyFloat(_), &ty::TyRef(..)) |
262 (&ty::TyFloat(_), &ty::TyRawPtr(_)) |
263 (&ty::TyChar, &ty::TyRef(..)) |
264 (&ty::TyChar, &ty::TyRawPtr(_)) => span_lint(
ea8adc8c
XL
265 cx,
266 WRONG_TRANSMUTE,
267 e.span,
268 &format!("transmute from a `{}` to a pointer", from_ty),
abe05a73
XL
269 ),
270 (&ty::TyRawPtr(from_ptr), _) if from_ptr.ty == to_ty => span_lint(
ea8adc8c
XL
271 cx,
272 CROSSPOINTER_TRANSMUTE,
273 e.span,
274 &format!(
275 "transmute from a type (`{}`) to the type that it points to (`{}`)",
276 from_ty,
277 to_ty
278 ),
abe05a73
XL
279 ),
280 (_, &ty::TyRawPtr(to_ptr)) if to_ptr.ty == from_ty => span_lint(
ea8adc8c
XL
281 cx,
282 CROSSPOINTER_TRANSMUTE,
283 e.span,
abe05a73
XL
284 &format!(
285 "transmute from a type (`{}`) to a pointer to that type (`{}`)",
286 from_ty,
287 to_ty
288 ),
289 ),
290 (&ty::TyRawPtr(from_pty), &ty::TyRef(_, to_ref_ty)) => span_lint_and_then(
ea8adc8c
XL
291 cx,
292 TRANSMUTE_PTR_TO_REF,
293 e.span,
294 &format!(
295 "transmute from a pointer type (`{}`) to a reference type \
abe05a73 296 (`{}`)",
ea8adc8c
XL
297 from_ty,
298 to_ty
299 ),
300 |db| {
301 let arg = sugg::Sugg::hir(cx, &args[0], "..");
abe05a73 302 let (deref, cast) = if to_ref_ty.mutbl == Mutability::MutMutable {
ea8adc8c
XL
303 ("&mut *", "*mut")
304 } else {
305 ("&*", "*const")
306 };
307
abe05a73 308 let arg = if from_pty.ty == to_ref_ty.ty {
ea8adc8c
XL
309 arg
310 } else {
abe05a73 311 arg.as_ty(&format!("{} {}", cast, get_type_snippet(cx, qpath, to_ref_ty.ty)))
ea8adc8c
XL
312 };
313
314 db.span_suggestion(e.span, "try", sugg::make_unop(deref, arg).to_string());
315 },
abe05a73
XL
316 ),
317 (&ty::TyInt(ast::IntTy::I32), &ty::TyChar) |
318 (&ty::TyUint(ast::UintTy::U32), &ty::TyChar) => span_lint_and_then(
319 cx,
320 TRANSMUTE_INT_TO_CHAR,
321 e.span,
322 &format!("transmute from a `{}` to a `char`", from_ty),
323 |db| {
324 let arg = sugg::Sugg::hir(cx, &args[0], "..");
325 let arg = if let ty::TyInt(_) = from_ty.sty {
326 arg.as_ty(ty::TyUint(ast::UintTy::U32))
327 } else {
328 arg
329 };
330 db.span_suggestion(
331 e.span,
332 "consider using",
333 format!("std::char::from_u32({}).unwrap()", arg.to_string()),
334 );
335 },
336 ),
337 (&ty::TyRef(_, ref ref_from), &ty::TyRef(_, ref ref_to)) => {
338 if_chain! {
339 if let (&ty::TySlice(slice_ty), &ty::TyStr) = (&ref_from.ty.sty, &ref_to.ty.sty);
340 if let ty::TyUint(ast::UintTy::U8) = slice_ty.sty;
341 if ref_from.mutbl == ref_to.mutbl;
342 then {
343 let postfix = if ref_from.mutbl == Mutability::MutMutable {
344 "_mut"
345 } else {
346 ""
347 };
348
349 span_lint_and_then(
350 cx,
351 TRANSMUTE_BYTES_TO_STR,
352 e.span,
353 &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
354 |db| {
355 db.span_suggestion(
356 e.span,
357 "consider using",
358 format!(
359 "std::str::from_utf8{}({}).unwrap()",
360 postfix,
361 snippet(cx, args[0].span, ".."),
362 ),
363 );
364 }
365 )
366 }
367 }
368 },
369 (&ty::TyInt(ast::IntTy::I8), &ty::TyBool) | (&ty::TyUint(ast::UintTy::U8), &ty::TyBool) => {
370 span_lint_and_then(
371 cx,
372 TRANSMUTE_INT_TO_BOOL,
373 e.span,
374 &format!("transmute from a `{}` to a `bool`", from_ty),
375 |db| {
376 let arg = sugg::Sugg::hir(cx, &args[0], "..");
377 let zero = sugg::Sugg::NonParen(Cow::from("0"));
378 db.span_suggestion(
379 e.span,
380 "consider using",
381 sugg::make_binop(ast::BinOpKind::Ne, &arg, &zero).to_string(),
382 );
383 },
384 )
385 },
386 (&ty::TyInt(_), &ty::TyFloat(_)) | (&ty::TyUint(_), &ty::TyFloat(_)) => {
387 span_lint_and_then(
388 cx,
389 TRANSMUTE_INT_TO_FLOAT,
390 e.span,
391 &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
392 |db| {
393 let arg = sugg::Sugg::hir(cx, &args[0], "..");
394 let arg = if let ty::TyInt(int_ty) = from_ty.sty {
395 arg.as_ty(format!(
396 "u{}",
397 int_ty
398 .bit_width()
399 .map_or_else(|| "size".to_string(), |v| v.to_string())
400 ))
401 } else {
402 arg
403 };
404 db.span_suggestion(
405 e.span,
406 "consider using",
407 format!("{}::from_bits({})", to_ty, arg.to_string()),
408 );
409 },
410 )
411 },
412 _ => return,
413 };
414 }
ea8adc8c
XL
415 }
416 }
417 }
418 }
419}
420
421/// Get the snippet of `Bar` in `…::transmute<Foo, &Bar>`. If that snippet is
422/// not available , use
423/// the type's `ToString` implementation. In weird cases it could lead to types
424/// with invalid `'_`
425/// lifetime, but it should be rare.
abe05a73 426fn get_type_snippet(cx: &LateContext, path: &QPath, to_ref_ty: Ty) -> String {
ea8adc8c 427 let seg = last_path_segment(path);
abe05a73
XL
428 if_chain! {
429 if let Some(ref params) = seg.parameters;
430 if !params.parenthesized;
431 if let Some(to_ty) = params.types.get(1);
432 if let TyRptr(_, ref to_ty) = to_ty.node;
433 then {
434 return snippet(cx, to_ty.ty.span, &to_ref_ty.to_string()).to_string();
435 }
436 }
437
438 to_ref_ty.to_string()
ea8adc8c 439}