]> git.proxmox.com Git - rustc.git/blobdiff - src/tools/clippy/clippy_lints/src/misc_early/zero_prefixed_literal.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / misc_early / zero_prefixed_literal.rs
index 4963bba82f2da169887da3f88a3a5c7641a64000..9ead43ea4a477060961a8ae1afbd842f527cb71f 100644 (file)
@@ -6,6 +6,7 @@ use rustc_lint::EarlyContext;
 use super::ZERO_PREFIXED_LITERAL;
 
 pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str) {
+    let trimmed_lit_snip = lit_snip.trim_start_matches(|c| c == '_' || c == '0');
     span_lint_and_then(
         cx,
         ZERO_PREFIXED_LITERAL,
@@ -15,15 +16,18 @@ pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str) {
             diag.span_suggestion(
                 lit.span,
                 "if you mean to use a decimal constant, remove the `0` to avoid confusion",
-                lit_snip.trim_start_matches(|c| c == '_' || c == '0').to_string(),
-                Applicability::MaybeIncorrect,
-            );
-            diag.span_suggestion(
-                lit.span,
-                "if you mean to use an octal constant, use `0o`",
-                format!("0o{}", lit_snip.trim_start_matches(|c| c == '_' || c == '0')),
+                trimmed_lit_snip.to_string(),
                 Applicability::MaybeIncorrect,
             );
+            // do not advise to use octal form if the literal cannot be expressed in base 8.
+            if !lit_snip.contains(|c| c == '8' || c == '9') {
+                diag.span_suggestion(
+                    lit.span,
+                    "if you mean to use an octal constant, use `0o`",
+                    format!("0o{trimmed_lit_snip}"),
+                    Applicability::MaybeIncorrect,
+                );
+            }
         },
     );
 }