@@ -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 , UseInclusiveRange ,
14+ OverflowingBinHexSub , OverflowingInt , OverflowingIntError , OverflowingIntHelp ,
15+ OverflowingLiteral , OverflowingUInt , OverflowingUIntError , RangeEndpointOutOfRange ,
16+ UseInclusiveRange ,
1617} ;
1718use crate :: types:: { OVERFLOWING_LITERALS , TypeLimits } ;
1819
@@ -250,6 +251,7 @@ fn lint_int_literal<'tcx>(
250251 lit : & hir:: Lit ,
251252 t : ty:: IntTy ,
252253 v : u128 ,
254+ force_error : bool ,
253255) {
254256 let int_type = t. normalize ( cx. sess ( ) . target . pointer_width ) ;
255257 let ( min, max) = int_ty_range ( int_type) ;
@@ -287,11 +289,22 @@ fn lint_int_literal<'tcx>(
287289 let help = get_type_suggestion ( cx. typeck_results ( ) . node_type ( hir_id) , v, negative)
288290 . map ( |suggestion_ty| OverflowingIntHelp { suggestion_ty } ) ;
289291
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+ }
295308 }
296309}
297310
@@ -301,6 +314,7 @@ fn lint_uint_literal<'tcx>(
301314 span : Span ,
302315 lit : & hir:: Lit ,
303316 t : ty:: UintTy ,
317+ force_error : bool ,
304318) {
305319 let uint_type = t. normalize ( cx. sess ( ) . target . pointer_width ) ;
306320 let ( min, max) = uint_ty_range ( uint_type) ;
@@ -344,10 +358,9 @@ fn lint_uint_literal<'tcx>(
344358 ) ;
345359 return ;
346360 }
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,
351364 ty : t. name_str ( ) ,
352365 lit : cx
353366 . sess ( )
@@ -356,8 +369,23 @@ fn lint_uint_literal<'tcx>(
356369 . unwrap_or_else ( |_| lit_val. to_string ( ) ) ,
357370 min,
358371 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+ }
361389 }
362390}
363391
@@ -369,18 +397,39 @@ pub(crate) fn lint_literal<'tcx>(
369397 lit : & hir:: Lit ,
370398 negated : bool ,
371399) {
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 ( ) {
373422 ty:: Int ( t) => {
374423 match lit. node {
375424 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 )
377426 }
378427 _ => bug ! ( ) ,
379428 } ;
380429 }
381430 ty:: Uint ( t) => {
382431 assert ! ( !negated) ;
383- lint_uint_literal ( cx, hir_id, span, lit, t)
432+ lint_uint_literal ( cx, hir_id, span, lit, t, force_error )
384433 }
385434 ty:: Float ( t) => {
386435 let ( is_infinite, sym) = match lit. node {
@@ -409,6 +458,12 @@ pub(crate) fn lint_literal<'tcx>(
409458 ) ;
410459 }
411460 }
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+ }
412467 _ => { }
413468 }
414469}
0 commit comments