Skip to content

Commit

Permalink
Fix suspicious_xor_used_as_pow.rs performance
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Jul 29, 2023
1 parent 2973096 commit 7a8b9ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
33 changes: 17 additions & 16 deletions clippy_lints/src/suspicious_xor_used_as_pow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::numeric_literal::NumericLiteral;
use clippy_utils::source::snippet_with_context;
use clippy_utils::source::snippet;
use rustc_ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
Expand Down Expand Up @@ -34,21 +36,20 @@ impl LateLintPass<'_> for ConfusingXorAndPow {
left.span.ctxt() == right.span.ctxt() &&
let ExprKind::Lit(lit_left) = &left.kind &&
let ExprKind::Lit(lit_right) = &right.kind &&
let snip_left = snippet_with_context(cx, lit_left.span, lit_left.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) &&
let snip_right = snippet_with_context(cx, lit_right.span, lit_right.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) &&
let Some(left_val) = NumericLiteral::from_lit_kind(&snip_left.0, &lit_left.node) &&
let Some(right_val) = NumericLiteral::from_lit_kind(&snip_right.0, &lit_right.node) &&
left_val.is_decimal() &&
right_val.is_decimal() {
clippy_utils::diagnostics::span_lint_and_sugg(
cx,
SUSPICIOUS_XOR_USED_AS_POW,
expr.span,
"`^` is not the exponentiation operator",
"did you mean to write",
format!("{}.pow({})", left_val.format(), right_val.format()),
Applicability::MaybeIncorrect,
);
matches!(lit_right.node, LitKind::Int(..) | LitKind::Float(..)) &&
matches!(lit_left.node, LitKind::Int(..) | LitKind::Float(..)) &&
NumericLiteral::from_lit_kind(&snippet(cx, lit_right.span, ".."), &lit_right.node).is_some_and(|x| x.is_decimal())
{
span_lint_and_sugg(
cx,
SUSPICIOUS_XOR_USED_AS_POW,
expr.span,
"`^` is not the exponentiation operator",
"did you mean to write",
format!("{}.pow({})", lit_left
.node.to_string(), lit_right.node.to_string()),
Applicability::MaybeIncorrect,
);
}
}
}
10 changes: 5 additions & 5 deletions tests/ui/suspicious_xor_used_as_pow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ error: `^` is not the exponentiation operator
--> $DIR/suspicious_xor_used_as_pow.rs:20:13
|
LL | let _ = 2i32 ^ 9i32;
| ^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(9_i32)`
| ^^^^^^^^^^^ help: did you mean to write: `2i32.pow(9i32)`

error: `^` is not the exponentiation operator
--> $DIR/suspicious_xor_used_as_pow.rs:21:13
|
LL | let _ = 2i32 ^ 2i32;
| ^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(2_i32)`
| ^^^^^^^^^^^ help: did you mean to write: `2i32.pow(2i32)`

error: `^` is not the exponentiation operator
--> $DIR/suspicious_xor_used_as_pow.rs:22:13
|
LL | let _ = 50i32 ^ 3i32;
| ^^^^^^^^^^^^ help: did you mean to write: `50_i32.pow(3_i32)`
| ^^^^^^^^^^^^ help: did you mean to write: `50i32.pow(3i32)`

error: `^` is not the exponentiation operator
--> $DIR/suspicious_xor_used_as_pow.rs:23:13
|
LL | let _ = 5i32 ^ 8i32;
| ^^^^^^^^^^^ help: did you mean to write: `5_i32.pow(8_i32)`
| ^^^^^^^^^^^ help: did you mean to write: `5i32.pow(8i32)`

error: `^` is not the exponentiation operator
--> $DIR/suspicious_xor_used_as_pow.rs:24:13
|
LL | let _ = 2i32 ^ 32i32;
| ^^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(32_i32)`
| ^^^^^^^^^^^^ help: did you mean to write: `2i32.pow(32i32)`

error: `^` is not the exponentiation operator
--> $DIR/suspicious_xor_used_as_pow.rs:13:9
Expand Down

0 comments on commit 7a8b9ab

Please sign in to comment.