]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/suspicious_xor_used_as_pow.rs
Rollup merge of #105123 - BlackHoleFox:fixing-the-macos-deployment, r=oli-obk
[rust.git] / src / tools / clippy / clippy_lints / src / suspicious_xor_used_as_pow.rs
1 use clippy_utils::{numeric_literal::NumericLiteral, source::snippet_with_context};
2 use rustc_errors::Applicability;
3 use rustc_hir::{BinOpKind, Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass, LintContext};
5 use rustc_middle::lint::in_external_macro;
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7
8 declare_clippy_lint! {
9     /// ### What it does
10     /// Warns for a Bitwise XOR (`^`) operator being probably confused as a powering. It will not trigger if any of the numbers are not in decimal.
11     /// ### Why is this bad?
12     /// It's most probably a typo and may lead to unexpected behaviours.
13     /// ### Example
14     /// ```rust
15     /// let x = 3_i32 ^ 4_i32;
16     /// ```
17     /// Use instead:
18     /// ```rust
19     /// let x = 3_i32.pow(4);
20     /// ```
21     #[clippy::version = "1.66.0"]
22     pub SUSPICIOUS_XOR_USED_AS_POW,
23     restriction,
24     "XOR (`^`) operator possibly used as exponentiation operator"
25 }
26 declare_lint_pass!(ConfusingXorAndPow => [SUSPICIOUS_XOR_USED_AS_POW]);
27
28 impl LateLintPass<'_> for ConfusingXorAndPow {
29     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
30         if !in_external_macro(cx.sess(), expr.span) &&
31                 let ExprKind::Binary(op, left, right) = &expr.kind &&
32             op.node == BinOpKind::BitXor &&
33             left.span.ctxt() == right.span.ctxt() &&
34             let ExprKind::Lit(lit_left) = &left.kind &&
35             let ExprKind::Lit(lit_right) = &right.kind &&
36             let snip_left = snippet_with_context(cx, lit_left.span, lit_left.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) &&
37             let snip_right = snippet_with_context(cx, lit_right.span, lit_right.span.ctxt(), "..", &mut Applicability::MaybeIncorrect) &&
38             let Some(left_val) = NumericLiteral::from_lit_kind(&snip_left.0, &lit_left.node) &&
39             let Some(right_val) = NumericLiteral::from_lit_kind(&snip_right.0, &lit_right.node) &&
40                         left_val.is_decimal() &&
41                         right_val.is_decimal() {
42                                         clippy_utils::diagnostics::span_lint_and_sugg(
43                                                 cx,
44                                                 SUSPICIOUS_XOR_USED_AS_POW,
45                                                 expr.span,
46                                                 "`^` is not the exponentiation operator",
47                                                 "did you mean to write",
48                                                 format!("{}.pow({})", left_val.format(), right_val.format()),
49                                                 Applicability::MaybeIncorrect,
50                                             );
51                 }
52     }
53 }