Skip to content

Conversation

samueltardieu
Copy link
Member

Fix #14461:

  • insert parentheses as required in suggestion
  • check MSRV before suggesting fix in const context
  • do not lint macro expansion result

Commits have been logically separated to facilitate review, and start with a refactoring (and simplification) of the existing code.

changelog: [manual_is_power_of_two]: insert parentheses as required in suggestion, check MSRV before suggesting fix in const context, do not lint macro expansion results

@rustbot
Copy link
Collaborator

rustbot commented Mar 24, 2025

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 24, 2025
@samueltardieu samueltardieu force-pushed the push-yqozmwtrqtqs branch 2 times, most recently from d0d7a23 to abde06c Compare March 25, 2025 17:23
@samueltardieu
Copy link
Member Author

r? clippy

@rustbot rustbot assigned Jarcho and unassigned blyxyas Apr 6, 2025
This is a refactoring of the existing code to remove repetitive code.
Since the fixed expression is used as a receiver for `.is_power_of_two()`, it may require parentheses.
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

Great improvements to the lint! I've marked some slight nits

);
/// Return the unsigned integer receiver of `.count_ones()`
fn count_ones_receiver<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
if let ExprKind::MethodCall(method_name, receiver, [], _) = expr.kind
Copy link
Member

Choose a reason for hiding this comment

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

This could also check for <int>::count_ones(x), it's a normal ExprKind::Call.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Done, and tested, in a new commit on top of the others.

if let ExprKind::Binary(op, lhs, rhs) = smaller.kind
&& !lhs.span.from_expansion()
&& !rhs.span.from_expansion()
&& op.node == BinOpKind::Sub
Copy link
Member

Choose a reason for hiding this comment

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

Could we move this op.node == BinOpKind::Sub to above the expansion checking?

Also, thinking about checking for expansion so many times, we could probably minimize some of these (as we check for expansions 3 times before linting)

Copy link
Member Author

Choose a reason for hiding this comment

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

Right! All checks are necessary, but I've factored them in through a helper function. This makes code easier to read. The modified commit is Do not lint result from macro expansion.

If parts of the expression comes from macro expansion, it may match an expression equivalent to `is_power_of_two()` by chance only.
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! ❤️ 🌈

@blyxyas blyxyas added this pull request to the merge queue Apr 14, 2025
Merged via the queue into rust-lang:master with commit 02764f6 Apr 14, 2025
11 checks passed
@samueltardieu samueltardieu deleted the push-yqozmwtrqtqs branch April 15, 2025 08:16
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

4 participants