forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move char_lit_as_u8 to its own module
- Loading branch information
Showing
3 changed files
with
75 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use rustc_ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self, UintTy}; | ||
|
||
use if_chain::if_chain; | ||
|
||
use crate::utils::{snippet_with_applicability, span_lint_and_then}; | ||
|
||
use super::CHAR_LIT_AS_U8; | ||
|
||
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
if_chain! { | ||
if let ExprKind::Cast(e, _) = &expr.kind; | ||
if let ExprKind::Lit(l) = &e.kind; | ||
if let LitKind::Char(c) = l.node; | ||
if ty::Uint(UintTy::U8) == *cx.typeck_results().expr_ty(expr).kind(); | ||
then { | ||
let mut applicability = Applicability::MachineApplicable; | ||
let snippet = snippet_with_applicability(cx, e.span, "'x'", &mut applicability); | ||
|
||
span_lint_and_then( | ||
cx, | ||
CHAR_LIT_AS_U8, | ||
expr.span, | ||
"casting a character literal to `u8` truncates", | ||
|diag| { | ||
diag.note("`char` is four bytes wide, but `u8` is a single byte"); | ||
|
||
if c.is_ascii() { | ||
diag.span_suggestion( | ||
expr.span, | ||
"use a byte literal instead", | ||
format!("b{}", snippet), | ||
applicability, | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters