-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Warn about C-style octal literals #131309
base: master
Are you sure you want to change the base?
Warn about C-style octal literals #131309
Conversation
This comment has been minimized.
This comment has been minimized.
This comment was marked as resolved.
This comment was marked as resolved.
suspicious_leading_zero
lint for detecting C-style octalsfe17bb5
to
def1383
Compare
This comment has been minimized.
This comment has been minimized.
This comment was marked as resolved.
This comment was marked as resolved.
def1383
to
f70a324
Compare
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
f70a324
to
e7881ba
Compare
This comment has been minimized.
This comment has been minimized.
e7881ba
to
ea90417
Compare
This comment has been minimized.
This comment has been minimized.
ea90417
to
2bfd5cf
Compare
This comment has been minimized.
This comment has been minimized.
2bfd5cf
to
81b09ce
Compare
The Miri subtree was changed cc @rust-lang/miri |
src/tools/miri/tests/pass-dep/concurrency/tls_pthread_drop_order.rs
Outdated
Show resolved
Hide resolved
81b09ce
to
24e10ba
Compare
This looks fine to me. Let's nominate it for T-lang fcp (which could unfortunatly take months, due to T-lang backlog). I've taken the liberty to adjust the PR description to include the lint description as well, in order to clearly see what the lint does. Feel free to update it if you update the one in the PR. @rustbot labels -S-waiting-on-review +S-waiting-on-team +I-lang-nominated |
That seems like a clear win to me, since that's obviously not coming from some copy-pasting octal constants from C. In general, I'm strongly opposed to warning on any constant that isn't actually ambiguous. If it's not octal or it's interpreted the same in decimal and octal, any warning doesn't actually provide value. Personally, add me to the "skeptical about doing this always" camp. Especially with rustfmt being so common and how it hates vertical alignment, using zeros like this seems an entirely reasonable way for people to tabulate. In general, is this really a concern for anything other than |
f2ba593
to
7ce1ef1
Compare
I've relaxed the lint to not warn about numbers <10 and about literals with 8's or 9's in them. |
7ce1ef1
to
1ff80c4
Compare
077 is reasonably likely to be an octal mistake, intended to be the group and other permission bits. I would prefer to warn about every case that could be interpreted as octal and would have a different meaning. We could also exclude any literals containing underscores. I don't think it's unreasonable for people to have to allow a lint if they want to use zeroes for alignment. But also, it's easy to use rustfmt::skip and then align with spaces. |
This comment has been minimized.
This comment has been minimized.
Here's another thing. In C, I always found it mildly annoying you couldn't write decimals with leading zeros due to that weird octal literal rule. There may have been more times that I actually wanted to write leading zeros than that I wanted to write in octal. So it'd be kind of darkly ironic if, in Rust, having not made the same mistake, we'd go out of our way to tell people these are discouraged due to C's weird octal literal rule. In a way, we would be teaching new generations of people -- who might not ever learn C themselves -- about C's weird octal literal rule. |
1ff80c4
to
c3305be
Compare
This comment has been minimized.
This comment has been minimized.
@traviscross I can appreciate that. On the other hand, UNIX is the gift that keeps on giving, and octal lives on in permission bits, file modes, and (as a consequence) Git repository internals, among other places. Because of that, octal remains just common enough to be a potential hazard. If the consequence is that we need to write one I think the mitigation of allowing two-digit zero padding (e.g. for dates) will help greatly in reducing how often people encounter this as a "false" positive. For the remainder, I think there are enough C programmers or recovering C programmers in the world that we could be nice to them by not breaking their mental lexers when they read other people's code. :) |
I feel that targeting this lint specifically at unix permission bits would make sense -- something like `0[0-7][0-7][0-7]`.
…On Tue, Jan 14, 2025, at 1:53 AM, Josh Triplett wrote:
@traviscross <https://github.com/traviscross> I can appreciate that. On the other hand, UNIX is the gift that keeps on giving, and octal lives on in permission bits, file modes, and (as a consequence) Git repository internals, among other places. Because of that, octal remains *just* common enough to be a potential hazard.
If the consequence is that we need to write one `allow` if we actually *want* to use leading zeroes (or switch to using leading spaces with a `rustfmt::skip`), that seems reasonable. The failure mode of "my personal style produces a warning and I need to change it or add an `allow`" seems less hazardous than the failure mode of "my permissions are wrong in a way that's extremely difficult to spot when looking at the code".
—
Reply to this email directly, view it on GitHub <#131309 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AABF4ZSB7CZ2Q35Y2HFEJ2L2KSX6JAVCNFSM6AAAAABPNXCQFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKOBZGE3DCMRVGQ>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
To be honest you could probably go even narrower, like `0[75310][75310][75310]` or something. I feel like the vast majority of bits are something like `0755` and not arbitrary combinations. I think users would appreciate a narrowly tailored lint that can explain exactly why it is triggering.
…On Tue, Jan 14, 2025, at 5:24 AM, Niko Matsakis wrote:
I feel that targeting this lint specifically at unix permission bits would make sense -- something like `0[0-7][0-7][0-7]`.
On Tue, Jan 14, 2025, at 1:53 AM, Josh Triplett wrote:
>
>
> @traviscross <https://github.com/traviscross> I can appreciate that. On the other hand, UNIX is the gift that keeps on giving, and octal lives on in permission bits, file modes, and (as a consequence) Git repository internals, among other places. Because of that, octal remains *just* common enough to be a potential hazard.
>
> If the consequence is that we need to write one `allow` if we actually *want* to use leading zeroes (or switch to using leading spaces with a `rustfmt::skip`), that seems reasonable. The failure mode of "my personal style produces a warning and I need to change it or add an `allow`" seems less hazardous than the failure mode of "my permissions are wrong in a way that's extremely difficult to spot when looking at the code".
>
>
>
> —
> Reply to this email directly, view it on GitHub <#131309 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AABF4ZSB7CZ2Q35Y2HFEJ2L2KSX6JAVCNFSM6AAAAABPNXCQFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKOBZGE3DCMRVGQ>.
> You are receiving this because you were mentioned.Message ID: ***@***.***>
>
>
|
077 is also likely to be permissions, and 0100644 is likely to be a file mode. |
c3305be
to
eacb589
Compare
So, proposal: I think we should do two separate FCPs here. First, let's do an FCP for approving the lint, as allow-by-default. Then, separately, we can work on consensus for making it warn. |
I'm ok with the lint, but as an alternative, I think a lint described as detecting "unix file permissions and modes" (vs any use of leading 0) might be a better framing that would be more likely to be warn-by-default.
…On Tue, Jan 14, 2025, at 8:26 AM, Josh Triplett wrote:
So, proposal:
I think we should do two separate FCPs here. First, let's do an FCP for approving the lint, as allow-by-default. Then, separately, we can work on consensus for making it warn.
—
Reply to this email directly, view it on GitHub <#131309 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AABF4ZSNDG4B7WVZDH7NPZT2KUGBLAVCNFSM6AAAAABPNXCQFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKOBZHEYDONJYGQ>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Could @rust-lang/lang folks who don't object to having the lint as allow-by-default please check a box at #131309 (comment) ? When that's done we can do a separate PR and FCP for whether to warn by default, either with the current lint or one with additional scope reduction to specific number lengths. |
Does an allow-by-default rustc lint has better discoverability than a warn-by-default clippy lint? |
@rfcbot reviewed |
@rfcbot concern allow-by-default Per @joshtriplett's comment, the FCP has changed to allow-by-default, but the OP on the PR (and afaik the impl) is still warn-by-default. Filing this concern until those are updated. |
Regarding the lint, I think my ideal would probably be to have this lint as allow-by-default and a more narrowly targeted one as allow-by-default, with a "subtyping" relationship -- I guess this means a |
☔ The latest upstream changes (presumably #136070) made this pull request unmergeable. Please resolve the merge conflicts. |
eacb589
to
b410dcc
Compare
error: lint `clippy::zero_prefixed_literal` has been renamed to `leading_zeros_in_decimal_literals` | ||
--> tests/ui/literals.rs:5:9 | ||
| | ||
LL | #![warn(clippy::zero_prefixed_literal)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `leading_zeros_in_decimal_literals` | ||
| | ||
= note: `-D renamed-and-removed-lints` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(renamed_and_removed_lints)]` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests for the Clippy lint needs to be updated as well.
Uplifts
clippy::zero_prefixed_literal
asleading_zeros_in_decimal_literals
, warn-by-default.The
leading_zeros_in_decimal_literals
lint detects decimal integral literals with leading zeros.Example
Explanation
In some languages (including the infamous C language and most of its family), a leading zero marks an octal constant. In Rust however, a
0o
prefix is used instead.Thus, a leading zero can be confusing for both the writer and a reader.
In Rust:
prints
123
, while in C:prints
83
(as83 == 0o123
while123 == 0o173
).Notes
Compared to the Clippy lint, this will not warn if the literal is unambiguous, i.e. it has 8's or 9's in it (clearly not octal) or it is <10 (in this case the value does not depend on the base).
Closes #33448
r? @ghost