11//! Lints concerned with the grouping of digits with underscores in integral or
22//! floating-point literal expressions.
33
4- use crate :: utils:: { snippet_opt, span_lint_and_sugg} ;
4+ use crate :: utils:: { in_macro , snippet_opt, span_lint_and_sugg} ;
55use if_chain:: if_chain;
66use rustc:: lint:: { in_external_macro, EarlyContext , EarlyLintPass , LintArray , LintContext , LintPass } ;
77use rustc:: { declare_lint_pass, declare_tool_lint, impl_lint_pass} ;
@@ -355,6 +355,7 @@ impl EarlyLintPass for LiteralDigitGrouping {
355355
356356impl LiteralDigitGrouping {
357357 fn check_lit ( self , cx : & EarlyContext < ' _ > , lit : & Lit ) {
358+ let in_macro = in_macro ( lit. span ) ;
358359 match lit. node {
359360 LitKind :: Int ( ..) => {
360361 // Lint integral literals.
@@ -364,7 +365,7 @@ impl LiteralDigitGrouping {
364365 if char :: to_digit( firstch, 10 ) . is_some( ) ;
365366 then {
366367 let digit_info = DigitInfo :: new( & src, false ) ;
367- let _ = Self :: do_lint( digit_info. digits, digit_info. suffix) . map_err( |warning_type| {
368+ let _ = Self :: do_lint( digit_info. digits, digit_info. suffix, in_macro ) . map_err( |warning_type| {
368369 warning_type. display( & digit_info. grouping_hint( ) , cx, lit. span)
369370 } ) ;
370371 }
@@ -386,12 +387,12 @@ impl LiteralDigitGrouping {
386387
387388 // Lint integral and fractional parts separately, and then check consistency of digit
388389 // groups if both pass.
389- let _ = Self :: do_lint( parts[ 0 ] , digit_info. suffix)
390+ let _ = Self :: do_lint( parts[ 0 ] , digit_info. suffix, in_macro )
390391 . map( |integral_group_size| {
391392 if parts. len( ) > 1 {
392393 // Lint the fractional part of literal just like integral part, but reversed.
393394 let fractional_part = & parts[ 1 ] . chars( ) . rev( ) . collect:: <String >( ) ;
394- let _ = Self :: do_lint( fractional_part, None )
395+ let _ = Self :: do_lint( fractional_part, None , in_macro )
395396 . map( |fractional_group_size| {
396397 let consistent = Self :: parts_consistent( integral_group_size,
397398 fractional_group_size,
@@ -436,7 +437,7 @@ impl LiteralDigitGrouping {
436437
437438 /// Performs lint on `digits` (no decimal point) and returns the group
438439 /// size on success or `WarningType` when emitting a warning.
439- fn do_lint ( digits : & str , suffix : Option < & str > ) -> Result < usize , WarningType > {
440+ fn do_lint ( digits : & str , suffix : Option < & str > , in_macro : bool ) -> Result < usize , WarningType > {
440441 if let Some ( suffix) = suffix {
441442 if is_mistyped_suffix ( suffix) {
442443 return Err ( WarningType :: MistypedLiteralSuffix ) ;
@@ -452,7 +453,7 @@ impl LiteralDigitGrouping {
452453
453454 if underscore_positions. is_empty ( ) {
454455 // Check if literal needs underscores.
455- if digits. len ( ) > 5 {
456+ if !in_macro && digits. len ( ) > 5 {
456457 Err ( WarningType :: UnreadableLiteral )
457458 } else {
458459 Ok ( 0 )
0 commit comments