@@ -9,12 +9,12 @@ mod zero_prefixed_literal;
99
1010use clippy_utils:: diagnostics:: span_lint;
1111use clippy_utils:: source:: snippet_opt;
12- use rustc_ast:: ast:: { Expr , Generics , Lit , LitFloatType , LitIntType , LitKind , NodeId , Pat , PatKind } ;
12+ use rustc_ast:: ast:: { Expr , ExprKind , Generics , Lit , LitFloatType , LitIntType , LitKind , NodeId , Pat , PatKind } ;
1313use rustc_ast:: visit:: FnKind ;
1414use rustc_data_structures:: fx:: FxHashMap ;
1515use rustc_lint:: { EarlyContext , EarlyLintPass } ;
1616use rustc_middle:: lint:: in_external_macro;
17- use rustc_session:: { declare_lint_pass , declare_tool_lint } ;
17+ use rustc_session:: { declare_tool_lint , impl_lint_pass } ;
1818use rustc_span:: source_map:: Span ;
1919
2020declare_clippy_lint ! {
@@ -113,20 +113,35 @@ declare_clippy_lint! {
113113
114114declare_clippy_lint ! {
115115 /// ### What it does
116- /// Warns if literal suffixes are not separated by an
117- /// underscore.
116+ /// If `literal-suffix-stylle` = "separated", warns literal suffixes that are not separated by an
117+ /// underscore
118+ /// e.g `123i32`
118119 ///
119- /// ### Why is this bad?
120- /// It is much less readable.
120+ /// If `literal-suffix-style` = "unseparated", warns literal suffixes that are separated by an
121+ /// underscore
122+ /// e.g. `123_i32`
123+ ///
124+ /// else, any style of literal_suffix is allowed
121125 ///
122126 /// ### Example
127+ ///
128+ /// #### "separated"
123129 /// ```rust
124130 /// // Bad
125131 /// let y = 123832i32;
126132 ///
127133 /// // Good
128134 /// let y = 123832_i32;
129135 /// ```
136+ ///
137+ /// #### "unseparated"
138+ /// ```rust
139+ /// // Bad
140+ /// let y = 123832_i32;
141+ ///
142+ /// // Good
143+ /// let y = 123832i32;
144+ /// ```
130145 pub UNSEPARATED_LITERAL_SUFFIX ,
131146 restriction,
132147 "literals whose suffix is not separated by an underscore"
@@ -254,7 +269,12 @@ declare_clippy_lint! {
254269 "tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`)"
255270}
256271
257- declare_lint_pass ! ( MiscEarlyLints => [
272+ #[ allow( clippy:: module_name_repetitions) ]
273+ pub struct MiscEarlyLints {
274+ literal_suffix_style : Option < LiteralSuffixStyle > ,
275+ }
276+
277+ impl_lint_pass ! ( MiscEarlyLints => [
258278 UNNEEDED_FIELD_PATTERN ,
259279 DUPLICATE_UNDERSCORE_ARGUMENT ,
260280 DOUBLE_NEG ,
@@ -310,12 +330,29 @@ impl EarlyLintPass for MiscEarlyLints {
310330 if in_external_macro ( cx. sess , expr. span ) {
311331 return ;
312332 }
333+
334+ if let ExprKind :: Lit ( ref lit) = expr. kind {
335+ self . check_lit ( cx, lit) ;
336+ }
313337 double_neg:: check ( cx, expr) ;
314338 }
315339}
316340
317341impl MiscEarlyLints {
318- fn check_lit ( cx : & EarlyContext < ' _ > , lit : & Lit ) {
342+ pub fn new ( suffix_style : Option < String > ) -> Self {
343+ let literal_suffix_style = match suffix_style {
344+ Some ( style) => match style. as_str ( ) {
345+ "unseparated" => Some ( LiteralSuffixStyle :: Unseparated ) ,
346+ "separated" => Some ( LiteralSuffixStyle :: Separated ) ,
347+ _ => None ,
348+ } ,
349+ _ => None ,
350+ } ;
351+
352+ Self { literal_suffix_style }
353+ }
354+
355+ fn check_lit ( & self , cx : & EarlyContext < ' _ > , lit : & Lit ) {
319356 // We test if first character in snippet is a number, because the snippet could be an expansion
320357 // from a built-in macro like `line!()` or a proc-macro like `#[wasm_bindgen]`.
321358 // Note that this check also covers special case that `line!()` is eagerly expanded by compiler.
@@ -332,7 +369,7 @@ impl MiscEarlyLints {
332369 LitIntType :: Unsigned ( ty) => ty. name_str ( ) ,
333370 LitIntType :: Unsuffixed => "" ,
334371 } ;
335- unseparated_literal_suffix:: check ( cx, lit, & lit_snip, suffix, "integer" ) ;
372+ unseparated_literal_suffix:: check ( cx, lit, & lit_snip, suffix, "integer" , self . literal_suffix_style ) ;
336373 if lit_snip. starts_with ( "0x" ) {
337374 mixed_case_hex_literals:: check ( cx, lit, suffix, & lit_snip) ;
338375 } else if lit_snip. starts_with ( "0b" ) || lit_snip. starts_with ( "0o" ) {
@@ -342,7 +379,13 @@ impl MiscEarlyLints {
342379 }
343380 } else if let LitKind :: Float ( _, LitFloatType :: Suffixed ( float_ty) ) = lit. kind {
344381 let suffix = float_ty. name_str ( ) ;
345- unseparated_literal_suffix:: check ( cx, lit, & lit_snip, suffix, "float" ) ;
382+ unseparated_literal_suffix:: check ( cx, lit, & lit_snip, suffix, "float" , self . literal_suffix_style ) ;
346383 }
347384 }
348385}
386+
387+ #[ derive( Copy , Clone ) ]
388+ enum LiteralSuffixStyle {
389+ Unseparated ,
390+ Separated ,
391+ }
0 commit comments