Skip to content

Several problems with clippy::manual_is_power_of_two #14461

@samueltardieu

Description

@samueltardieu

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions