@@ -10,9 +10,9 @@ use {rustc_ast as ast, rustc_attr_data_structures as attrs, rustc_hir as hir};
1010use crate :: LateContext ;
1111use crate :: context:: LintContext ;
1212use crate :: lints:: {
13- OnlyCastu8ToChar , OverflowingBinHex , OverflowingBinHexSign , OverflowingBinHexSignBitSub ,
14- OverflowingBinHexSub , OverflowingInt , OverflowingIntHelp , OverflowingLiteral , OverflowingUInt ,
15- RangeEndpointOutOfRange , UseInclusiveRange ,
13+ InvalidCharCast , OnlyCastu8ToChar , OverflowingBinHex , OverflowingBinHexSign ,
14+ OverflowingBinHexSignBitSub , OverflowingBinHexSub , OverflowingInt , OverflowingIntHelp ,
15+ OverflowingLiteral , OverflowingUInt , RangeEndpointOutOfRange , UseInclusiveRange ,
1616} ;
1717use crate :: types:: { OVERFLOWING_LITERALS , TypeLimits } ;
1818
@@ -38,12 +38,18 @@ fn lint_overflowing_range_endpoint<'tcx>(
3838
3939 // We only want to handle exclusive (`..`) ranges,
4040 // which are represented as `ExprKind::Struct`.
41- let Node :: ExprField ( field) = cx. tcx . parent_hir_node ( hir_id) else { return false } ;
42- let Node :: Expr ( struct_expr) = cx. tcx . parent_hir_node ( field. hir_id ) else { return false } ;
41+ let Node :: ExprField ( field) = cx. tcx . parent_hir_node ( hir_id) else {
42+ return false ;
43+ } ;
44+ let Node :: Expr ( struct_expr) = cx. tcx . parent_hir_node ( field. hir_id ) else {
45+ return false ;
46+ } ;
4347 if !is_range_literal ( struct_expr) {
4448 return false ;
4549 } ;
46- let ExprKind :: Struct ( _, [ start, end] , _) = & struct_expr. kind else { return false } ;
50+ let ExprKind :: Struct ( _, [ start, end] , _) = & struct_expr. kind else {
51+ return false ;
52+ } ;
4753
4854 // We can suggest using an inclusive range
4955 // (`..=`) instead only if it is the `end` that is
@@ -61,7 +67,9 @@ fn lint_overflowing_range_endpoint<'tcx>(
6167 } ;
6268
6369 let sub_sugg = if span. lo ( ) == lit_span. lo ( ) {
64- let Ok ( start) = cx. sess ( ) . source_map ( ) . span_to_snippet ( start. span ) else { return false } ;
70+ let Ok ( start) = cx. sess ( ) . source_map ( ) . span_to_snippet ( start. span ) else {
71+ return false ;
72+ } ;
6573 UseInclusiveRange :: WithoutParen {
6674 sugg : struct_expr. span . shrink_to_lo ( ) . to ( lit_span. shrink_to_hi ( ) ) ,
6775 start,
@@ -316,11 +324,19 @@ fn lint_uint_literal<'tcx>(
316324 match par_e. kind {
317325 hir:: ExprKind :: Cast ( ..) => {
318326 if let ty:: Char = cx. typeck_results ( ) . expr_ty ( par_e) . kind ( ) {
319- cx. emit_span_lint (
320- OVERFLOWING_LITERALS ,
321- par_e. span ,
322- OnlyCastu8ToChar { span : par_e. span , literal : lit_val } ,
323- ) ;
327+ if lit_val <= 0xFF {
328+ cx. emit_span_lint (
329+ OVERFLOWING_LITERALS ,
330+ par_e. span ,
331+ OnlyCastu8ToChar { span : par_e. span , literal : lit_val } ,
332+ ) ;
333+ } else {
334+ cx. emit_span_lint (
335+ OVERFLOWING_LITERALS ,
336+ par_e. span ,
337+ InvalidCharCast { literal : lit_val } ,
338+ ) ;
339+ }
324340 return ;
325341 }
326342 }
0 commit comments