From 608ba7357f8bd88e71e6174d7d3ecb4e23961f08 Mon Sep 17 00:00:00 2001 From: cgm616 Date: Thu, 22 Oct 2020 18:15:29 -0400 Subject: [PATCH] Prevent linting on zero values --- clippy_lints/src/xor_used_as_pow.rs | 13 ++---- tests/ui/xor_used_as_pow.rs | 7 +--- tests/ui/xor_used_as_pow.stderr | 41 ++----------------- ...ow.fixed => xor_used_as_pow_fixable.fixed} | 5 +-- tests/ui/xor_used_as_pow_fixable.rs | 31 ++++++++++++++ tests/ui/xor_used_as_pow_fixable.stderr | 22 ++++++++++ 6 files changed, 63 insertions(+), 56 deletions(-) rename tests/ui/{xor_used_as_pow.fixed => xor_used_as_pow_fixable.fixed} (88%) create mode 100644 tests/ui/xor_used_as_pow_fixable.rs create mode 100644 tests/ui/xor_used_as_pow_fixable.stderr diff --git a/clippy_lints/src/xor_used_as_pow.rs b/clippy_lints/src/xor_used_as_pow.rs index b2e5837863ba..81fbe01240fd 100644 --- a/clippy_lints/src/xor_used_as_pow.rs +++ b/clippy_lints/src/xor_used_as_pow.rs @@ -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) => { @@ -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 } } } @@ -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 { diff --git a/tests/ui/xor_used_as_pow.rs b/tests/ui/xor_used_as_pow.rs index 2ba4d8cb3595..80d90f310803 100644 --- a/tests/ui/xor_used_as_pow.rs +++ b/tests/ui/xor_used_as_pow.rs @@ -1,4 +1,3 @@ -// run-rustfix #![warn(clippy::xor_used_as_pow)] #![allow(clippy::identity_op)] @@ -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; } } diff --git a/tests/ui/xor_used_as_pow.stderr b/tests/ui/xor_used_as_pow.stderr index 4441fdef94e9..77e7bc1221ac 100644 --- a/tests/ui/xor_used_as_pow.stderr +++ b/tests/ui/xor_used_as_pow.stderr @@ -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 diff --git a/tests/ui/xor_used_as_pow.fixed b/tests/ui/xor_used_as_pow_fixable.fixed similarity index 88% rename from tests/ui/xor_used_as_pow.fixed rename to tests/ui/xor_used_as_pow_fixable.fixed index eac4d1ba1353..26e32126a8a9 100644 --- a/tests/ui/xor_used_as_pow.fixed +++ b/tests/ui/xor_used_as_pow_fixable.fixed @@ -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; } } diff --git a/tests/ui/xor_used_as_pow_fixable.rs b/tests/ui/xor_used_as_pow_fixable.rs new file mode 100644 index 000000000000..9a020f8601b1 --- /dev/null +++ b/tests/ui/xor_used_as_pow_fixable.rs @@ -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; + } +} diff --git a/tests/ui/xor_used_as_pow_fixable.stderr b/tests/ui/xor_used_as_pow_fixable.stderr new file mode 100644 index 000000000000..64a612d7a72c --- /dev/null +++ b/tests/ui/xor_used_as_pow_fixable.stderr @@ -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 +