@@ -11,8 +11,9 @@ use crate::LateContext;
1111use crate :: context:: LintContext ;
1212use crate :: lints:: {
1313 OnlyCastu8ToChar , OverflowingBinHex , OverflowingBinHexSign , OverflowingBinHexSignBitSub ,
14- OverflowingBinHexSub , OverflowingInt , OverflowingIntHelp , OverflowingLiteral , OverflowingUInt ,
15- RangeEndpointOutOfRange , SurrogateCharCast , TooLargeCharCast , UseInclusiveRange ,
14+ OverflowingBinHexSub , OverflowingInt , OverflowingIntError , OverflowingIntHelp ,
15+ OverflowingLiteral , OverflowingUInt , OverflowingUIntError , RangeEndpointOutOfRange ,
16+ SurrogateCharCast , TooLargeCharCast , UseInclusiveRange ,
1617} ;
1718use crate :: types:: { OVERFLOWING_LITERALS , TypeLimits } ;
1819
@@ -258,6 +259,7 @@ fn lint_int_literal<'tcx>(
258259 lit : & hir:: Lit ,
259260 t : ty:: IntTy ,
260261 v : u128 ,
262+ force_error : bool ,
261263) {
262264 let int_type = t. normalize ( cx. sess ( ) . target . pointer_width ) ;
263265 let ( min, max) = int_ty_range ( int_type) ;
@@ -295,11 +297,22 @@ fn lint_int_literal<'tcx>(
295297 let help = get_type_suggestion ( cx. typeck_results ( ) . node_type ( hir_id) , v, negative)
296298 . map ( |suggestion_ty| OverflowingIntHelp { suggestion_ty } ) ;
297299
298- cx. emit_span_lint (
299- OVERFLOWING_LITERALS ,
300- span,
301- OverflowingInt { ty : t. name_str ( ) , lit, min, max, help } ,
302- ) ;
300+ if force_error {
301+ cx. tcx . dcx ( ) . emit_err ( OverflowingIntError {
302+ span,
303+ ty : t. name_str ( ) ,
304+ lit,
305+ min,
306+ max,
307+ help,
308+ } ) ;
309+ } else {
310+ cx. emit_span_lint (
311+ OVERFLOWING_LITERALS ,
312+ span,
313+ OverflowingInt { ty : t. name_str ( ) , lit, min, max, help } ,
314+ ) ;
315+ }
303316 }
304317}
305318
@@ -309,6 +322,7 @@ fn lint_uint_literal<'tcx>(
309322 span : Span ,
310323 lit : & hir:: Lit ,
311324 t : ty:: UintTy ,
325+ force_error : bool ,
312326) {
313327 let uint_type = t. normalize ( cx. sess ( ) . target . pointer_width ) ;
314328 let ( min, max) = uint_ty_range ( uint_type) ;
@@ -366,10 +380,9 @@ fn lint_uint_literal<'tcx>(
366380 ) ;
367381 return ;
368382 }
369- cx. emit_span_lint (
370- OVERFLOWING_LITERALS ,
371- span,
372- OverflowingUInt {
383+ if force_error {
384+ cx. tcx . dcx ( ) . emit_err ( OverflowingUIntError {
385+ span,
373386 ty : t. name_str ( ) ,
374387 lit : cx
375388 . sess ( )
@@ -378,8 +391,23 @@ fn lint_uint_literal<'tcx>(
378391 . unwrap_or_else ( |_| lit_val. to_string ( ) ) ,
379392 min,
380393 max,
381- } ,
382- ) ;
394+ } ) ;
395+ } else {
396+ cx. emit_span_lint (
397+ OVERFLOWING_LITERALS ,
398+ span,
399+ OverflowingUInt {
400+ ty : t. name_str ( ) ,
401+ lit : cx
402+ . sess ( )
403+ . source_map ( )
404+ . span_to_snippet ( lit. span )
405+ . unwrap_or_else ( |_| lit_val. to_string ( ) ) ,
406+ min,
407+ max,
408+ } ,
409+ ) ;
410+ }
383411 }
384412}
385413
@@ -391,18 +419,39 @@ pub(crate) fn lint_literal<'tcx>(
391419 lit : & hir:: Lit ,
392420 negated : bool ,
393421) {
394- match * cx. typeck_results ( ) . node_type ( hir_id) . kind ( ) {
422+ lint_literal_inner (
423+ cx,
424+ type_limits,
425+ hir_id,
426+ cx. typeck_results ( ) . node_type ( hir_id) ,
427+ span,
428+ lit,
429+ negated,
430+ false ,
431+ )
432+ }
433+ pub ( crate ) fn lint_literal_inner < ' tcx > (
434+ cx : & LateContext < ' tcx > ,
435+ type_limits : & TypeLimits ,
436+ hir_id : HirId ,
437+ ty : Ty < ' tcx > ,
438+ span : Span ,
439+ lit : & hir:: Lit ,
440+ negated : bool ,
441+ force_error : bool ,
442+ ) {
443+ match * ty. kind ( ) {
395444 ty:: Int ( t) => {
396445 match lit. node {
397446 ast:: LitKind :: Int ( v, ast:: LitIntType :: Signed ( _) | ast:: LitIntType :: Unsuffixed ) => {
398- lint_int_literal ( cx, type_limits, hir_id, span, lit, t, v. get ( ) )
447+ lint_int_literal ( cx, type_limits, hir_id, span, lit, t, v. get ( ) , force_error )
399448 }
400449 _ => bug ! ( ) ,
401450 } ;
402451 }
403452 ty:: Uint ( t) => {
404453 assert ! ( !negated) ;
405- lint_uint_literal ( cx, hir_id, span, lit, t)
454+ lint_uint_literal ( cx, hir_id, span, lit, t, force_error )
406455 }
407456 ty:: Float ( t) => {
408457 let ( is_infinite, sym) = match lit. node {
@@ -431,6 +480,12 @@ pub(crate) fn lint_literal<'tcx>(
431480 ) ;
432481 }
433482 }
483+ ty:: Pat ( base, ..) if base. is_integral ( ) => {
484+ lint_literal_inner ( cx, type_limits, hir_id, base, span, lit, negated, true )
485+ }
486+ ty:: Adt ( adt, args) if cx. tcx . is_lang_item ( adt. did ( ) , hir:: LangItem :: NonZero ) => {
487+ lint_literal_inner ( cx, type_limits, hir_id, args. type_at ( 0 ) , span, lit, negated, true )
488+ }
434489 _ => { }
435490 }
436491}
0 commit comments