Skip to content

Commit 5818225

Browse files
committed
Auto merge of #11255 - blyxyas:fix-perf-sus_xor_used_as_pow, r=xFrednet
Fix `suspicious_xor_used_as_pow.rs` performance The original `suspicious_xor_used_as_pow` lint had poor performance, so I fixed that + a little refactor so that module is readable. **107 millis. -> 106 millis.** Using `SPEEDTEST` on Rust's VMs fix #11060 changelog: [`suspicious_xor_used_as_pow`]: Improve performance by 0.934%
2 parents 1eb254e + 3fb8441 commit 5818225

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed
+26-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
12
use clippy_utils::numeric_literal::NumericLiteral;
2-
use clippy_utils::source::snippet_with_context;
3+
use clippy_utils::source::snippet;
4+
use rustc_ast::LitKind;
35
use rustc_errors::Applicability;
46
use rustc_hir::{BinOpKind, Expr, ExprKind};
57
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -28,27 +30,29 @@ declare_lint_pass!(ConfusingXorAndPow => [SUSPICIOUS_XOR_USED_AS_POW]);
2830

2931
impl LateLintPass<'_> for ConfusingXorAndPow {
3032
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
31-
if !in_external_macro(cx.sess(), expr.span) &&
32-
let ExprKind::Binary(op, left, right) = &expr.kind &&
33-
op.node == BinOpKind::BitXor &&
34-
left.span.ctxt() == right.span.ctxt() &&
35-
let ExprKind::Lit(lit_left) = &left.kind &&
36-
let ExprKind::Lit(lit_right) = &right.kind &&
37-
let snip_left = snippet_with_context(cx, lit_left.span, lit_left.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) &&
38-
let snip_right = snippet_with_context(cx, lit_right.span, lit_right.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) &&
39-
let Some(left_val) = NumericLiteral::from_lit_kind(&snip_left.0, &lit_left.node) &&
40-
let Some(right_val) = NumericLiteral::from_lit_kind(&snip_right.0, &lit_right.node) &&
41-
left_val.is_decimal() &&
42-
right_val.is_decimal() {
43-
clippy_utils::diagnostics::span_lint_and_sugg(
44-
cx,
45-
SUSPICIOUS_XOR_USED_AS_POW,
46-
expr.span,
47-
"`^` is not the exponentiation operator",
48-
"did you mean to write",
49-
format!("{}.pow({})", left_val.format(), right_val.format()),
50-
Applicability::MaybeIncorrect,
51-
);
33+
if !in_external_macro(cx.sess(), expr.span)
34+
&& let ExprKind::Binary(op, left, right) = &expr.kind
35+
&& op.node == BinOpKind::BitXor
36+
&& left.span.ctxt() == right.span.ctxt()
37+
&& let ExprKind::Lit(lit_left) = &left.kind
38+
&& let ExprKind::Lit(lit_right) = &right.kind
39+
&& matches!(lit_right.node, LitKind::Int(..) | LitKind::Float(..))
40+
&& matches!(lit_left.node, LitKind::Int(..) | LitKind::Float(..))
41+
&& NumericLiteral::from_lit_kind(&snippet(cx, lit_right.span, ".."), &lit_right.node).is_some_and(|x| x.is_decimal())
42+
{
43+
span_lint_and_sugg(
44+
cx,
45+
SUSPICIOUS_XOR_USED_AS_POW,
46+
expr.span,
47+
"`^` is not the exponentiation operator",
48+
"did you mean to write",
49+
format!(
50+
"{}.pow({})",
51+
lit_left.node,
52+
lit_right.node
53+
),
54+
Applicability::MaybeIncorrect,
55+
);
5256
}
5357
}
5458
}

tests/ui/suspicious_xor_used_as_pow.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ error: `^` is not the exponentiation operator
1010
--> $DIR/suspicious_xor_used_as_pow.rs:20:13
1111
|
1212
LL | let _ = 2i32 ^ 9i32;
13-
| ^^^^^^^^^^^ help: did you mean to write: `2_i32.pow(9_i32)`
13+
| ^^^^^^^^^^^ help: did you mean to write: `2i32.pow(9i32)`
1414

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

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

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

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

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

0 commit comments

Comments
 (0)