Skip to content

Commit 2e3342a

Browse files
committed
[zero_prefixed_literal] Do not advise to use octal form if not possible
1 parent 50f192f commit 2e3342a

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

clippy_lints/src/misc_early/zero_prefixed_literal.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_lint::EarlyContext;
66
use super::ZERO_PREFIXED_LITERAL;
77

88
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str) {
9+
let trimmed_lit_snip = lit_snip.trim_start_matches(|c| c == '_' || c == '0');
910
span_lint_and_then(
1011
cx,
1112
ZERO_PREFIXED_LITERAL,
@@ -15,15 +16,18 @@ pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str) {
1516
diag.span_suggestion(
1617
lit.span,
1718
"if you mean to use a decimal constant, remove the `0` to avoid confusion",
18-
lit_snip.trim_start_matches(|c| c == '_' || c == '0').to_string(),
19-
Applicability::MaybeIncorrect,
20-
);
21-
diag.span_suggestion(
22-
lit.span,
23-
"if you mean to use an octal constant, use `0o`",
24-
format!("0o{}", lit_snip.trim_start_matches(|c| c == '_' || c == '0')),
19+
trimmed_lit_snip.to_string(),
2520
Applicability::MaybeIncorrect,
2621
);
22+
// do not advise to use octal form if the literal cannot be expressed in base 8.
23+
if !lit_snip.contains(|c| c == '8' || c == '9') {
24+
diag.span_suggestion(
25+
lit.span,
26+
"if you mean to use an octal constant, use `0o`",
27+
format!("0o{trimmed_lit_snip}"),
28+
Applicability::MaybeIncorrect,
29+
);
30+
}
2731
},
2832
);
2933
}

tests/ui/literals.rs

+7
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ fn main() {
4040
let ok26 = 0x6_A0_BF;
4141
let ok27 = 0b1_0010_0101;
4242
}
43+
44+
fn issue9651() {
45+
// lint but octal form is not possible here
46+
let _ = 08;
47+
let _ = 09;
48+
let _ = 089;
49+
}

tests/ui/literals.stderr

+34-1
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,38 @@ error: digits of hex or binary literal not grouped by four
135135
LL | let fail25 = 0b01_100_101;
136136
| ^^^^^^^^^^^^ help: consider: `0b0110_0101`
137137

138-
error: aborting due to 18 previous errors
138+
error: this is a decimal constant
139+
--> $DIR/literals.rs:46:13
140+
|
141+
LL | let _ = 08;
142+
| ^^
143+
|
144+
help: if you mean to use a decimal constant, remove the `0` to avoid confusion
145+
|
146+
LL | let _ = 8;
147+
| ~
148+
149+
error: this is a decimal constant
150+
--> $DIR/literals.rs:47:13
151+
|
152+
LL | let _ = 09;
153+
| ^^
154+
|
155+
help: if you mean to use a decimal constant, remove the `0` to avoid confusion
156+
|
157+
LL | let _ = 9;
158+
| ~
159+
160+
error: this is a decimal constant
161+
--> $DIR/literals.rs:48:13
162+
|
163+
LL | let _ = 089;
164+
| ^^^
165+
|
166+
help: if you mean to use a decimal constant, remove the `0` to avoid confusion
167+
|
168+
LL | let _ = 89;
169+
| ~~
170+
171+
error: aborting due to 21 previous errors
139172

0 commit comments

Comments
 (0)