@@ -11,7 +11,7 @@ use rustc_ast::ast::{Expr, ExprKind, Lit, LitKind};
1111use rustc_errors:: Applicability ;
1212use rustc_lint:: { EarlyContext , EarlyLintPass , LintContext } ;
1313use rustc_middle:: lint:: in_external_macro;
14- use rustc_session:: { declare_lint_pass , declare_tool_lint, impl_lint_pass} ;
14+ use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
1515
1616declare_clippy_lint ! {
1717 /// **What it does:** Warns if a long integral or floating-point constant does
@@ -32,7 +32,7 @@ declare_clippy_lint! {
3232 /// ```
3333 pub UNREADABLE_LITERAL ,
3434 pedantic,
35- "long integer literal without underscores"
35+ "long literal without underscores"
3636}
3737
3838declare_clippy_lint ! {
@@ -208,7 +208,13 @@ impl WarningType {
208208 }
209209}
210210
211- declare_lint_pass ! ( LiteralDigitGrouping => [
211+ #[ allow( clippy:: module_name_repetitions) ]
212+ #[ derive( Copy , Clone ) ]
213+ pub struct LiteralDigitGrouping {
214+ lint_fraction_readability : bool ,
215+ }
216+
217+ impl_lint_pass ! ( LiteralDigitGrouping => [
212218 UNREADABLE_LITERAL ,
213219 INCONSISTENT_DIGIT_GROUPING ,
214220 LARGE_DIGIT_GROUPS ,
@@ -223,7 +229,7 @@ impl EarlyLintPass for LiteralDigitGrouping {
223229 }
224230
225231 if let ExprKind :: Lit ( ref lit) = expr. kind {
226- Self :: check_lit ( cx, lit)
232+ self . check_lit ( cx, lit)
227233 }
228234 }
229235}
@@ -232,7 +238,13 @@ impl EarlyLintPass for LiteralDigitGrouping {
232238const UUID_GROUP_LENS : [ usize ; 5 ] = [ 8 , 4 , 4 , 4 , 12 ] ;
233239
234240impl LiteralDigitGrouping {
235- fn check_lit ( cx : & EarlyContext < ' _ > , lit : & Lit ) {
241+ pub fn new ( lint_fraction_readability : bool ) -> Self {
242+ Self {
243+ lint_fraction_readability,
244+ }
245+ }
246+
247+ fn check_lit ( & self , cx : & EarlyContext < ' _ > , lit : & Lit ) {
236248 if_chain ! {
237249 if let Some ( src) = snippet_opt( cx, lit. span) ;
238250 if let Some ( mut num_lit) = NumericLiteral :: from_lit( & src, & lit) ;
@@ -247,9 +259,12 @@ impl LiteralDigitGrouping {
247259
248260 let result = ( || {
249261
250- let integral_group_size = Self :: get_group_size( num_lit. integer. split( '_' ) , num_lit. radix) ?;
262+ let integral_group_size = Self :: get_group_size( num_lit. integer. split( '_' ) , num_lit. radix, true ) ?;
251263 if let Some ( fraction) = num_lit. fraction {
252- let fractional_group_size = Self :: get_group_size( fraction. rsplit( '_' ) , num_lit. radix) ?;
264+ let fractional_group_size = Self :: get_group_size(
265+ fraction. rsplit( '_' ) ,
266+ num_lit. radix,
267+ self . lint_fraction_readability) ?;
253268
254269 let consistent = Self :: parts_consistent( integral_group_size,
255270 fractional_group_size,
@@ -363,7 +378,11 @@ impl LiteralDigitGrouping {
363378
364379 /// Returns the size of the digit groups (or None if ungrouped) if successful,
365380 /// otherwise returns a `WarningType` for linting.
366- fn get_group_size < ' a > ( groups : impl Iterator < Item = & ' a str > , radix : Radix ) -> Result < Option < usize > , WarningType > {
381+ fn get_group_size < ' a > (
382+ groups : impl Iterator < Item = & ' a str > ,
383+ radix : Radix ,
384+ lint_unreadable : bool ,
385+ ) -> Result < Option < usize > , WarningType > {
367386 let mut groups = groups. map ( str:: len) ;
368387
369388 let first = groups. next ( ) . expect ( "At least one group" ) ;
@@ -380,7 +399,7 @@ impl LiteralDigitGrouping {
380399 } else {
381400 Ok ( Some ( second) )
382401 }
383- } else if first > 5 {
402+ } else if first > 5 && lint_unreadable {
384403 Err ( WarningType :: UnreadableLiteral )
385404 } else {
386405 Ok ( None )
0 commit comments