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

Several problems with clippy::manual_is_power_of_two #14461

Open
samueltardieu opened this issue Mar 24, 2025 · 0 comments · May be fixed by #14463
Open

Several problems with clippy::manual_is_power_of_two #14461

samueltardieu opened this issue Mar 24, 2025 · 0 comments · May be fixed by #14463
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@samueltardieu
Copy link
Contributor

samueltardieu commented Mar 24, 2025

Summary

The manual_is_power_of_two lint:

  • may suggest invalid code by not adding required parentheses
  • suggest invalid code by not checking the MSRV in a const context

Reproducer

I tried this code:

#![allow(unused)]
#![warn(clippy::manual_is_power_of_two)]
#[clippy::msrv = "1.0.0"]
const fn f(a: i32) -> bool {
    a as u32 & (a as u32 - 1) == 0
}
fn main() {}

I expected to see this happen: either no suggestion, or a suggestion to use (a as u32).count_ones() == 1.

Instead, this happened:

warning: manually reimplementing `is_power_of_two`
 --> u.rs:6:5
  |
6 |     a as u32 & (a as u32 - 1) == 0
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a as u32.is_power_of_two()`

Not only .is_power_of_two() cannot be used in a const context below Rust 1.32.0, but also the suggestion a as u32.is_power_of_two() lacks parentheses around a as u32.

Version

rustc 1.87.0-nightly (1aeb99d24 2025-03-19)
binary: rustc
commit-hash: 1aeb99d248e1b0069110cb03c6f1dcc7b36fd7f3
commit-date: 2025-03-19
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Additional Labels

@rustbot label +Isuggestion-causes-error
@rustbot claim

@samueltardieu samueltardieu added the C-bug Category: Clippy is not doing the correct thing label Mar 24, 2025
@samueltardieu samueltardieu linked a pull request Mar 24, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant