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

Add new lint manual_is_power_of_two #13327

Merged
merged 4 commits into from
Sep 6, 2024
Merged

Add new lint manual_is_power_of_two #13327

merged 4 commits into from
Sep 6, 2024

Conversation

Sour1emon
Copy link
Contributor

@Sour1emon Sour1emon commented Aug 31, 2024

Suggest using is_power_of_two() instead of the manual implementations for some basic cases

Part of #12894


changelog: new [manual_is_power_of_two] lint

@rustbot
Copy link
Collaborator

rustbot commented Aug 31, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 31, 2024
@bors
Copy link
Collaborator

bors commented Sep 3, 2024

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

&& let ExprKind::Lit(lit1) = right.kind
&& let LitKind::Int(Pu128(0), _) = lit1.node
{
let snippet = snippet_with_applicability(cx, left1.span, "..", &mut applicability);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might make sense combine sugg and span_lint_and_sugg here and 48-58 so change in one place if needed


let b = 4_i64;

// is_power_of_two only works for unsigned integers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good idea mentioned about this at lint doc as well

Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some duplication and the binary operations might be reversed. Either you implement the tests for both directions or you document the missing cases in the lint docs under # Known Problems and we can open an issue to amend the lint later. Both are fine.

clippy_lints/src/manual_is_power_of_two.rs Outdated Show resolved Hide resolved
tests/ui/manual_is_power_of_two.rs Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Sep 5, 2024

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

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Sep 6, 2024
@rustbot
Copy link
Collaborator

rustbot commented Sep 6, 2024

There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

You can start a rebase with the following commands:

$ # rebase
$ git pull --rebase https://github.com/rust-lang/rust-clippy.git master
$ git push --force-with-lease

The following commits are merge commits:

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Sep 6, 2024
Sour1emon and others added 4 commits September 5, 2024 18:38
Co-authored-by: Ruby Lazuli <general@patchmixolydic.com>
Co-authored-by: Ruby Lazuli <general@patchmixolydic.com>
Comment on lines +44 to +59
if let ExprKind::MethodCall(method_name, reciever, _, _) = left.kind
&& method_name.ident.as_str() == "count_ones"
&& let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind()
&& check_lit(right, 1)
{
build_sugg(cx, expr, reciever, &mut applicability);
}

// 1 == a.count_ones()
if let ExprKind::MethodCall(method_name, reciever, _, _) = right.kind
&& method_name.ident.as_str() == "count_ones"
&& let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind()
&& check_lit(left, 1)
{
build_sugg(cx, expr, reciever, &mut applicability);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can loop over [(left, right), (right, left)] to reduce the code duplication here and elsewhere.

@llogiq
Copy link
Contributor

llogiq commented Sep 6, 2024

Apart from a good deal of code duplication, this seems good to merge, we can refactor in a followup PR later.

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 6, 2024

📌 Commit f994797 has been approved by llogiq

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Sep 6, 2024

⌛ Testing commit f994797 with merge fb9913e...

@bors
Copy link
Collaborator

bors commented Sep 6, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing fb9913e to master...

@bors bors merged commit fb9913e into rust-lang:master Sep 6, 2024
11 checks passed
@Sour1emon Sour1emon deleted the master branch September 7, 2024 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants