]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/literal_representation.rs
rustup https://github.com/rust-lang/rust/pull/67455
[rust.git] / clippy_lints / src / literal_representation.rs
index b6f641d1b8cf2799708f2981339f965acf51277a..24f40a161be3450b0fb6e1db99177ea32084a565 100644 (file)
@@ -4,8 +4,9 @@
 use crate::utils::{in_macro, snippet_opt, span_lint_and_sugg};
 use if_chain::if_chain;
 use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
-use rustc::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
+use rustc::{declare_lint_pass, impl_lint_pass};
 use rustc_errors::Applicability;
+use rustc_session::declare_tool_lint;
 use syntax::ast::*;
 use syntax_pos;
 
@@ -268,7 +269,7 @@ fn group_digits(output: &mut String, input: &str, group_size: usize, partial_gro
         let first_group_size;
 
         if partial_group_first {
-            first_group_size = (digits.clone().count() + group_size - 1) % group_size + 1;
+            first_group_size = (digits.clone().count() - 1) % group_size + 1;
             if pad {
                 for _ in 0..group_size - first_group_size {
                     output.push('0');
@@ -402,8 +403,6 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
 
 impl LiteralDigitGrouping {
     fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
-        let in_macro = in_macro(lit.span);
-
         if_chain! {
             if let Some(src) = snippet_opt(cx, lit.span);
             if let Some(mut num_lit) = NumericLiteral::from_lit(&src, &lit);
@@ -414,9 +413,9 @@ fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
 
                 let result = (|| {
 
-                    let integral_group_size = Self::get_group_size(num_lit.integer.split('_'), in_macro)?;
+                    let integral_group_size = Self::get_group_size(num_lit.integer.split('_'))?;
                     if let Some(fraction) = num_lit.fraction {
-                        let fractional_group_size = Self::get_group_size(fraction.rsplit('_'), in_macro)?;
+                        let fractional_group_size = Self::get_group_size(fraction.rsplit('_'))?;
 
                         let consistent = Self::parts_consistent(integral_group_size,
                                                                 fractional_group_size,
@@ -431,7 +430,19 @@ fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
 
 
                 if let Err(warning_type) = result {
-                    warning_type.display(num_lit.format(), cx, lit.span)
+                    let should_warn = match warning_type {
+                        | WarningType::UnreadableLiteral
+                        | WarningType::InconsistentDigitGrouping
+                        | WarningType::LargeDigitGroups => {
+                            !in_macro(lit.span)
+                        }
+                        WarningType::DecimalRepresentation | WarningType::MistypedLiteralSuffix => {
+                            true
+                        }
+                    };
+                    if should_warn {
+                        warning_type.display(num_lit.format(), cx, lit.span)
+                    }
                 }
             }
         }
@@ -494,7 +505,7 @@ fn parts_consistent(
 
     /// Returns the size of the digit groups (or None if ungrouped) if successful,
     /// otherwise returns a `WarningType` for linting.
-    fn get_group_size<'a>(groups: impl Iterator<Item = &'a str>, in_macro: bool) -> Result<Option<usize>, WarningType> {
+    fn get_group_size<'a>(groups: impl Iterator<Item = &'a str>) -> Result<Option<usize>, WarningType> {
         let mut groups = groups.map(str::len);
 
         let first = groups.next().expect("At least one group");
@@ -507,7 +518,7 @@ fn get_group_size<'a>(groups: impl Iterator<Item = &'a str>, in_macro: bool) ->
             } else {
                 Ok(Some(second))
             }
-        } else if first > 5 && !in_macro {
+        } else if first > 5 {
             Err(WarningType::UnreadableLiteral)
         } else {
             Ok(None)