-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add lint for confusing use of ^
instead of .pow
#9506
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @giraffate (or someone else) soon. Please see the contribution instructions for more information. |
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.
That lint really look useful, thanks for adding it!
All changes and suggestions made by @kraktus were commited (some in other commits) |
clippy_lints/src/renamed_lints.rs
Outdated
@@ -6,6 +6,7 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[ | |||
("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions"), | |||
("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions"), | |||
("clippy::box_vec", "clippy::box_collection"), | |||
("clippy::confusing_xor_and_pow", "clippy::suspicious_xor"), |
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.
This is a new lint, so it doesn't need renaming.
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.
Can this lint warn only the case where 2^x
and 10^y
? Its context is #4205 (comment).
#6182 would be helpful in implementing it.
clippy_lints/src/suspicious_xor.rs
Outdated
/// let x = 3_i32.pow(4); | ||
/// ``` | ||
#[clippy::version = "1.65.0"] | ||
pub SUSPICIOUS_XOR, |
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.
IMO it would be better to point out misuse as a power, so its lint name should include pow
. For example, suspicious_xor_as_pow
or xor_used_as_pow
from #6182.
I think the lint should warn for every decimal number, personally I think that the XOR operator is sufficiently rare that any use of it between two decimal numbers should give a warning. |
☔ The latest upstream changes (presumably #9511) made this pull request unmergeable. Please resolve the merge conflicts. |
Agreed, I think we should lint every |
I ran a lintcheck, but I cannot understand the logs. They don't seem to contain anything triggering this lint (the lint isn't even mentioned in |
IMO, it depends on the context because
As a first step, it would be better to cover only the |
I think warning for decimal literal would be fine, suggesting to use hex or bin instead for better clarity |
☔ The latest upstream changes (presumably #9516) made this pull request unmergeable. Please resolve the merge conflicts. |
We follow the no merge-commit policy, so could you rebase, not merge? |
Sorry about that. |
I've tried myself too to fix the suggestion without luck due to how the suggestion is built. In the end I think it's better not to lint in those case, at least at first. Sorry for the dead end. Made a PR against yours to get you back on track 👍 If you have more questions do not hesitate! |
☔ The latest upstream changes (presumably #9549) made this pull request unmergeable. Please resolve the merge conflicts. |
I've been having issues with Git lately but I think I've fixed them, I think I haven't merged anything |
Sorry for the late reviewing.
As a first step, I don't think we need to warn the case where |
tests/ui/suspicious_xor.stderr
Outdated
error: '^' is not the exponentiation operator | ||
--> $DIR/suspicious_xor.rs:22:13 | ||
| | ||
LL | let _ = 50i32 ^ 3i32; |
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.
I don't think this case should be warned. #9506 (review)
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.
I think 50 ^ 3 should be warned because, is really that common xoring 50 and 3? In my opinion the cases where this happens is as common as using a bitwise OR in an if statement. Both cases are just confusion with other operators in a vast majority of cases.
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.
It depends on contexts. If we warn any x^y
(x, y = literal integer
), the category should be restriction
or pedantic
.
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.
We’re only talking about linting literal using the decimal form which I’ve never seen used when knowingly xoring.
Message could suggest ‘if this is intentional, use the hexadecimal or binary form as this is more explicit’
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.
Message could suggest ‘if this is intentional, use the hexadecimal or binary form as this is more explicit’
Are you suggesting to replace the current message (did you mean to write: 50.pow(3)
) or to have both?
@rustbot ready |
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.
Overall looks good, thanks! I made small comments.
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.
Could you squash commits?
Do you mean squashing all commits? Sorry but I've never squashed. So I don't know if I need to squash all, or do a selection. If I need to do a selection, which commits should I select? |
Hey, you should look at git interactive rebase. |
I've looked at it a little bit, but I don't know if I should squash all commits (including ones not made on this PR) from the first commit on this PR ( |
Just those from the PR, otherwise you’ll have merging conflits |
The last two commits were me trying to revert the errors I did (Just wasted several hours trying to squash). |
☔ The latest upstream changes (presumably #9670) made this pull request unmergeable. Please resolve the merge conflicts. |
Given the history of your branch I believe the easiest option is just to copy-paste your change in a new branch |
I agree. |
So, should I create another fork, for another PR with just the final commit? |
You can create a new local branch on your fork based on the head master branch, and pushes it to this remote branch directly. |
Ok, the new branch ( |
|
Mmmmmm... Something is wrong, I cannot push because git says that everything is up to date, which makes sense because I haven't changed anything on Btw, sorry for being such an inconvenience, this is my first time contributing to a big repo |
It is because your
|
I think I did it, somehow. I would squash the second commit into the first, but it being a merge from the |
☔ The latest upstream changes (presumably #9690) made this pull request unmergeable. Please resolve the merge conflicts. |
To squash commits, I tried below. Now, * 9029aba99 (HEAD -> pr_9506) Merge pull request #3 from blyxyas/new2
|\
| * 8109ebb23 Just lint changes
|/
* 967f172e2 Auto merge of #9635 - smoelius:fix-9386-bug, r=Jarcho
* 967f172e2 (HEAD -> pr_9506) Auto merge of #9635 - smoelius:fix-9386-bug, r=Jarcho git status
On branch pr_9506
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: CHANGELOG.md
modified: clippy_lints/src/lib.register_lints.rs
modified: clippy_lints/src/lib.register_restriction.rs
modified: clippy_lints/src/lib.rs
modified: src/docs.rs
modified: tests/ui/eq_op.rs
modified: tests/ui/eq_op.stderr
Untracked files:
(use "git add <file>..." to include in what will be committed)
clippy_lints/src/suspicious_xor_used_as_pow.rs
src/docs/suspicious_xor_used_as_pow.txt
tests/ui/suspicious_xor_used_as_pow.rs
tests/ui/suspicious_xor_used_as_pow.stderr So, And, to resolve conflicts, |
Co-authored-by: NAKATA Takayuki <[email protected]>
@bors r+ Thanks for your patience! |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
fixes #4205
Adds a lint named [
confusing_xor_and_pow
], it warns the user whena ^ b
is used as the.pow()
function, it doesn't warn for Hex, Binary... etc.changelog: New lint: [
confusing_xor_and_pow
]