diff --git a/clippy_lints/src/casts/char_lit_as_u8.rs b/clippy_lints/src/casts/char_lit_as_u8.rs index a7d3868f76c6..964eaf2a0a2d 100644 --- a/clippy_lints/src/casts/char_lit_as_u8.rs +++ b/clippy_lints/src/casts/char_lit_as_u8.rs @@ -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, diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs index dcc439a272cf..e25df9dd249a 100644 --- a/clippy_lints/src/casts/mod.rs +++ b/clippy_lints/src/casts/mod.rs @@ -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); diff --git a/tests/ui/char_lit_as_u8_suggestions.fixed b/tests/ui/char_lit_as_u8.fixed similarity index 100% rename from tests/ui/char_lit_as_u8_suggestions.fixed rename to tests/ui/char_lit_as_u8.fixed diff --git a/tests/ui/char_lit_as_u8.rs b/tests/ui/char_lit_as_u8.rs index c8774c7f3091..a8f39e27605e 100644 --- a/tests/ui/char_lit_as_u8.rs +++ b/tests/ui/char_lit_as_u8.rs @@ -1,7 +1,12 @@ #![warn(clippy::char_lit_as_u8)] fn main() { - // no suggestion, since a byte literal won't work. - let _ = '❤' as u8; + let _ = 'a' as u8; + //~^ char_lit_as_u8 + let _ = '\n' as u8; + //~^ char_lit_as_u8 + let _ = '\0' as u8; + //~^ char_lit_as_u8 + let _ = '\x01' as u8; //~^ char_lit_as_u8 } diff --git a/tests/ui/char_lit_as_u8.stderr b/tests/ui/char_lit_as_u8.stderr index ec02f1341c0a..9bcded7b0ff4 100644 --- a/tests/ui/char_lit_as_u8.stderr +++ b/tests/ui/char_lit_as_u8.stderr @@ -1,12 +1,36 @@ error: casting a character literal to `u8` truncates - --> tests/ui/char_lit_as_u8.rs:5:13 + --> tests/ui/char_lit_as_u8.rs:4:13 | -LL | let _ = '❤' as u8; - | ^^^^^^^^^ +LL | let _ = 'a' as u8; + | ^^^^^^^^^ help: use a byte literal instead: `b'a'` | = note: `char` is four bytes wide, but `u8` is a single byte = note: `-D clippy::char-lit-as-u8` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::char_lit_as_u8)]` -error: aborting due to 1 previous error +error: casting a character literal to `u8` truncates + --> tests/ui/char_lit_as_u8.rs:6:13 + | +LL | let _ = '\n' as u8; + | ^^^^^^^^^^ help: use a byte literal instead: `b'\n'` + | + = note: `char` is four bytes wide, but `u8` is a single byte + +error: casting a character literal to `u8` truncates + --> tests/ui/char_lit_as_u8.rs:8:13 + | +LL | let _ = '\0' as u8; + | ^^^^^^^^^^ help: use a byte literal instead: `b'\0'` + | + = note: `char` is four bytes wide, but `u8` is a single byte + +error: casting a character literal to `u8` truncates + --> tests/ui/char_lit_as_u8.rs:10:13 + | +LL | let _ = '\x01' as u8; + | ^^^^^^^^^^^^ help: use a byte literal instead: `b'\x01'` + | + = note: `char` is four bytes wide, but `u8` is a single byte + +error: aborting due to 4 previous errors diff --git a/tests/ui/char_lit_as_u8_suggestions.rs b/tests/ui/char_lit_as_u8_suggestions.rs deleted file mode 100644 index a8f39e27605e..000000000000 --- a/tests/ui/char_lit_as_u8_suggestions.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![warn(clippy::char_lit_as_u8)] - -fn main() { - let _ = 'a' as u8; - //~^ char_lit_as_u8 - let _ = '\n' as u8; - //~^ char_lit_as_u8 - let _ = '\0' as u8; - //~^ char_lit_as_u8 - let _ = '\x01' as u8; - //~^ char_lit_as_u8 -} diff --git a/tests/ui/char_lit_as_u8_suggestions.stderr b/tests/ui/char_lit_as_u8_suggestions.stderr deleted file mode 100644 index 158dfd6bed26..000000000000 --- a/tests/ui/char_lit_as_u8_suggestions.stderr +++ /dev/null @@ -1,36 +0,0 @@ -error: casting a character literal to `u8` truncates - --> tests/ui/char_lit_as_u8_suggestions.rs:4:13 - | -LL | let _ = 'a' as u8; - | ^^^^^^^^^ help: use a byte literal instead: `b'a'` - | - = note: `char` is four bytes wide, but `u8` is a single byte - = note: `-D clippy::char-lit-as-u8` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::char_lit_as_u8)]` - -error: casting a character literal to `u8` truncates - --> tests/ui/char_lit_as_u8_suggestions.rs:6:13 - | -LL | let _ = '\n' as u8; - | ^^^^^^^^^^ help: use a byte literal instead: `b'\n'` - | - = note: `char` is four bytes wide, but `u8` is a single byte - -error: casting a character literal to `u8` truncates - --> tests/ui/char_lit_as_u8_suggestions.rs:8:13 - | -LL | let _ = '\0' as u8; - | ^^^^^^^^^^ help: use a byte literal instead: `b'\0'` - | - = note: `char` is four bytes wide, but `u8` is a single byte - -error: casting a character literal to `u8` truncates - --> tests/ui/char_lit_as_u8_suggestions.rs:10:13 - | -LL | let _ = '\x01' as u8; - | ^^^^^^^^^^^^ help: use a byte literal instead: `b'\x01'` - | - = note: `char` is four bytes wide, but `u8` is a single byte - -error: aborting due to 4 previous errors - diff --git a/tests/ui/char_lit_as_u8_unfixable.rs b/tests/ui/char_lit_as_u8_unfixable.rs new file mode 100644 index 000000000000..e5c094f158ec --- /dev/null +++ b/tests/ui/char_lit_as_u8_unfixable.rs @@ -0,0 +1,8 @@ +//@no-rustfix +#![warn(clippy::char_lit_as_u8)] + +fn main() { + // no suggestion, since a byte literal won't work. + let _ = '❤' as u8; + //~^ char_lit_as_u8 +} diff --git a/tests/ui/char_lit_as_u8_unfixable.stderr b/tests/ui/char_lit_as_u8_unfixable.stderr new file mode 100644 index 000000000000..49e555ae638a --- /dev/null +++ b/tests/ui/char_lit_as_u8_unfixable.stderr @@ -0,0 +1,12 @@ +error: casting a character literal to `u8` truncates + --> tests/ui/char_lit_as_u8_unfixable.rs:6:13 + | +LL | let _ = '❤' as u8; + | ^^^^^^^^^ + | + = note: `char` is four bytes wide, but `u8` is a single byte + = note: `-D clippy::char-lit-as-u8` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::char_lit_as_u8)]` + +error: aborting due to 1 previous error +