about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYusuke Tanaka <yusuktan@maguro.dev>2021-02-11 00:55:32 +0900
committerflip1995 <philipp.krones@embecosm.com>2021-03-02 10:40:24 +0100
commitd04ea41d1fbf8c60115bf0746fd9756ff17183c2 (patch)
tree27a80447e4da8b8075eaf968bc9750118a90ff2a
parentf8bc0e249c532ceacc9897ea08f7b97ecba5408e (diff)
downloadrust-d04ea41d1fbf8c60115bf0746fd9756ff17183c2.tar.gz
rust-d04ea41d1fbf8c60115bf0746fd9756ff17183c2.zip
Move transmute_int_to_bool to its own module
-rw-r--r--clippy_lints/src/transmute/mod.rs23
-rw-r--r--clippy_lints/src/transmute/transmute_int_to_bool.rs42
2 files changed, 47 insertions, 18 deletions
diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs
index e144c96498b..fa9edc9ae2c 100644
--- a/clippy_lints/src/transmute/mod.rs
+++ b/clippy_lints/src/transmute/mod.rs
@@ -1,4 +1,5 @@
 mod crosspointer_transmute;
+mod transmute_int_to_bool;
 mod transmute_int_to_char;
 mod transmute_ptr_to_ptr;
 mod transmute_ptr_to_ref;
@@ -380,26 +381,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
                 if triggered {
                     return;
                 }
+                let triggered = transmute_int_to_bool::check(cx, e, from_ty, to_ty, args);
+                if triggered {
+                    return;
+                }
 
                 match (&from_ty.kind(), &to_ty.kind()) {
-                    (ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
-                        span_lint_and_then(
-                            cx,
-                            TRANSMUTE_INT_TO_BOOL,
-                            e.span,
-                            &format!("transmute from a `{}` to a `bool`", from_ty),
-                            |diag| {
-                                let arg = sugg::Sugg::hir(cx, &args[0], "..");
-                                let zero = sugg::Sugg::NonParen(Cow::from("0"));
-                                diag.span_suggestion(
-                                    e.span,
-                                    "consider using",
-                                    sugg::make_binop(ast::BinOpKind::Ne, &arg, &zero).to_string(),
-                                    Applicability::Unspecified,
-                                );
-                            },
-                        )
-                    },
                     (ty::Int(_) | ty::Uint(_), ty::Float(_)) if !const_context => span_lint_and_then(
                         cx,
                         TRANSMUTE_INT_TO_FLOAT,
diff --git a/clippy_lints/src/transmute/transmute_int_to_bool.rs b/clippy_lints/src/transmute/transmute_int_to_bool.rs
new file mode 100644
index 00000000000..c66c7bff232
--- /dev/null
+++ b/clippy_lints/src/transmute/transmute_int_to_bool.rs
@@ -0,0 +1,42 @@
+use super::TRANSMUTE_INT_TO_BOOL;
+use crate::utils::{span_lint_and_then, sugg};
+use rustc_ast as ast;
+use rustc_errors::Applicability;
+use rustc_hir::Expr;
+use rustc_lint::LateContext;
+use rustc_middle::ty;
+use rustc_middle::ty::Ty;
+use std::borrow::Cow;
+
+/// Checks for `transmute_int_to_bool` lint.
+/// Returns `true` if it's triggered, otherwise returns `false`.
+pub(super) fn check<'tcx>(
+    cx: &LateContext<'tcx>,
+    e: &'tcx Expr<'_>,
+    from_ty: Ty<'tcx>,
+    to_ty: Ty<'tcx>,
+    args: &'tcx [Expr<'_>],
+) -> bool {
+    match (&from_ty.kind(), &to_ty.kind()) {
+        (ty::Int(ty::IntTy::I8) | ty::Uint(ty::UintTy::U8), ty::Bool) => {
+            span_lint_and_then(
+                cx,
+                TRANSMUTE_INT_TO_BOOL,
+                e.span,
+                &format!("transmute from a `{}` to a `bool`", from_ty),
+                |diag| {
+                    let arg = sugg::Sugg::hir(cx, &args[0], "..");
+                    let zero = sugg::Sugg::NonParen(Cow::from("0"));
+                    diag.span_suggestion(
+                        e.span,
+                        "consider using",
+                        sugg::make_binop(ast::BinOpKind::Ne, &arg, &zero).to_string(),
+                        Applicability::Unspecified,
+                    );
+                },
+            );
+            true
+        },
+        _ => false,
+    }
+}