Skip to content

Commit fe17bb5

Browse files
committed
suspicious_leading_zero lint
1 parent e561499 commit fe17bb5

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ lint_suspicious_double_ref_clone =
778778
lint_suspicious_double_ref_deref =
779779
using `.deref()` on a double reference, which returns `{$ty}` instead of dereferencing the inner type
780780
781+
lint_suspicious_leading_zero = TODO
782+
.suggestion = TODO
783+
781784
lint_tail_expr_drop_order = these values and local bindings have significant drop implementation that will have a different drop order from that of Edition 2021
782785
.label = these values have significant drop implementation and will observe changes in drop order under Edition 2024
783786

compiler/rustc_lint/src/builtin.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::lints::{
6666
BuiltinTrivialBounds, BuiltinTypeAliasBounds, BuiltinUngatedAsyncFnTrackCaller,
6767
BuiltinUnpermittedTypeInit, BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub,
6868
BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub,
69-
BuiltinWhileTrue, InvalidAsmLabel,
69+
BuiltinWhileTrue, InvalidAsmLabel, SuspiciousLeadingZero,
7070
};
7171
use crate::nonstandard_style::{MethodLateContext, method_context};
7272
use crate::{
@@ -3035,3 +3035,33 @@ impl EarlyLintPass for SpecialModuleName {
30353035
}
30363036
}
30373037
}
3038+
3039+
declare_lint! {
3040+
/// TODO
3041+
pub SUSPICIOUS_LEADING_ZERO,
3042+
Warn,
3043+
"TODO",
3044+
}
3045+
3046+
declare_lint_pass!(LeadingZero => [SUSPICIOUS_LEADING_ZERO]);
3047+
3048+
impl EarlyLintPass for LeadingZero {
3049+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
3050+
if let ast::ExprKind::Lit(literal) = expr.kind
3051+
&& let token::Lit { kind: token::LitKind::Integer, symbol, suffix: _ } = literal
3052+
&& let s = symbol.as_str()
3053+
&& s.starts_with('0')
3054+
&& !["0x", "0b", "0o"].into_iter().any(|prefix| s.starts_with(prefix))
3055+
// Has some other digits after the leading 0 and all those digits are octal:
3056+
&& let Some(true) = s.bytes().skip(1).try_fold(false, |res, c| match c {
3057+
b'0'..=b'7' => Some(true),
3058+
b'8' | b'9' => None,
3059+
..=b'/' | b':'.. => Some(res),
3060+
})
3061+
{
3062+
let lit_span = expr.span;
3063+
let span = lit_span.with_hi(lit_span.lo() + BytePos(1));
3064+
cx.emit_span_lint(SUSPICIOUS_LEADING_ZERO, span, SuspiciousLeadingZero { span });
3065+
}
3066+
}
3067+
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ early_lint_methods!(
187187
UnusedDocComment: UnusedDocComment,
188188
Expr2024: Expr2024,
189189
Precedence: Precedence,
190+
LeadingZero: LeadingZero,
190191
]
191192
]
192193
);

compiler/rustc_lint/src/lints.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3061,3 +3061,10 @@ pub(crate) enum MutRefSugg {
30613061
#[derive(LintDiagnostic)]
30623062
#[diag(lint_unqualified_local_imports)]
30633063
pub(crate) struct UnqualifiedLocalImportsDiag {}
3064+
3065+
#[derive(LintDiagnostic)]
3066+
#[diag(lint_suspicious_leading_zero)]
3067+
pub(crate) struct SuspiciousLeadingZero {
3068+
#[suggestion(code = "0o", style = "verbose", applicability = "maybe-incorrect")]
3069+
pub span: Span,
3070+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![deny(suspicious_leading_zero)]
2+
3+
fn main() {
4+
let _ = 0111; //~ ERROR [suspicious_leading_zero]
5+
let _ = 0007; //~ ERROR [suspicious_leading_zero]
6+
let _ = 0008;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: TODO
2+
--> $DIR/suspicious-leading-zero.rs:4:13
3+
|
4+
LL | let _ = 0111;
5+
| ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/suspicious-leading-zero.rs:1:9
9+
|
10+
LL | #![deny(suspicious_leading_zero)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^
12+
help: TODO
13+
|
14+
LL | let _ = 0o111;
15+
| ~~
16+
17+
error: TODO
18+
--> $DIR/suspicious-leading-zero.rs:5:13
19+
|
20+
LL | let _ = 0007;
21+
| ^
22+
|
23+
help: TODO
24+
|
25+
LL | let _ = 0o007;
26+
| ~~
27+
28+
error: aborting due to 2 previous errors
29+

0 commit comments

Comments
 (0)