Skip to content

Commit

Permalink
Prevent linting on zero values
Browse files Browse the repository at this point in the history
  • Loading branch information
cgm616 committed Oct 25, 2020
1 parent 12f150e commit ebb71f2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 56 deletions.
13 changes: 4 additions & 9 deletions clippy_lints/src/xor_used_as_pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl LateLintPass<'_> for XorUsedAsPow {
if let ExprKind::Binary(op, left, right) = &expr.kind;
if BinOpKind::BitXor == op.node;
if let ExprKind::Lit(lhs) = &left.kind;
if let Some((lhs_val, lhs_type)) = unwrap_dec_int_literal(cx, lhs);
if let Some((lhs_val, _)) = unwrap_dec_int_literal(cx, lhs);
then {
match &right.kind {
ExprKind::Lit(rhs) => {
Expand Down Expand Up @@ -110,10 +110,10 @@ fn unwrap_dec_int_literal(cx: &LateContext<'_>, lit: &Lit) -> Option<(u128, LitI
if let Some(decoded) = NumericLiteral::from_lit_kind(&snippet, &lit.node);
if decoded.is_decimal();
then {
return Some((val, val_type));
Some((val, val_type))
}
else {
return None;
None
}
}
}
Expand All @@ -130,16 +130,11 @@ fn report_with_ident(cx: &LateContext<'_>, lhs: u128, rhs: &QPath<'_>, span: Spa
}

fn report_with_lit(cx: &LateContext<'_>, lhs: u128, rhs: u128, span: Span) {
if rhs > 127 {
if rhs > 127 || rhs == 0 {
return;
}
match lhs {
2 => {
if rhs == 0 {
report_pow_of_two(cx, format!("1"), span);
return;
}

let lhs_str = if rhs <= 31 {
"1_u32"
} else if rhs <= 63 {
Expand Down
7 changes: 1 addition & 6 deletions tests/ui/xor_used_as_pow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// run-rustfix
#![warn(clippy::xor_used_as_pow)]
#![allow(clippy::identity_op)]

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

// These should fail
let _ = 2 ^ 3;
let _ = 10 ^ 4;
let _ = 2 ^ 32;
let _ = 2 ^ 0;
let _ = 10 ^ 0;
{
let x = 15;
let _ = 2 ^ x;
let _ = 10 ^ x;
}
}
41 changes: 4 additions & 37 deletions tests/ui/xor_used_as_pow.stderr
Original file line number Diff line number Diff line change
@@ -1,52 +1,19 @@
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
--> $DIR/xor_used_as_pow.rs:24:13
|
LL | let _ = 2 ^ 3;
| ^^^^^ help: use a bitshift or constant instead: `1_u32 << 3`
|
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings`

error: `^` is not an exponentiation operator but appears to have been used as one
--> $DIR/xor_used_as_pow.rs:25:13
--> $DIR/xor_used_as_pow.rs:24:13
|
LL | let _ = 10 ^ 4;
| ^^^^^^
|
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings`
= help: did you mean to use .pow()?

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

error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
--> $DIR/xor_used_as_pow.rs:27:13
|
LL | let _ = 2 ^ 0;
| ^^^^^ help: use a bitshift or constant instead: `1`

error: `^` is not an exponentiation operator but appears to have been used as one
--> $DIR/xor_used_as_pow.rs:28:13
|
LL | let _ = 10 ^ 0;
| ^^^^^^
|
= help: did you mean to use .pow()?

error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
--> $DIR/xor_used_as_pow.rs:31:17
|
LL | let _ = 2 ^ x;
| ^^^^^ help: use a bitshift or constant instead: `1 << x`

error: `^` is not an exponentiation operator but appears to have been used as one
--> $DIR/xor_used_as_pow.rs:32:17
--> $DIR/xor_used_as_pow.rs:27:17
|
LL | let _ = 10 ^ x;
| ^^^^^^
|
= help: did you mean to use .pow()?

error: aborting due to 7 previous errors
error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ fn main() {
let _ = 10 ^ 0b0101; // rhs binary
let _ = 2 ^ 0o1; // rhs octal
let _ = 10 ^ -18; // negative rhs
let _ = 2 ^ 0; // zero rhs

// These should fail
let _ = 1_u32 << 3;
let _ = 10 ^ 4;
let _ = 1_u64 << 32;
let _ = 1;
let _ = 10 ^ 0;
{
let x = 15;
let _ = 1 << x;
let _ = 10 ^ x;
}
}
31 changes: 31 additions & 0 deletions tests/ui/xor_used_as_pow_fixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// run-rustfix
#![warn(clippy::xor_used_as_pow)]
#![allow(clippy::identity_op)]

// Should not be linted
#[allow(dead_code)]
enum E {
First = 1 ^ 8,
Second = 2 ^ 8,
Third = 3 ^ 8,
Tenth = 10 ^ 8,
}

fn main() {
// These should succeed:
let _ = 9 ^ 3; // lhs other than 2 or 10
let _ = 0x02 ^ 6; // lhs not decimal
let _ = 2 ^ 0x10; // rhs hexadecimal
let _ = 10 ^ 0b0101; // rhs binary
let _ = 2 ^ 0o1; // rhs octal
let _ = 10 ^ -18; // negative rhs
let _ = 2 ^ 0; // zero rhs

// These should fail
let _ = 2 ^ 3;
let _ = 2 ^ 32;
{
let x = 15;
let _ = 2 ^ x;
}
}
22 changes: 22 additions & 0 deletions tests/ui/xor_used_as_pow_fixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
--> $DIR/xor_used_as_pow_fixable.rs:25:13
|
LL | let _ = 2 ^ 3;
| ^^^^^ help: use a bitshift or constant instead: `1_u32 << 3`
|
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings`

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

error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
--> $DIR/xor_used_as_pow_fixable.rs:29:17
|
LL | let _ = 2 ^ x;
| ^^^^^ help: use a bitshift or constant instead: `1 << x`

error: aborting due to 3 previous errors

0 comments on commit ebb71f2

Please sign in to comment.