mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-06 03:52:53 +00:00
Move fn_to_numeric_cast to its own module
This commit is contained in:
parent
0dce9921fd
commit
458f6d19d0
37
clippy_lints/src/casts/fn_to_numeric_cast.rs
Normal file
37
clippy_lints/src/casts/fn_to_numeric_cast.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::Expr;
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, Ty, UintTy};
|
||||
|
||||
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
|
||||
|
||||
use super::{utils, FN_TO_NUMERIC_CAST};
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
|
||||
// We only want to check casts to `ty::Uint` or `ty::Int`
|
||||
match cast_to.kind() {
|
||||
ty::Uint(_) | ty::Int(..) => { /* continue on */ },
|
||||
_ => return,
|
||||
}
|
||||
|
||||
match cast_from.kind() {
|
||||
ty::FnDef(..) | ty::FnPtr(_) => {
|
||||
let mut applicability = Applicability::MaybeIncorrect;
|
||||
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
|
||||
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
|
||||
|
||||
if (to_nbits >= cx.tcx.data_layout.pointer_size.bits()) && (*cast_to.kind() != ty::Uint(UintTy::Usize)) {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
FN_TO_NUMERIC_CAST,
|
||||
expr.span,
|
||||
&format!("casting function pointer `{}` to `{}`", from_snippet, cast_to),
|
||||
"try",
|
||||
format!("{} as usize", from_snippet),
|
||||
applicability,
|
||||
);
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ mod cast_possible_truncation;
|
||||
mod cast_possible_wrap;
|
||||
mod cast_precision_loss;
|
||||
mod cast_sign_loss;
|
||||
mod fn_to_numeric_cast;
|
||||
mod unnecessary_cast;
|
||||
mod utils;
|
||||
|
||||
@ -301,6 +302,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
|
||||
if unnecessary_cast::check(cx, expr, cast_expr, cast_from, cast_to) {
|
||||
return;
|
||||
}
|
||||
|
||||
fn_to_numeric_cast::check(cx, expr, cast_expr, cast_from, cast_to);
|
||||
lint_fn_to_numeric_cast(cx, expr, cast_expr, cast_from, cast_to);
|
||||
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
|
||||
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
|
||||
@ -386,16 +389,6 @@ fn lint_fn_to_numeric_cast(
|
||||
format!("{} as usize", from_snippet),
|
||||
applicability,
|
||||
);
|
||||
} else if *cast_to.kind() != ty::Uint(UintTy::Usize) {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
FN_TO_NUMERIC_CAST,
|
||||
expr.span,
|
||||
&format!("casting function pointer `{}` to `{}`", from_snippet, cast_to),
|
||||
"try",
|
||||
format!("{} as usize", from_snippet),
|
||||
applicability,
|
||||
);
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
|
Loading…
Reference in New Issue
Block a user