Skip to content

Commit acedc7b

Browse files
magurotunaflip1995
authored andcommitted
Move transmute_int_to_float to its own module
1 parent d04ea41 commit acedc7b

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod crosspointer_transmute;
22
mod transmute_int_to_bool;
33
mod transmute_int_to_char;
4+
mod transmute_int_to_float;
45
mod transmute_ptr_to_ptr;
56
mod transmute_ptr_to_ref;
67
mod transmute_ref_to_ref;
@@ -385,31 +386,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
385386
if triggered {
386387
return;
387388
}
389+
let triggered = transmute_int_to_float::check(cx, e, from_ty, to_ty, args, const_context);
390+
if triggered {
391+
return;
392+
}
388393

389394
match (&from_ty.kind(), &to_ty.kind()) {
390-
(ty::Int(_) | ty::Uint(_), ty::Float(_)) if !const_context => span_lint_and_then(
391-
cx,
392-
TRANSMUTE_INT_TO_FLOAT,
393-
e.span,
394-
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
395-
|diag| {
396-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
397-
let arg = if let ty::Int(int_ty) = from_ty.kind() {
398-
arg.as_ty(format!(
399-
"u{}",
400-
int_ty.bit_width().map_or_else(|| "size".to_string(), |v| v.to_string())
401-
))
402-
} else {
403-
arg
404-
};
405-
diag.span_suggestion(
406-
e.span,
407-
"consider using",
408-
format!("{}::from_bits({})", to_ty, arg.to_string()),
409-
Applicability::Unspecified,
410-
);
411-
},
412-
),
413395
(ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) if !const_context => span_lint_and_then(
414396
cx,
415397
TRANSMUTE_FLOAT_TO_INT,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use super::TRANSMUTE_INT_TO_FLOAT;
2+
use crate::utils::{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 `transmute_int_to_float` 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+
const_context: bool,
18+
) -> bool {
19+
match (&from_ty.kind(), &to_ty.kind()) {
20+
(ty::Int(_) | ty::Uint(_), ty::Float(_)) if !const_context => {
21+
span_lint_and_then(
22+
cx,
23+
TRANSMUTE_INT_TO_FLOAT,
24+
e.span,
25+
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
26+
|diag| {
27+
let arg = sugg::Sugg::hir(cx, &args[0], "..");
28+
let arg = if let ty::Int(int_ty) = from_ty.kind() {
29+
arg.as_ty(format!(
30+
"u{}",
31+
int_ty.bit_width().map_or_else(|| "size".to_string(), |v| v.to_string())
32+
))
33+
} else {
34+
arg
35+
};
36+
diag.span_suggestion(
37+
e.span,
38+
"consider using",
39+
format!("{}::from_bits({})", to_ty, arg.to_string()),
40+
Applicability::Unspecified,
41+
);
42+
},
43+
);
44+
true
45+
},
46+
_ => false,
47+
}
48+
}

0 commit comments

Comments
 (0)