Skip to content

Commit d04ea41

Browse files
magurotunaflip1995
authored andcommitted
Move transmute_int_to_bool to its own module
1 parent f8bc0e2 commit d04ea41

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod crosspointer_transmute;
2+
mod transmute_int_to_bool;
23
mod transmute_int_to_char;
34
mod transmute_ptr_to_ptr;
45
mod transmute_ptr_to_ref;
@@ -380,26 +381,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
380381
if triggered {
381382
return;
382383
}
384+
let triggered = transmute_int_to_bool::check(cx, e, from_ty, to_ty, args);
385+
if triggered {
386+
return;
387+
}
383388

384389
match (&from_ty.kind(), &to_ty.kind()) {
385-
(ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
386-
span_lint_and_then(
387-
cx,
388-
TRANSMUTE_INT_TO_BOOL,
389-
e.span,
390-
&format!("transmute from a `{}` to a `bool`", from_ty),
391-
|diag| {
392-
let arg = sugg::Sugg::hir(cx, &args[0], "..");
393-
let zero = sugg::Sugg::NonParen(Cow::from("0"));
394-
diag.span_suggestion(
395-
e.span,
396-
"consider using",
397-
sugg::make_binop(ast::BinOpKind::Ne, &arg, &zero).to_string(),
398-
Applicability::Unspecified,
399-
);
400-
},
401-
)
402-
},
403390
(ty::Int(_) | ty::Uint(_), ty::Float(_)) if !const_context => span_lint_and_then(
404391
cx,
405392
TRANSMUTE_INT_TO_FLOAT,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use super::TRANSMUTE_INT_TO_BOOL;
2+
use crate::utils::{span_lint_and_then, sugg};
3+
use rustc_ast as ast;
4+
use rustc_errors::Applicability;
5+
use rustc_hir::Expr;
6+
use rustc_lint::LateContext;
7+
use rustc_middle::ty;
8+
use rustc_middle::ty::Ty;
9+
use std::borrow::Cow;
10+
11+
/// Checks for `transmute_int_to_bool` lint.
12+
/// Returns `true` if it's triggered, otherwise returns `false`.
13+
pub(super) fn check<'tcx>(
14+
cx: &LateContext<'tcx>,
15+
e: &'tcx Expr<'_>,
16+
from_ty: Ty<'tcx>,
17+
to_ty: Ty<'tcx>,
18+
args: &'tcx [Expr<'_>],
19+
) -> bool {
20+
match (&from_ty.kind(), &to_ty.kind()) {
21+
(ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
22+
span_lint_and_then(
23+
cx,
24+
TRANSMUTE_INT_TO_BOOL,
25+
e.span,
26+
&format!("transmute from a `{}` to a `bool`", from_ty),
27+
|diag| {
28+
let arg = sugg::Sugg::hir(cx, &args[0], "..");
29+
let zero = sugg::Sugg::NonParen(Cow::from("0"));
30+
diag.span_suggestion(
31+
e.span,
32+
"consider using",
33+
sugg::make_binop(ast::BinOpKind::Ne, &arg, &zero).to_string(),
34+
Applicability::Unspecified,
35+
);
36+
},
37+
);
38+
true
39+
},
40+
_ => false,
41+
}
42+
}

0 commit comments

Comments
 (0)