Skip to content

Commit 2451781

Browse files
committed
Move useless_transmute to its own module
1 parent f30f382 commit 2451781

File tree

2 files changed

+81
-44
lines changed

2 files changed

+81
-44
lines changed

clippy_lints/src/transmute/mod.rs

+7-44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
mod useless_transmute;
12
mod utils;
3+
24
use utils::*;
35

46
use crate::utils::{
@@ -344,51 +346,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
344346
let from_ty = cx.typeck_results().expr_ty(&args[0]);
345347
let to_ty = cx.typeck_results().expr_ty(e);
346348

347-
match (&from_ty.kind(), &to_ty.kind()) {
348-
_ if from_ty == to_ty => span_lint(
349-
cx,
350-
USELESS_TRANSMUTE,
351-
e.span,
352-
&format!("transmute from a type (`{}`) to itself", from_ty),
353-
),
354-
(ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => span_lint_and_then(
355-
cx,
356-
USELESS_TRANSMUTE,
357-
e.span,
358-
"transmute from a reference to a pointer",
359-
|diag| {
360-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
361-
let rty_and_mut = ty::TypeAndMut {
362-
ty: rty,
363-
mutbl: *rty_mutbl,
364-
};
365-
366-
let sugg = if *ptr_ty == rty_and_mut {
367-
arg.as_ty(to_ty)
368-
} else {
369-
arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
370-
};
349+
let triggered = useless_transmute::check(cx, e, from_ty, to_ty, args);
350+
if triggered {
351+
return;
352+
}
371353

372-
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
373-
}
374-
},
375-
),
376-
(ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => span_lint_and_then(
377-
cx,
378-
USELESS_TRANSMUTE,
379-
e.span,
380-
"transmute from an integer to a pointer",
381-
|diag| {
382-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
383-
diag.span_suggestion(
384-
e.span,
385-
"try",
386-
arg.as_ty(&to_ty.to_string()).to_string(),
387-
Applicability::Unspecified,
388-
);
389-
}
390-
},
391-
),
354+
match (&from_ty.kind(), &to_ty.kind()) {
392355
(ty::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_)) => span_lint(
393356
cx,
394357
WRONG_TRANSMUTE,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use super::USELESS_TRANSMUTE;
2+
use crate::utils::{span_lint, span_lint_and_then, sugg};
3+
use rustc_errors::Applicability;
4+
use rustc_hir::Expr;
5+
use rustc_lint::LateContext;
6+
use rustc_middle::ty;
7+
use rustc_middle::ty::Ty;
8+
9+
/// Checks for `useless_transmute` lint.
10+
/// Returns `true` if it's triggered, otherwise returns `false`.
11+
pub(super) fn check<'tcx>(
12+
cx: &LateContext<'tcx>,
13+
e: &'tcx Expr<'_>,
14+
from_ty: Ty<'tcx>,
15+
to_ty: Ty<'tcx>,
16+
args: &'tcx [Expr<'_>],
17+
) -> bool {
18+
match (&from_ty.kind(), &to_ty.kind()) {
19+
_ if from_ty == to_ty => {
20+
span_lint(
21+
cx,
22+
USELESS_TRANSMUTE,
23+
e.span,
24+
&format!("transmute from a type (`{}`) to itself", from_ty),
25+
);
26+
true
27+
},
28+
(ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => {
29+
span_lint_and_then(
30+
cx,
31+
USELESS_TRANSMUTE,
32+
e.span,
33+
"transmute from a reference to a pointer",
34+
|diag| {
35+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
36+
let rty_and_mut = ty::TypeAndMut {
37+
ty: rty,
38+
mutbl: *rty_mutbl,
39+
};
40+
41+
let sugg = if *ptr_ty == rty_and_mut {
42+
arg.as_ty(to_ty)
43+
} else {
44+
arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
45+
};
46+
47+
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
48+
}
49+
},
50+
);
51+
true
52+
},
53+
(ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => {
54+
span_lint_and_then(
55+
cx,
56+
USELESS_TRANSMUTE,
57+
e.span,
58+
"transmute from an integer to a pointer",
59+
|diag| {
60+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
61+
diag.span_suggestion(
62+
e.span,
63+
"try",
64+
arg.as_ty(&to_ty.to_string()).to_string(),
65+
Applicability::Unspecified,
66+
);
67+
}
68+
},
69+
);
70+
true
71+
},
72+
_ => false,
73+
}
74+
}

0 commit comments

Comments
 (0)