@@ -11,8 +11,9 @@ use crate::LateContext;
11
11
use crate :: context:: LintContext ;
12
12
use crate :: lints:: {
13
13
OnlyCastu8ToChar , OverflowingBinHex , OverflowingBinHexSign , OverflowingBinHexSignBitSub ,
14
- OverflowingBinHexSub , OverflowingInt , OverflowingIntHelp , OverflowingLiteral , OverflowingUInt ,
15
- RangeEndpointOutOfRange , UseInclusiveRange ,
14
+ OverflowingBinHexSub , OverflowingInt , OverflowingIntError , OverflowingIntHelp ,
15
+ OverflowingLiteral , OverflowingUInt , OverflowingUIntError , RangeEndpointOutOfRange ,
16
+ UseInclusiveRange ,
16
17
} ;
17
18
use crate :: types:: { OVERFLOWING_LITERALS , TypeLimits } ;
18
19
@@ -250,6 +251,7 @@ fn lint_int_literal<'tcx>(
250
251
lit : & hir:: Lit ,
251
252
t : ty:: IntTy ,
252
253
v : u128 ,
254
+ force_error : bool ,
253
255
) {
254
256
let int_type = t. normalize ( cx. sess ( ) . target . pointer_width ) ;
255
257
let ( min, max) = int_ty_range ( int_type) ;
@@ -287,11 +289,22 @@ fn lint_int_literal<'tcx>(
287
289
let help = get_type_suggestion ( cx. typeck_results ( ) . node_type ( hir_id) , v, negative)
288
290
. map ( |suggestion_ty| OverflowingIntHelp { suggestion_ty } ) ;
289
291
290
- cx. emit_span_lint (
291
- OVERFLOWING_LITERALS ,
292
- span,
293
- OverflowingInt { ty : t. name_str ( ) , lit, min, max, help } ,
294
- ) ;
292
+ if force_error {
293
+ cx. tcx . dcx ( ) . emit_err ( OverflowingIntError {
294
+ span,
295
+ ty : t. name_str ( ) ,
296
+ lit,
297
+ min,
298
+ max,
299
+ help,
300
+ } ) ;
301
+ } else {
302
+ cx. emit_span_lint (
303
+ OVERFLOWING_LITERALS ,
304
+ span,
305
+ OverflowingInt { ty : t. name_str ( ) , lit, min, max, help } ,
306
+ ) ;
307
+ }
295
308
}
296
309
}
297
310
@@ -301,6 +314,7 @@ fn lint_uint_literal<'tcx>(
301
314
span : Span ,
302
315
lit : & hir:: Lit ,
303
316
t : ty:: UintTy ,
317
+ force_error : bool ,
304
318
) {
305
319
let uint_type = t. normalize ( cx. sess ( ) . target . pointer_width ) ;
306
320
let ( min, max) = uint_ty_range ( uint_type) ;
@@ -344,10 +358,9 @@ fn lint_uint_literal<'tcx>(
344
358
) ;
345
359
return ;
346
360
}
347
- cx. emit_span_lint (
348
- OVERFLOWING_LITERALS ,
349
- span,
350
- OverflowingUInt {
361
+ if force_error {
362
+ cx. tcx . dcx ( ) . emit_err ( OverflowingUIntError {
363
+ span,
351
364
ty : t. name_str ( ) ,
352
365
lit : cx
353
366
. sess ( )
@@ -356,8 +369,23 @@ fn lint_uint_literal<'tcx>(
356
369
. unwrap_or_else ( |_| lit_val. to_string ( ) ) ,
357
370
min,
358
371
max,
359
- } ,
360
- ) ;
372
+ } ) ;
373
+ } else {
374
+ cx. emit_span_lint (
375
+ OVERFLOWING_LITERALS ,
376
+ span,
377
+ OverflowingUInt {
378
+ ty : t. name_str ( ) ,
379
+ lit : cx
380
+ . sess ( )
381
+ . source_map ( )
382
+ . span_to_snippet ( lit. span )
383
+ . unwrap_or_else ( |_| lit_val. to_string ( ) ) ,
384
+ min,
385
+ max,
386
+ } ,
387
+ ) ;
388
+ }
361
389
}
362
390
}
363
391
@@ -369,18 +397,39 @@ pub(crate) fn lint_literal<'tcx>(
369
397
lit : & hir:: Lit ,
370
398
negated : bool ,
371
399
) {
372
- match * cx. typeck_results ( ) . node_type ( hir_id) . kind ( ) {
400
+ lint_literal_inner (
401
+ cx,
402
+ type_limits,
403
+ hir_id,
404
+ cx. typeck_results ( ) . node_type ( hir_id) ,
405
+ span,
406
+ lit,
407
+ negated,
408
+ false ,
409
+ )
410
+ }
411
+ pub ( crate ) fn lint_literal_inner < ' tcx > (
412
+ cx : & LateContext < ' tcx > ,
413
+ type_limits : & TypeLimits ,
414
+ hir_id : HirId ,
415
+ ty : Ty < ' tcx > ,
416
+ span : Span ,
417
+ lit : & hir:: Lit ,
418
+ negated : bool ,
419
+ force_error : bool ,
420
+ ) {
421
+ match * ty. kind ( ) {
373
422
ty:: Int ( t) => {
374
423
match lit. node {
375
424
ast:: LitKind :: Int ( v, ast:: LitIntType :: Signed ( _) | ast:: LitIntType :: Unsuffixed ) => {
376
- lint_int_literal ( cx, type_limits, hir_id, span, lit, t, v. get ( ) )
425
+ lint_int_literal ( cx, type_limits, hir_id, span, lit, t, v. get ( ) , force_error )
377
426
}
378
427
_ => bug ! ( ) ,
379
428
} ;
380
429
}
381
430
ty:: Uint ( t) => {
382
431
assert ! ( !negated) ;
383
- lint_uint_literal ( cx, hir_id, span, lit, t)
432
+ lint_uint_literal ( cx, hir_id, span, lit, t, force_error )
384
433
}
385
434
ty:: Float ( t) => {
386
435
let ( is_infinite, sym) = match lit. node {
@@ -409,6 +458,12 @@ pub(crate) fn lint_literal<'tcx>(
409
458
) ;
410
459
}
411
460
}
461
+ ty:: Pat ( base, ..) if base. is_integral ( ) => {
462
+ lint_literal_inner ( cx, type_limits, hir_id, base, span, lit, negated, true )
463
+ }
464
+ ty:: Adt ( adt, args) if cx. tcx . is_lang_item ( adt. did ( ) , hir:: LangItem :: NonZero ) => {
465
+ lint_literal_inner ( cx, type_limits, hir_id, args. type_at ( 0 ) , span, lit, negated, true )
466
+ }
412
467
_ => { }
413
468
}
414
469
}
0 commit comments