Skip to content

Commit 6659540

Browse files
authored
move char_lit_as_u8 under the explicit-cast case (#15477)
allows reusing `cast_from_expr` and `cast_to` also make the test files a bit more.. standard changelog: none
2 parents 306d4e3 + e6f3cb0 commit 6659540

9 files changed

+61
-61
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);

tests/ui/char_lit_as_u8.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#![warn(clippy::char_lit_as_u8)]
22

33
fn main() {
4-
// no suggestion, since a byte literal won't work.
5-
let _ = '❤' as u8;
4+
let _ = 'a' as u8;
5+
//~^ char_lit_as_u8
6+
let _ = '\n' as u8;
7+
//~^ char_lit_as_u8
8+
let _ = '\0' as u8;
9+
//~^ char_lit_as_u8
10+
let _ = '\x01' as u8;
611
//~^ char_lit_as_u8
712
}

tests/ui/char_lit_as_u8.stderr

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
error: casting a character literal to `u8` truncates
2-
--> tests/ui/char_lit_as_u8.rs:5:13
2+
--> tests/ui/char_lit_as_u8.rs:4:13
33
|
4-
LL | let _ = '' as u8;
5-
| ^^^^^^^^^
4+
LL | let _ = 'a' as u8;
5+
| ^^^^^^^^^ help: use a byte literal instead: `b'a'`
66
|
77
= note: `char` is four bytes wide, but `u8` is a single byte
88
= note: `-D clippy::char-lit-as-u8` implied by `-D warnings`
99
= help: to override `-D warnings` add `#[allow(clippy::char_lit_as_u8)]`
1010

11-
error: aborting due to 1 previous error
11+
error: casting a character literal to `u8` truncates
12+
--> tests/ui/char_lit_as_u8.rs:6:13
13+
|
14+
LL | let _ = '\n' as u8;
15+
| ^^^^^^^^^^ help: use a byte literal instead: `b'\n'`
16+
|
17+
= note: `char` is four bytes wide, but `u8` is a single byte
18+
19+
error: casting a character literal to `u8` truncates
20+
--> tests/ui/char_lit_as_u8.rs:8:13
21+
|
22+
LL | let _ = '\0' as u8;
23+
| ^^^^^^^^^^ help: use a byte literal instead: `b'\0'`
24+
|
25+
= note: `char` is four bytes wide, but `u8` is a single byte
26+
27+
error: casting a character literal to `u8` truncates
28+
--> tests/ui/char_lit_as_u8.rs:10:13
29+
|
30+
LL | let _ = '\x01' as u8;
31+
| ^^^^^^^^^^^^ help: use a byte literal instead: `b'\x01'`
32+
|
33+
= note: `char` is four bytes wide, but `u8` is a single byte
34+
35+
error: aborting due to 4 previous errors
1236

tests/ui/char_lit_as_u8_suggestions.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/ui/char_lit_as_u8_suggestions.stderr

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/ui/char_lit_as_u8_unfixable.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@no-rustfix
2+
#![warn(clippy::char_lit_as_u8)]
3+
4+
fn main() {
5+
// no suggestion, since a byte literal won't work.
6+
let _ = '❤' as u8;
7+
//~^ char_lit_as_u8
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: casting a character literal to `u8` truncates
2+
--> tests/ui/char_lit_as_u8_unfixable.rs:6:13
3+
|
4+
LL | let _ = '❤' as u8;
5+
| ^^^^^^^^^
6+
|
7+
= note: `char` is four bytes wide, but `u8` is a single byte
8+
= note: `-D clippy::char-lit-as-u8` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::char_lit_as_u8)]`
10+
11+
error: aborting due to 1 previous error
12+

0 commit comments

Comments
 (0)