Skip to content
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 unexpected unary precedence #121364

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Urgau
Copy link
Contributor

@Urgau Urgau commented Feb 20, 2024

This PR implements a lint against unexpected unary precedence with a literal and method calls.

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

-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.


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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 20, 2024
@rustbot
Copy link
Collaborator

rustbot commented Feb 20, 2024

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. label Feb 20, 2024
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Feb 25, 2024

☔ The latest upstream changes (presumably #120393) made this pull request unmergeable. Please resolve the merge conflicts.

@traviscross
Copy link
Contributor

@rustbot labels -I-lang-nominated

Let's remove the nomination here in favor of the one on #117161.

@rustbot rustbot removed the I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. label Mar 6, 2024
@traviscross
Copy link
Contributor

@rustbot labels +I-lang-nominated

Thanks to discussion with @Urgau, I now notice that this is in fact proposing a subset of #117161 and so should be separately considered (though still probably should be discussed in the same breath). Let's renominate on that basis.

@rustbot rustbot added the I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. label Mar 20, 2024
@scottmcm
Copy link
Member

I'm (unsurprisingly) a fan of having a lint like this.

For triage discussion: do we want it called unary like this, or maybe ambiguous_literal_precedence?

@apiraino apiraino added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"-NUM.method()" is parsed in surprising ways
8 participants