@@ -10,9 +10,9 @@ use {rustc_ast as ast, rustc_attr_data_structures as attrs, rustc_hir as hir};
10
10
use crate :: LateContext ;
11
11
use crate :: context:: LintContext ;
12
12
use 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 ,
16
16
} ;
17
17
use crate :: types:: { OVERFLOWING_LITERALS , TypeLimits } ;
18
18
@@ -38,12 +38,18 @@ fn lint_overflowing_range_endpoint<'tcx>(
38
38
39
39
// We only want to handle exclusive (`..`) ranges,
40
40
// 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
+ } ;
43
47
if !is_range_literal ( struct_expr) {
44
48
return false ;
45
49
} ;
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
+ } ;
47
53
48
54
// We can suggest using an inclusive range
49
55
// (`..=`) instead only if it is the `end` that is
@@ -61,7 +67,9 @@ fn lint_overflowing_range_endpoint<'tcx>(
61
67
} ;
62
68
63
69
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
+ } ;
65
73
UseInclusiveRange :: WithoutParen {
66
74
sugg : struct_expr. span . shrink_to_lo ( ) . to ( lit_span. shrink_to_hi ( ) ) ,
67
75
start,
@@ -316,11 +324,19 @@ fn lint_uint_literal<'tcx>(
316
324
match par_e. kind {
317
325
hir:: ExprKind :: Cast ( ..) => {
318
326
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
+ }
324
340
return ;
325
341
}
326
342
}
0 commit comments