Skip to content

Commit 4ea246b

Browse files
committed
use a structured suggestion for char-lit-as-u8
1 parent 70e7d07 commit 4ea246b

6 files changed

+89
-25
lines changed

clippy_lints/src/types.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
1515
use rustc_errors::Applicability;
1616
use rustc_target::spec::abi::Abi;
1717
use rustc_typeck::hir_ty_to_ty;
18-
use syntax::ast::{FloatTy, IntTy, UintTy};
18+
use syntax::ast::{FloatTy, IntTy, LitIntType, LitKind, UintTy};
1919
use syntax::errors::DiagnosticBuilder;
2020
use syntax::source_map::Span;
2121
use syntax::symbol::sym;
@@ -1122,7 +1122,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
11221122
let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr));
11231123
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
11241124
if let ExprKind::Lit(ref lit) = ex.node {
1125-
use syntax::ast::{LitIntType, LitKind};
11261125
if let LitKind::Int(n, _) = lit.node {
11271126
if cast_to.is_floating_point() {
11281127
let from_nbits = 128 - n.leading_zeros();
@@ -1473,29 +1472,40 @@ declare_clippy_lint! {
14731472
/// ```
14741473
pub CHAR_LIT_AS_U8,
14751474
complexity,
1476-
"casting a character literal to u8"
1475+
"casting a character literal to u8 truncates"
14771476
}
14781477

14791478
declare_lint_pass!(CharLitAsU8 => [CHAR_LIT_AS_U8]);
14801479

14811480
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CharLitAsU8 {
14821481
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
1483-
use syntax::ast::LitKind;
1484-
1485-
if let ExprKind::Cast(ref e, _) = expr.node {
1486-
if let ExprKind::Lit(ref l) = e.node {
1487-
if let LitKind::Char(_) = l.node {
1488-
if ty::Uint(UintTy::U8) == cx.tables.expr_ty(expr).sty && !expr.span.from_expansion() {
1489-
let msg = "casting character literal to u8. `char`s \
1490-
are 4 bytes wide in rust, so casting to u8 \
1491-
truncates them";
1492-
let help = format!(
1493-
"Consider using a byte literal instead:\nb{}",
1494-
snippet(cx, e.span, "'x'")
1495-
);
1496-
span_help_and_lint(cx, CHAR_LIT_AS_U8, expr.span, msg, &help);
1497-
}
1498-
}
1482+
if_chain! {
1483+
if !expr.span.from_expansion();
1484+
if let ExprKind::Cast(e, _) = &expr.node;
1485+
if let ExprKind::Lit(l) = &e.node;
1486+
if let LitKind::Char(c) = l.node;
1487+
if ty::Uint(UintTy::U8) == cx.tables.expr_ty(expr).sty;
1488+
then {
1489+
let mut applicability = Applicability::MachineApplicable;
1490+
let snippet = snippet_with_applicability(cx, e.span, "'x'", &mut applicability);
1491+
1492+
span_lint_and_then(
1493+
cx,
1494+
CHAR_LIT_AS_U8,
1495+
expr.span,
1496+
"casting a character literal to `u8` truncates",
1497+
|db| {
1498+
db.note("`char` is four bytes wide, but `u8` is a single byte");
1499+
1500+
if c.is_ascii() {
1501+
db.span_suggestion(
1502+
expr.span,
1503+
"use a byte literal instead",
1504+
format!("b{}", snippet),
1505+
applicability,
1506+
);
1507+
}
1508+
});
14991509
}
15001510
}
15011511
}

tests/ui/char_lit_as_u8.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::char_lit_as_u8)]
2-
#![allow(unused_variables)]
2+
33
fn main() {
4-
let c = 'a' as u8;
4+
let _ = '' as u8; // no suggestion, since a byte literal won't work.
55
}

tests/ui/char_lit_as_u8.stderr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
error: casting character literal to u8. `char`s are 4 bytes wide in rust, so casting to u8 truncates them
1+
error: casting a character literal to `u8` truncates
22
--> $DIR/char_lit_as_u8.rs:4:13
33
|
4-
LL | let c = 'a' as u8;
4+
LL | let _ = '' as u8; // no suggestion, since a byte literal won't work.
55
| ^^^^^^^^^
66
|
77
= note: `-D clippy::char-lit-as-u8` implied by `-D warnings`
8-
= help: Consider using a byte literal instead:
9-
b'a'
8+
= note: `char` is four bytes wide, but `u8` is a single byte
109

1110
error: aborting due to previous error
1211

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::char_lit_as_u8)]
4+
5+
fn main() {
6+
let _ = b'a';
7+
let _ = b'\n';
8+
let _ = b'\0';
9+
let _ = b'\x01';
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::char_lit_as_u8)]
4+
5+
fn main() {
6+
let _ = 'a' as u8;
7+
let _ = '\n' as u8;
8+
let _ = '\0' as u8;
9+
let _ = '\x01' as u8;
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: casting a character literal to `u8` truncates
2+
--> $DIR/char_lit_as_u8_suggestions.rs:6:13
3+
|
4+
LL | let _ = 'a' as u8;
5+
| ^^^^^^^^^ help: use a byte literal instead: `b'a'`
6+
|
7+
= note: `-D clippy::char-lit-as-u8` implied by `-D warnings`
8+
= note: `char` is four bytes wide, but `u8` is a single byte
9+
10+
error: casting a character literal to `u8` truncates
11+
--> $DIR/char_lit_as_u8_suggestions.rs:7:13
12+
|
13+
LL | let _ = '/n' as u8;
14+
| ^^^^^^^^^^ help: use a byte literal instead: `b'/n'`
15+
|
16+
= note: `char` is four bytes wide, but `u8` is a single byte
17+
18+
error: casting a character literal to `u8` truncates
19+
--> $DIR/char_lit_as_u8_suggestions.rs:8:13
20+
|
21+
LL | let _ = '/0' as u8;
22+
| ^^^^^^^^^^ help: use a byte literal instead: `b'/0'`
23+
|
24+
= note: `char` is four bytes wide, but `u8` is a single byte
25+
26+
error: casting a character literal to `u8` truncates
27+
--> $DIR/char_lit_as_u8_suggestions.rs:9:13
28+
|
29+
LL | let _ = '/x01' as u8;
30+
| ^^^^^^^^^^^^ help: use a byte literal instead: `b'/x01'`
31+
|
32+
= note: `char` is four bytes wide, but `u8` is a single byte
33+
34+
error: aborting due to 4 previous errors
35+

0 commit comments

Comments
 (0)