Insert parentheses as required

Since the fixed expression is used as a receiver for
`.is_power_of_two()`, it may require parentheses.
This commit is contained in:
Samuel Tardieu 2025-03-24 18:57:58 +01:00
parent bd2cb6a0af
commit 89385c135d
4 changed files with 16 additions and 2 deletions

View file

@ -69,7 +69,7 @@ fn build_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
expr.span,
"manually reimplementing `is_power_of_two`",
"consider using `.is_power_of_two()`",
format!("{snippet}.is_power_of_two()"),
format!("{}.is_power_of_two()", snippet.maybe_paren()),
applicability,
);
}

View file

@ -23,4 +23,8 @@ fn main() {
// is_power_of_two only works for unsigned integers
let _ = b.count_ones() == 1;
let _ = b & (b - 1) == 0;
let i: i32 = 3;
let _ = (i as u32).is_power_of_two();
//~^ manual_is_power_of_two
}

View file

@ -23,4 +23,8 @@ fn main() {
// is_power_of_two only works for unsigned integers
let _ = b.count_ones() == 1;
let _ = b & (b - 1) == 0;
let i: i32 = 3;
let _ = i as u32 & (i as u32 - 1) == 0;
//~^ manual_is_power_of_two
}

View file

@ -37,5 +37,11 @@ error: manually reimplementing `is_power_of_two`
LL | let _ = 0 == (a - 1) & a;
| ^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a.is_power_of_two()`
error: aborting due to 6 previous errors
error: manually reimplementing `is_power_of_two`
--> tests/ui/manual_is_power_of_two.rs:28:13
|
LL | let _ = i as u32 & (i as u32 - 1) == 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `(i as u32).is_power_of_two()`
error: aborting due to 7 previous errors