-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Implement lint against ambiguous negative literals #121364
Conversation
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
176a44a
to
144d1bb
Compare
This comment was marked as outdated.
This comment was marked as outdated.
I'm (unsurprisingly) a fan of having a lint like this. For triage discussion: do we want it called |
We discussed this in the lang triage meeting today. There was some unwillingness to phrase this in terms of unary operators, in the sense that there's lots of potentially-ambiguous things -- There was broad agreement that the confusing case of negation with literals is particularly footgunny, however. So the proposed decision is the following:
@rfcbot fcp merge I'm checking tyler and josh's boxes per their in-meeting request. (I think that's already the implemented semantic, just a different name?) |
Team member @scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
@rfcbot reviewed |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@bors r+ |
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#121364 (Implement lint against ambiguous negative literals) - rust-lang#127300 (Fix connect timeout for non-linux targets, read readiness of socket connection, Read readiness to detect errors. `Fixes rust-lang#127018`) - rust-lang#128138 (`#[naked]`: use an allowlist for allowed options on `asm!` in naked functions) - rust-lang#128158 (std: unsafe-wrap personality::gcc) - rust-lang#128171 (Make sure that args are compatible in `resolve_associated_item`) - rust-lang#128172 (Don't ICE if HIR and middle types disagree in borrowck error reporting) - rust-lang#128173 (Remove crashes for misuses of intrinsics) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#121364 - Urgau:unary_precedence, r=compiler-errors Implement lint against ambiguous negative literals This PR implements a lint against ambiguous negative literals with a literal and method calls right after it. ## `ambiguous_negative_literals` (deny-by-default) The `ambiguous_negative_literals` lint checks for cases that are confusing between a negative literal and a negation that's not part of the literal. ### Example ```rust,compile_fail -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 ``` ### Explanation Method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs. <details> <summary>Old proposed lint</summary> ## `ambiguous_unary_precedence` (deny-by-default) The `ambiguous_unary_precedence` lint checks for use the negative unary operator with a literal and method calls. ### Example ```rust -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 ``` ### Explanation Unary operations take precedence on binary operations and method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs. </details> ----- Note: This is a strip down version of rust-lang#117161, without the binary op precedence. Fixes rust-lang#117155 `@rustbot` labels +I-lang-nominated cc `@scottmcm` r? compiler
This completely broke Jiff once the change landed. Because it specifically supports negating spans like |
This was bad timing. The lang team just stabilized (in nightly) a new deny-by-default lint, named `ambiguous_negative_literals`, which triggers an error for things like `-1.hour()`. While such things can be confusingly ambiguous in some cases, in Jiff, `-1.hour()`, `(-1).hour()` and `-(1.hour())` are all, very intentionally, precisely equivalent. For now we just `allow` the lint. If the lint stays, we'll likely want to recommend allowing it in the Jiff docs. See: rust-lang/rust#121364
This was bad timing. The lang team just stabilized (in nightly) a new deny-by-default lint, named `ambiguous_negative_literals`, which triggers an error for things like `-1.hour()`. While such things can be confusingly ambiguous in some cases, in Jiff, `-1.hour()`, `(-1).hour()` and `-(1.hour())` are all, very intentionally, precisely equivalent. For now we just `allow` the lint. If the lint stays, we'll likely want to recommend allowing it in the Jiff docs. See: rust-lang/rust#121364
This was bad timing. The lang team just stabilized (in nightly) a new deny-by-default lint, named `ambiguous_negative_literals`, which triggers an error for things like `-1.hour()`. While such things can be confusingly ambiguous in some cases, in Jiff, `-1.hour()`, `(-1).hour()` and `-(1.hour())` are all, very intentionally, precisely equivalent. For now we just `allow` the lint. If the lint stays, we'll likely want to recommend allowing it in the Jiff docs. See: rust-lang/rust#121364
This was bad timing. The lang team just stabilized (in nightly) a new deny-by-default lint, named `ambiguous_negative_literals`, which triggers an error for things like `-1.hour()`. While such things can be confusingly ambiguous in some cases, in Jiff, `-1.hour()`, `(-1).hour()` and `-(1.hour())` are all, very intentionally, precisely equivalent. For now we just `allow` the lint. If the lint stays, we'll likely want to recommend allowing it in the Jiff docs. See: rust-lang/rust#121364
For info: T-lang decided to temporarily switch the lint to |
…-errors Implement lint against ambiguous negative literals This PR implements a lint against ambiguous negative literals with a literal and method calls right after it. ## `ambiguous_negative_literals` (deny-by-default) The `ambiguous_negative_literals` lint checks for cases that are confusing between a negative literal and a negation that's not part of the literal. ### Example ```rust,compile_fail -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 ``` ### Explanation Method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs. <details> <summary>Old proposed lint</summary> ## `ambiguous_unary_precedence` (deny-by-default) The `ambiguous_unary_precedence` lint checks for use the negative unary operator with a literal and method calls. ### Example ```rust -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 ``` ### Explanation Unary operations take precedence on binary operations and method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs. </details> ----- Note: This is a strip down version of rust-lang#117161, without the binary op precedence. Fixes rust-lang#117155 `@rustbot` labels +I-lang-nominated cc `@scottmcm` r? compiler
This PR implements a lint against ambiguous negative literals with a literal and method calls right after it.
ambiguous_negative_literals
(deny-by-default)
The
ambiguous_negative_literals
lint checks for cases that are confusing between a negative literal and a negation that's not part of the literal.Example
Explanation
Method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs.
Old proposed lint
ambiguous_unary_precedence
(deny-by-default)
The
ambiguous_unary_precedence
lint checks for use the negative unary operator with a literal and method calls.Example
Explanation
Unary operations take precedence on binary operations and method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs.
Note: This is a strip down version of #117161, without the binary op precedence.
Fixes #117155
@rustbot labels +I-lang-nominated
cc @scottmcm
r? compiler