move char_lit_as_u8 into the if
allows reusing `cast_from_expr` and `cast_to`
This commit is contained in:
parent
ff2b5e66a1
commit
e6f3cb00a7
2 changed files with 6 additions and 7 deletions
|
|
@ -4,18 +4,17 @@ use rustc_ast::LitKind;
|
|||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, ExprKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, UintTy};
|
||||
use rustc_middle::ty::{self, Ty, UintTy};
|
||||
|
||||
use super::CHAR_LIT_AS_U8;
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||
if let ExprKind::Cast(e, _) = &expr.kind
|
||||
&& let ExprKind::Lit(l) = &e.kind
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from_expr: &Expr<'_>, cast_to: Ty<'_>) {
|
||||
if let ExprKind::Lit(l) = &cast_from_expr.kind
|
||||
&& let LitKind::Char(c) = l.node
|
||||
&& ty::Uint(UintTy::U8) == *cx.typeck_results().expr_ty(expr).kind()
|
||||
&& ty::Uint(UintTy::U8) == *cast_to.kind()
|
||||
{
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
let snippet = snippet_with_applicability(cx, e.span, "'x'", &mut applicability);
|
||||
let snippet = snippet_with_applicability(cx, cast_from_expr.span, "'x'", &mut applicability);
|
||||
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
|
|
|
|||
|
|
@ -871,6 +871,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
|
|||
if !expr.span.from_expansion() && unnecessary_cast::check(cx, expr, cast_from_expr, cast_from, cast_to) {
|
||||
return;
|
||||
}
|
||||
char_lit_as_u8::check(cx, expr, cast_from_expr, cast_to);
|
||||
cast_slice_from_raw_parts::check(cx, expr, cast_from_expr, cast_to, self.msrv);
|
||||
ptr_cast_constness::check(cx, expr, cast_from_expr, cast_from, cast_to, self.msrv);
|
||||
as_ptr_cast_mut::check(cx, expr, cast_from_expr, cast_to);
|
||||
|
|
@ -911,7 +912,6 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
|
|||
borrow_as_ptr::check_implicit_cast(cx, expr);
|
||||
}
|
||||
cast_ptr_alignment::check(cx, expr);
|
||||
char_lit_as_u8::check(cx, expr);
|
||||
ptr_as_ptr::check(cx, expr, self.msrv);
|
||||
cast_slice_different_sizes::check(cx, expr, self.msrv);
|
||||
ptr_cast_constness::check_null_ptr_cast_method(cx, expr);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue