Skip to content

Commit e6f3cb0

Browse files
committed
move char_lit_as_u8 into the if
allows reusing `cast_from_expr` and `cast_to`
1 parent ff2b5e6 commit e6f3cb0

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

clippy_lints/src/casts/char_lit_as_u8.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ use rustc_ast::LitKind;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::LateContext;
7-
use rustc_middle::ty::{self, UintTy};
7+
use rustc_middle::ty::{self, Ty, UintTy};
88

99
use super::CHAR_LIT_AS_U8;
1010

11-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
12-
if let ExprKind::Cast(e, _) = &expr.kind
13-
&& let ExprKind::Lit(l) = &e.kind
11+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from_expr: &Expr<'_>, cast_to: Ty<'_>) {
12+
if let ExprKind::Lit(l) = &cast_from_expr.kind
1413
&& let LitKind::Char(c) = l.node
15-
&& ty::Uint(UintTy::U8) == *cx.typeck_results().expr_ty(expr).kind()
14+
&& ty::Uint(UintTy::U8) == *cast_to.kind()
1615
{
1716
let mut applicability = Applicability::MachineApplicable;
18-
let snippet = snippet_with_applicability(cx, e.span, "'x'", &mut applicability);
17+
let snippet = snippet_with_applicability(cx, cast_from_expr.span, "'x'", &mut applicability);
1918

2019
span_lint_and_then(
2120
cx,

clippy_lints/src/casts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
871871
if !expr.span.from_expansion() && unnecessary_cast::check(cx, expr, cast_from_expr, cast_from, cast_to) {
872872
return;
873873
}
874+
char_lit_as_u8::check(cx, expr, cast_from_expr, cast_to);
874875
cast_slice_from_raw_parts::check(cx, expr, cast_from_expr, cast_to, self.msrv);
875876
ptr_cast_constness::check(cx, expr, cast_from_expr, cast_from, cast_to, self.msrv);
876877
as_ptr_cast_mut::check(cx, expr, cast_from_expr, cast_to);
@@ -911,7 +912,6 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
911912
borrow_as_ptr::check_implicit_cast(cx, expr);
912913
}
913914
cast_ptr_alignment::check(cx, expr);
914-
char_lit_as_u8::check(cx, expr);
915915
ptr_as_ptr::check(cx, expr, self.msrv);
916916
cast_slice_different_sizes::check(cx, expr, self.msrv);
917917
ptr_cast_constness::check_null_ptr_cast_method(cx, expr);

0 commit comments

Comments
 (0)