mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
Move transmute_int_to_char to its own module
This commit is contained in:
parent
6442d45d3a
commit
4c65221188
2 changed files with 52 additions and 22 deletions
|
@ -1,4 +1,5 @@
|
|||
mod crosspointer_transmute;
|
||||
mod transmute_int_to_char;
|
||||
mod transmute_ptr_to_ref;
|
||||
mod useless_transmute;
|
||||
mod utils;
|
||||
|
@ -365,30 +366,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
|
|||
if triggered {
|
||||
return;
|
||||
}
|
||||
let triggered = transmute_int_to_char::check(cx, e, from_ty, to_ty, args);
|
||||
if triggered {
|
||||
return;
|
||||
}
|
||||
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
(ty::Int(ty::IntTy::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) => {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
TRANSMUTE_INT_TO_CHAR,
|
||||
e.span,
|
||||
&format!("transmute from a `{}` to a `char`", from_ty),
|
||||
|diag| {
|
||||
let arg = sugg::Sugg::hir(cx, &args[0], "..");
|
||||
let arg = if let ty::Int(_) = from_ty.kind() {
|
||||
arg.as_ty(ast::UintTy::U32.name_str())
|
||||
} else {
|
||||
arg
|
||||
};
|
||||
diag.span_suggestion(
|
||||
e.span,
|
||||
"consider using",
|
||||
format!("std::char::from_u32({}).unwrap()", arg.to_string()),
|
||||
Applicability::Unspecified,
|
||||
);
|
||||
},
|
||||
)
|
||||
},
|
||||
(ty::Ref(_, ty_from, from_mutbl), ty::Ref(_, ty_to, to_mutbl)) => {
|
||||
if_chain! {
|
||||
if let (&ty::Slice(slice_ty), &ty::Str) = (&ty_from.kind(), &ty_to.kind());
|
||||
|
|
47
clippy_lints/src/transmute/transmute_int_to_char.rs
Normal file
47
clippy_lints/src/transmute/transmute_int_to_char.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use super::TRANSMUTE_INT_TO_CHAR;
|
||||
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;
|
||||
|
||||
/// Checks for `transmute_int_to_char` 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::I32) | ty::Uint(ty::UintTy::U32), &ty::Char) => {
|
||||
{
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
TRANSMUTE_INT_TO_CHAR,
|
||||
e.span,
|
||||
&format!("transmute from a `{}` to a `char`", from_ty),
|
||||
|diag| {
|
||||
let arg = sugg::Sugg::hir(cx, &args[0], "..");
|
||||
let arg = if let ty::Int(_) = from_ty.kind() {
|
||||
arg.as_ty(ast::UintTy::U32.name_str())
|
||||
} else {
|
||||
arg
|
||||
};
|
||||
diag.span_suggestion(
|
||||
e.span,
|
||||
"consider using",
|
||||
format!("std::char::from_u32({}).unwrap()", arg.to_string()),
|
||||
Applicability::Unspecified,
|
||||
);
|
||||
},
|
||||
)
|
||||
};
|
||||
true
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue