Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions clippy_lints/src/casts/char_lit_as_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/casts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions tests/ui/char_lit_as_u8.rs
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 28 additions & 4 deletions tests/ui/char_lit_as_u8.stderr
Original file line number Diff line number Diff line change
@@ -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

12 changes: 0 additions & 12 deletions tests/ui/char_lit_as_u8_suggestions.rs

This file was deleted.

36 changes: 0 additions & 36 deletions tests/ui/char_lit_as_u8_suggestions.stderr

This file was deleted.

8 changes: 8 additions & 0 deletions tests/ui/char_lit_as_u8_unfixable.rs
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions tests/ui/char_lit_as_u8_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -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