Skip to content

Commit 6ab11c8

Browse files
committed
Prevent linting on zero values
1 parent 56e6588 commit 6ab11c8

File tree

4 files changed

+9
-30
lines changed

4 files changed

+9
-30
lines changed

clippy_lints/src/xor_used_as_pow.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,11 @@ fn report_with_ident(cx: &LateContext<'_>, lhs: u128, rhs: &QPath<'_>, span: Spa
130130
}
131131

132132
fn report_with_lit(cx: &LateContext<'_>, lhs: u128, rhs: u128, span: Span) {
133-
if rhs > 127 {
133+
if rhs > 127 || rhs == 0 {
134134
return;
135135
}
136136
match lhs {
137137
2 => {
138-
if rhs == 0 {
139-
report_pow_of_two(cx, format!("1"), span);
140-
return;
141-
}
142-
143138
let lhs_str = if rhs <= 31 {
144139
"1_u32"
145140
} else if rhs <= 63 {

tests/ui/xor_used_as_pow.fixed

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ fn main() {
1919
let _ = 10 ^ 0b0101; // rhs binary
2020
let _ = 2 ^ 0o1; // rhs octal
2121
let _ = 10 ^ -18; // negative rhs
22+
let _ = 2 ^ 0; // zero rhs
2223

2324
// These should fail
2425
let _ = 1_u32 << 3;
2526
let _ = 10 ^ 4;
2627
let _ = 1_u64 << 32;
27-
let _ = 1;
28-
let _ = 10 ^ 0;
2928
{
3029
let x = 15;
3130
let _ = 1 << x;

tests/ui/xor_used_as_pow.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ fn main() {
1919
let _ = 10 ^ 0b0101; // rhs binary
2020
let _ = 2 ^ 0o1; // rhs octal
2121
let _ = 10 ^ -18; // negative rhs
22+
let _ = 2 ^ 0; // zero rhs
2223

2324
// These should fail
2425
let _ = 2 ^ 3;
2526
let _ = 10 ^ 4;
2627
let _ = 2 ^ 32;
27-
let _ = 2 ^ 0;
28-
let _ = 10 ^ 0;
2928
{
3029
let x = 15;
3130
let _ = 2 ^ x;

tests/ui/xor_used_as_pow.stderr

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,38 @@
11
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
2-
--> $DIR/xor_used_as_pow.rs:24:13
2+
--> $DIR/xor_used_as_pow.rs:25:13
33
|
44
LL | let _ = 2 ^ 3;
55
| ^^^^^ help: use a bitshift or constant instead: `1_u32 << 3`
66
|
77
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings`
88

99
error: `^` is not an exponentiation operator but appears to have been used as one
10-
--> $DIR/xor_used_as_pow.rs:25:13
10+
--> $DIR/xor_used_as_pow.rs:26:13
1111
|
1212
LL | let _ = 10 ^ 4;
1313
| ^^^^^^
1414
|
1515
= help: did you mean to use .pow()?
1616

1717
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
18-
--> $DIR/xor_used_as_pow.rs:26:13
18+
--> $DIR/xor_used_as_pow.rs:27:13
1919
|
2020
LL | let _ = 2 ^ 32;
2121
| ^^^^^^ help: use a bitshift or constant instead: `1_u64 << 32`
2222

2323
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
24-
--> $DIR/xor_used_as_pow.rs:27:13
25-
|
26-
LL | let _ = 2 ^ 0;
27-
| ^^^^^ help: use a bitshift or constant instead: `1`
28-
29-
error: `^` is not an exponentiation operator but appears to have been used as one
30-
--> $DIR/xor_used_as_pow.rs:28:13
31-
|
32-
LL | let _ = 10 ^ 0;
33-
| ^^^^^^
34-
|
35-
= help: did you mean to use .pow()?
36-
37-
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
38-
--> $DIR/xor_used_as_pow.rs:31:17
24+
--> $DIR/xor_used_as_pow.rs:30:17
3925
|
4026
LL | let _ = 2 ^ x;
4127
| ^^^^^ help: use a bitshift or constant instead: `1 << x`
4228

4329
error: `^` is not an exponentiation operator but appears to have been used as one
44-
--> $DIR/xor_used_as_pow.rs:32:17
30+
--> $DIR/xor_used_as_pow.rs:31:17
4531
|
4632
LL | let _ = 10 ^ x;
4733
| ^^^^^^
4834
|
4935
= help: did you mean to use .pow()?
5036

51-
error: aborting due to 7 previous errors
37+
error: aborting due to 5 previous errors
5238

0 commit comments

Comments
 (0)