Skip to content

Commit 89385c1

Browse files
committed
Insert parentheses as required
Since the fixed expression is used as a receiver for `.is_power_of_two()`, it may require parentheses.
1 parent bd2cb6a commit 89385c1

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

clippy_lints/src/manual_is_power_of_two.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn build_sugg(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
6969
expr.span,
7070
"manually reimplementing `is_power_of_two`",
7171
"consider using `.is_power_of_two()`",
72-
format!("{snippet}.is_power_of_two()"),
72+
format!("{}.is_power_of_two()", snippet.maybe_paren()),
7373
applicability,
7474
);
7575
}

tests/ui/manual_is_power_of_two.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ fn main() {
2323
// is_power_of_two only works for unsigned integers
2424
let _ = b.count_ones() == 1;
2525
let _ = b & (b - 1) == 0;
26+
27+
let i: i32 = 3;
28+
let _ = (i as u32).is_power_of_two();
29+
//~^ manual_is_power_of_two
2630
}

tests/ui/manual_is_power_of_two.rs

+4
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ fn main() {
2323
// is_power_of_two only works for unsigned integers
2424
let _ = b.count_ones() == 1;
2525
let _ = b & (b - 1) == 0;
26+
27+
let i: i32 = 3;
28+
let _ = i as u32 & (i as u32 - 1) == 0;
29+
//~^ manual_is_power_of_two
2630
}

tests/ui/manual_is_power_of_two.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ error: manually reimplementing `is_power_of_two`
3737
LL | let _ = 0 == (a - 1) & a;
3838
| ^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `a.is_power_of_two()`
3939

40-
error: aborting due to 6 previous errors
40+
error: manually reimplementing `is_power_of_two`
41+
--> tests/ui/manual_is_power_of_two.rs:28:13
42+
|
43+
LL | let _ = i as u32 & (i as u32 - 1) == 0;
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.is_power_of_two()`: `(i as u32).is_power_of_two()`
45+
46+
error: aborting due to 7 previous errors
4147

0 commit comments

Comments
 (0)