@@ -11,7 +11,7 @@ use rustc_ast::ast::{Expr, ExprKind, Lit, LitKind};
11
11
use rustc_errors:: Applicability ;
12
12
use rustc_lint:: { EarlyContext , EarlyLintPass , LintContext } ;
13
13
use 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} ;
15
15
16
16
declare_clippy_lint ! {
17
17
/// **What it does:** Warns if a long integral or floating-point constant does
@@ -32,7 +32,7 @@ declare_clippy_lint! {
32
32
/// ```
33
33
pub UNREADABLE_LITERAL ,
34
34
pedantic,
35
- "long integer literal without underscores"
35
+ "long literal without underscores"
36
36
}
37
37
38
38
declare_clippy_lint ! {
@@ -208,7 +208,13 @@ impl WarningType {
208
208
}
209
209
}
210
210
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 => [
212
218
UNREADABLE_LITERAL ,
213
219
INCONSISTENT_DIGIT_GROUPING ,
214
220
LARGE_DIGIT_GROUPS ,
@@ -223,7 +229,7 @@ impl EarlyLintPass for LiteralDigitGrouping {
223
229
}
224
230
225
231
if let ExprKind :: Lit ( ref lit) = expr. kind {
226
- Self :: check_lit ( cx, lit)
232
+ self . check_lit ( cx, lit)
227
233
}
228
234
}
229
235
}
@@ -232,7 +238,13 @@ impl EarlyLintPass for LiteralDigitGrouping {
232
238
const UUID_GROUP_LENS : [ usize ; 5 ] = [ 8 , 4 , 4 , 4 , 12 ] ;
233
239
234
240
impl 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 ) {
236
248
if_chain ! {
237
249
if let Some ( src) = snippet_opt( cx, lit. span) ;
238
250
if let Some ( mut num_lit) = NumericLiteral :: from_lit( & src, & lit) ;
@@ -247,9 +259,12 @@ impl LiteralDigitGrouping {
247
259
248
260
let result = ( || {
249
261
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 ) ?;
251
263
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) ?;
253
268
254
269
let consistent = Self :: parts_consistent( integral_group_size,
255
270
fractional_group_size,
@@ -363,7 +378,11 @@ impl LiteralDigitGrouping {
363
378
364
379
/// Returns the size of the digit groups (or None if ungrouped) if successful,
365
380
/// 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 > {
367
386
let mut groups = groups. map ( str:: len) ;
368
387
369
388
let first = groups. next ( ) . expect ( "At least one group" ) ;
@@ -380,7 +399,7 @@ impl LiteralDigitGrouping {
380
399
} else {
381
400
Ok ( Some ( second) )
382
401
}
383
- } else if first > 5 {
402
+ } else if first > 5 && lint_unreadable {
384
403
Err ( WarningType :: UnreadableLiteral )
385
404
} else {
386
405
Ok ( None )
0 commit comments