rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.27k stars 12.71k forks source link

Lint against leading 0 in integer literals #33448

Open huonw opened 8 years ago

huonw commented 8 years ago

A literal like 0111 looks like an octal literal in C, but is actually decimal in Rust (octal would be0o111). This is a footgun, and so seems like something that could be trivially checked for.

(I suppose this could be a "clippy" lint, but that seems like it would lose most of the benefit: it seems to me that most people who encounter this will be just starting out, not invested in running external, third-party commands using nightly.)

oli-obk commented 8 years ago

There are lots of lints in clippy that would most help new rustaceans. If there were a process to move lints to rustc as long as they have proven themselves as non-opinionated and false-positive-free, then a lot of people could start benefiting from clippy's lints.

oli-obk commented 8 years ago

One thing speaking against doing this in clippy is that this specific lint needs to be done during parsing, because the ast does not get the string representation of a literal, but only a numeric representation. Clippy would have to use the span to obtain the text and then act on the text.

Alternatively the ast could be changed to represent the original text, and ast -> hir lowering could extract the numerical representation.

oli-obk commented 8 years ago

cc @Manishearth @llogiq

llogiq commented 8 years ago

I don't see a problem with looking at the span of a literal, so we could well develop the lint in clippy, and move it to rustc later.

AFAIR, the core team has expressed they don't want to increase the number of included lints to keep it manageable (and probably also to keep compile time from ballooning :smile:). The longer we go without merging lints into rustc, the more I think of clippy as -Wall for Rust...

Manishearth commented 8 years ago

Works in clippy. There are a lot of lints in clippy specifically for newcomers and stuff. That was in fact the original scope/goal, but it has expanded since then.

Also, zero-setup cargo install clippy is happening soon, so I suspect clippy to gain more use amognst newcomers.

therealbstern commented 7 years ago

I'm in favor of this being a compile time warning that could be disabled with #allow, just like the dead_code warning. I think this will bite a lot of newcomers who won't know to run clippy.

Nicholas-Baron commented 3 years ago

I am going to assume that since this topic has had little discussion for around 3-4 years and the issue still persists (see this playground), that it has been "agreed to by silence".

Two locations in the rustc_parse crate seem to be good places to try implementing this warning. 0o191 errors with an 'invalid digit' error, produced here (lexer/mod.rs). However, there is also an 'invalid suffix' error, produced in a different file (parser/expr.rs).

The main questions to answer is: Where should this warning be emitted? Ideally, it should be produced

  1. Before rustc "loses" knowledge of the raw characters of the token
  2. Where other invalid numbers (like 0o191 or 0xG00D) are caught
GrigorenkoPV commented 1 month ago

@rustbot claim