Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move transmute_int_to_bool to its own module
- Loading branch information
1 parent
f8bc0e2
commit d04ea41
Showing
2 changed files
with
47 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
} |