-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Warn about C-style octal literals #131309
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
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 |
f41dbfa to
e004aff
Compare
|
☔ The latest upstream changes (presumably #119220) made this pull request unmergeable. Please resolve the merge conflicts. |
e004aff to
15a539a
Compare
|
☔ The latest upstream changes (presumably #139983) made this pull request unmergeable. Please resolve the merge conflicts. |
15a539a to
c724951
Compare
|
+1 for a narrowly-scoped lint that only fires on patterns that look like file modes and other very common uses of octal. For that I think the upside (likelihood times benefit) of finding bugs far exceeds the downside (likelihood times inconvenience) of adding a blanket allow if a false positive slaps you and you don't like it. I don't see why we would also have an allow-by-default lint that blanket warns on leading zeros. If we have the narrowly scoped version, that starts to sound more like a clippy lint in the style category. |
|
☔ The latest upstream changes (presumably #141044) made this pull request unmergeable. Please resolve the merge conflicts. |
c724951 to
541333f
Compare
|
☔ The latest upstream changes (presumably #141343) made this pull request unmergeable. Please resolve the merge conflicts. |
541333f to
110b94c
Compare
|
In the interest of getting this closed out, in looking around GitHub for uses of If we scoped this down to linting against @rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
|
@traviscross There are a number of functions in the ecosystem that will want similar protection. Could we add some kind of |
|
Yes, it is interesting to think how we might generalize this further. Probably -- again in the interest of getting this closed out with a Pareto win -- I'd prefer to just land the lint with a fixed list first and then think how we might extend it. In searching around, there seemed a fairly finite number of ecosystem functions that would benefit from this, mostly from the It occurs to me also that in these cases where we know the function is expecting a mode we could look for decimal literals matching a certain pattern even without leading zeros; e.g. Then we could tackle a proposal for something like |
|
☔ The latest upstream changes (presumably #138677) made this pull request unmergeable. Please resolve the merge conflicts. |
110b94c to
d6631b0
Compare
|
☔ The latest upstream changes (presumably #142901) made this pull request unmergeable. Please resolve the merge conflicts. |
d6631b0 to
ac23a6b
Compare
|
☔ The latest upstream changes (presumably #144740) made this pull request unmergeable. Please resolve the merge conflicts. |
Uplifts
clippy::zero_prefixed_literalasleading_zeros_in_decimal_literals, warn-by-default.The
leading_zeros_in_decimal_literalslint 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
0oprefix 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 == 0o123while123 == 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