mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
[zero_prefixed_literal
] Do not advise to use octal form if not possible
This commit is contained in:
parent
50f192f86a
commit
2e3342af4a
3 changed files with 52 additions and 8 deletions
|
@ -6,6 +6,7 @@ use rustc_lint::EarlyContext;
|
||||||
use super::ZERO_PREFIXED_LITERAL;
|
use super::ZERO_PREFIXED_LITERAL;
|
||||||
|
|
||||||
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str) {
|
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(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
ZERO_PREFIXED_LITERAL,
|
ZERO_PREFIXED_LITERAL,
|
||||||
|
@ -15,15 +16,18 @@ pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str) {
|
||||||
diag.span_suggestion(
|
diag.span_suggestion(
|
||||||
lit.span,
|
lit.span,
|
||||||
"if you mean to use a decimal constant, remove the `0` to avoid confusion",
|
"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(),
|
trimmed_lit_snip.to_string(),
|
||||||
Applicability::MaybeIncorrect,
|
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(
|
diag.span_suggestion(
|
||||||
lit.span,
|
lit.span,
|
||||||
"if you mean to use an octal constant, use `0o`",
|
"if you mean to use an octal constant, use `0o`",
|
||||||
format!("0o{}", lit_snip.trim_start_matches(|c| c == '_' || c == '0')),
|
format!("0o{trimmed_lit_snip}"),
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,3 +40,10 @@ fn main() {
|
||||||
let ok26 = 0x6_A0_BF;
|
let ok26 = 0x6_A0_BF;
|
||||||
let ok27 = 0b1_0010_0101;
|
let ok27 = 0b1_0010_0101;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue9651() {
|
||||||
|
// lint but octal form is not possible here
|
||||||
|
let _ = 08;
|
||||||
|
let _ = 09;
|
||||||
|
let _ = 089;
|
||||||
|
}
|
||||||
|
|
|
@ -135,5 +135,38 @@ error: digits of hex or binary literal not grouped by four
|
||||||
LL | let fail25 = 0b01_100_101;
|
LL | let fail25 = 0b01_100_101;
|
||||||
| ^^^^^^^^^^^^ help: consider: `0b0110_0101`
|
| ^^^^^^^^^^^^ help: consider: `0b0110_0101`
|
||||||
|
|
||||||
error: aborting due to 18 previous errors
|
error: this is a decimal constant
|
||||||
|
--> $DIR/literals.rs:46:13
|
||||||
|
|
|
||||||
|
LL | let _ = 08;
|
||||||
|
| ^^
|
||||||
|
|
|
||||||
|
help: if you mean to use a decimal constant, remove the `0` to avoid confusion
|
||||||
|
|
|
||||||
|
LL | let _ = 8;
|
||||||
|
| ~
|
||||||
|
|
||||||
|
error: this is a decimal constant
|
||||||
|
--> $DIR/literals.rs:47:13
|
||||||
|
|
|
||||||
|
LL | let _ = 09;
|
||||||
|
| ^^
|
||||||
|
|
|
||||||
|
help: if you mean to use a decimal constant, remove the `0` to avoid confusion
|
||||||
|
|
|
||||||
|
LL | let _ = 9;
|
||||||
|
| ~
|
||||||
|
|
||||||
|
error: this is a decimal constant
|
||||||
|
--> $DIR/literals.rs:48:13
|
||||||
|
|
|
||||||
|
LL | let _ = 089;
|
||||||
|
| ^^^
|
||||||
|
|
|
||||||
|
help: if you mean to use a decimal constant, remove the `0` to avoid confusion
|
||||||
|
|
|
||||||
|
LL | let _ = 89;
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: aborting due to 21 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue