|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
1 | 2 | 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; |
3 | 5 | use rustc_errors::Applicability;
|
4 | 6 | use rustc_hir::{BinOpKind, Expr, ExprKind};
|
5 | 7 | use rustc_lint::{LateContext, LateLintPass, LintContext};
|
@@ -28,27 +30,29 @@ declare_lint_pass!(ConfusingXorAndPow => [SUSPICIOUS_XOR_USED_AS_POW]);
|
28 | 30 |
|
29 | 31 | impl LateLintPass<'_> for ConfusingXorAndPow {
|
30 | 32 | 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 | + ); |
52 | 56 | }
|
53 | 57 | }
|
54 | 58 | }
|
0 commit comments