Skip to content

Commit 608ba73

Browse files
committed
Prevent linting on zero values
1 parent 56e6588 commit 608ba73

6 files changed

+63
-56
lines changed

clippy_lints/src/xor_used_as_pow.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl LateLintPass<'_> for XorUsedAsPow {
5555
if let ExprKind::Binary(op, left, right) = &expr.kind;
5656
if BinOpKind::BitXor == op.node;
5757
if let ExprKind::Lit(lhs) = &left.kind;
58-
if let Some((lhs_val, lhs_type)) = unwrap_dec_int_literal(cx, lhs);
58+
if let Some((lhs_val, _)) = unwrap_dec_int_literal(cx, lhs);
5959
then {
6060
match &right.kind {
6161
ExprKind::Lit(rhs) => {
@@ -110,10 +110,10 @@ fn unwrap_dec_int_literal(cx: &LateContext<'_>, lit: &Lit) -> Option<(u128, LitI
110110
if let Some(decoded) = NumericLiteral::from_lit_kind(&snippet, &lit.node);
111111
if decoded.is_decimal();
112112
then {
113-
return Some((val, val_type));
113+
Some((val, val_type))
114114
}
115115
else {
116-
return None;
116+
None
117117
}
118118
}
119119
}
@@ -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.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// run-rustfix
21
#![warn(clippy::xor_used_as_pow)]
32
#![allow(clippy::identity_op)]
43

@@ -19,16 +18,12 @@ fn main() {
1918
let _ = 10 ^ 0b0101; // rhs binary
2019
let _ = 2 ^ 0o1; // rhs octal
2120
let _ = 10 ^ -18; // negative rhs
21+
let _ = 2 ^ 0; // zero rhs
2222

2323
// These should fail
24-
let _ = 2 ^ 3;
2524
let _ = 10 ^ 4;
26-
let _ = 2 ^ 32;
27-
let _ = 2 ^ 0;
28-
let _ = 10 ^ 0;
2925
{
3026
let x = 15;
31-
let _ = 2 ^ x;
3227
let _ = 10 ^ x;
3328
}
3429
}

tests/ui/xor_used_as_pow.stderr

+4-37
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,19 @@
1-
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
3-
|
4-
LL | let _ = 2 ^ 3;
5-
| ^^^^^ help: use a bitshift or constant instead: `1_u32 << 3`
6-
|
7-
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings`
8-
91
error: `^` is not an exponentiation operator but appears to have been used as one
10-
--> $DIR/xor_used_as_pow.rs:25:13
2+
--> $DIR/xor_used_as_pow.rs:24:13
113
|
124
LL | let _ = 10 ^ 4;
135
| ^^^^^^
146
|
7+
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings`
158
= help: did you mean to use .pow()?
169

17-
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
19-
|
20-
LL | let _ = 2 ^ 32;
21-
| ^^^^^^ help: use a bitshift or constant instead: `1_u64 << 32`
22-
23-
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
39-
|
40-
LL | let _ = 2 ^ x;
41-
| ^^^^^ help: use a bitshift or constant instead: `1 << x`
42-
4310
error: `^` is not an exponentiation operator but appears to have been used as one
44-
--> $DIR/xor_used_as_pow.rs:32:17
11+
--> $DIR/xor_used_as_pow.rs:27:17
4512
|
4613
LL | let _ = 10 ^ x;
4714
| ^^^^^^
4815
|
4916
= help: did you mean to use .pow()?
5017

51-
error: aborting due to 7 previous errors
18+
error: aborting due to 2 previous errors
5219

tests/ui/xor_used_as_pow.fixed renamed to tests/ui/xor_used_as_pow_fixable.fixed

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ 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;
25-
let _ = 10 ^ 4;
2626
let _ = 1_u64 << 32;
27-
let _ = 1;
28-
let _ = 10 ^ 0;
2927
{
3028
let x = 15;
3129
let _ = 1 << x;
32-
let _ = 10 ^ x;
3330
}
3431
}

tests/ui/xor_used_as_pow_fixable.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run-rustfix
2+
#![warn(clippy::xor_used_as_pow)]
3+
#![allow(clippy::identity_op)]
4+
5+
// Should not be linted
6+
#[allow(dead_code)]
7+
enum E {
8+
First = 1 ^ 8,
9+
Second = 2 ^ 8,
10+
Third = 3 ^ 8,
11+
Tenth = 10 ^ 8,
12+
}
13+
14+
fn main() {
15+
// These should succeed:
16+
let _ = 9 ^ 3; // lhs other than 2 or 10
17+
let _ = 0x02 ^ 6; // lhs not decimal
18+
let _ = 2 ^ 0x10; // rhs hexadecimal
19+
let _ = 10 ^ 0b0101; // rhs binary
20+
let _ = 2 ^ 0o1; // rhs octal
21+
let _ = 10 ^ -18; // negative rhs
22+
let _ = 2 ^ 0; // zero rhs
23+
24+
// These should fail
25+
let _ = 2 ^ 3;
26+
let _ = 2 ^ 32;
27+
{
28+
let x = 15;
29+
let _ = 2 ^ x;
30+
}
31+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
2+
--> $DIR/xor_used_as_pow_fixable.rs:25:13
3+
|
4+
LL | let _ = 2 ^ 3;
5+
| ^^^^^ help: use a bitshift or constant instead: `1_u32 << 3`
6+
|
7+
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings`
8+
9+
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
10+
--> $DIR/xor_used_as_pow_fixable.rs:26:13
11+
|
12+
LL | let _ = 2 ^ 32;
13+
| ^^^^^^ help: use a bitshift or constant instead: `1_u64 << 32`
14+
15+
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
16+
--> $DIR/xor_used_as_pow_fixable.rs:29:17
17+
|
18+
LL | let _ = 2 ^ x;
19+
| ^^^^^ help: use a bitshift or constant instead: `1 << x`
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)