Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zero_prefixed_literal] Do not advise to use octal form if not possible #9652

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions clippy_lints/src/misc_early/zero_prefixed_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
);
}
},
);
}
7 changes: 7 additions & 0 deletions tests/ui/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ fn main() {
let ok26 = 0x6_A0_BF;
let ok27 = 0b1_0010_0101;
}

fn issue9651() {
// lint but octal form is not possible here
let _ = 08;
let _ = 09;
let _ = 089;
}
35 changes: 34 additions & 1 deletion tests/ui/literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,38 @@ error: digits of hex or binary literal not grouped by four
LL | let fail25 = 0b01_100_101;
| ^^^^^^^^^^^^ 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