]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_bool.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / transmute / transmute_int_to_bool.rs
1 use super::TRANSMUTE_INT_TO_BOOL;
2 use clippy_utils::diagnostics::span_lint_and_then;
3 use clippy_utils::sugg;
4 use rustc_ast as ast;
5 use rustc_errors::Applicability;
6 use rustc_hir::Expr;
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::{self, 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 arg: &'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 `{from_ty}` to a `bool`"),
27 |diag| {
28 let arg = sugg::Sugg::hir(cx, arg, "..");
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 }