|
| 1 | +#![allow( |
| 2 | + // False positive |
| 3 | + clippy::match_same_arms |
| 4 | +)] |
| 5 | + |
| 6 | +use super::ARITHMETIC; |
| 7 | +use clippy_utils::{consts::constant_simple, diagnostics::span_lint}; |
| 8 | +use rustc_data_structures::fx::FxHashSet; |
| 9 | +use rustc_hir as hir; |
| 10 | +use rustc_lint::{LateContext, LateLintPass}; |
| 11 | +use rustc_session::impl_lint_pass; |
| 12 | +use rustc_span::source_map::Span; |
| 13 | + |
| 14 | +const HARD_CODED_ALLOWED: &[&str] = &["std::num::Saturating", "std::string::String", "std::num::Wrapping"]; |
| 15 | + |
| 16 | +#[derive(Debug)] |
| 17 | +pub struct Arithmetic { |
| 18 | + allowed: FxHashSet<String>, |
| 19 | + // Used to check whether expressions are constants, such as in enum discriminants and consts |
| 20 | + const_span: Option<Span>, |
| 21 | + expr_span: Option<Span>, |
| 22 | +} |
| 23 | + |
| 24 | +impl_lint_pass!(Arithmetic => [ARITHMETIC]); |
| 25 | + |
| 26 | +impl Arithmetic { |
| 27 | + #[must_use] |
| 28 | + pub fn new(mut allowed: FxHashSet<String>) -> Self { |
| 29 | + allowed.extend(HARD_CODED_ALLOWED.iter().copied().map(String::from)); |
| 30 | + Self { |
| 31 | + allowed, |
| 32 | + const_span: None, |
| 33 | + expr_span: None, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Checks if the given `expr` has any of the inner `allowed` elements. |
| 38 | + fn is_allowed_ty(&self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool { |
| 39 | + self.allowed.contains( |
| 40 | + cx.typeck_results() |
| 41 | + .expr_ty(expr) |
| 42 | + .to_string() |
| 43 | + .split('<') |
| 44 | + .next() |
| 45 | + .unwrap_or_default(), |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + fn issue_lint(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) { |
| 50 | + span_lint(cx, ARITHMETIC, expr.span, "arithmetic detected"); |
| 51 | + self.expr_span = Some(expr.span); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<'tcx> LateLintPass<'tcx> for Arithmetic { |
| 56 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
| 57 | + if self.expr_span.is_some() { |
| 58 | + return; |
| 59 | + } |
| 60 | + if let Some(span) = self.const_span && span.contains(expr.span) { |
| 61 | + return; |
| 62 | + } |
| 63 | + match &expr.kind { |
| 64 | + hir::ExprKind::Binary(op, lhs, rhs) | hir::ExprKind::AssignOp(op, lhs, rhs) => { |
| 65 | + let ( |
| 66 | + hir::BinOpKind::Add |
| 67 | + | hir::BinOpKind::Sub |
| 68 | + | hir::BinOpKind::Mul |
| 69 | + | hir::BinOpKind::Div |
| 70 | + | hir::BinOpKind::Rem |
| 71 | + | hir::BinOpKind::Shl |
| 72 | + | hir::BinOpKind::Shr |
| 73 | + ) = op.node else { |
| 74 | + return; |
| 75 | + }; |
| 76 | + if self.is_allowed_ty(cx, lhs) || self.is_allowed_ty(cx, rhs) { |
| 77 | + return; |
| 78 | + } |
| 79 | + self.issue_lint(cx, expr); |
| 80 | + }, |
| 81 | + hir::ExprKind::Unary(hir::UnOp::Neg, _) => { |
| 82 | + // CTFE already takes care of things like `-1` that do not overflow. |
| 83 | + if constant_simple(cx, cx.typeck_results(), expr).is_none() { |
| 84 | + self.issue_lint(cx, expr); |
| 85 | + } |
| 86 | + }, |
| 87 | + _ => {}, |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { |
| 92 | + let body_owner = cx.tcx.hir().body_owner_def_id(body.id()); |
| 93 | + match cx.tcx.hir().body_owner_kind(body_owner) { |
| 94 | + hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => { |
| 95 | + let body_span = cx.tcx.def_span(body_owner); |
| 96 | + if let Some(span) = self.const_span && span.contains(body_span) { |
| 97 | + return; |
| 98 | + } |
| 99 | + self.const_span = Some(body_span); |
| 100 | + }, |
| 101 | + hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => {}, |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { |
| 106 | + let body_owner = cx.tcx.hir().body_owner(body.id()); |
| 107 | + let body_span = cx.tcx.hir().span(body_owner); |
| 108 | + if let Some(span) = self.const_span && span.contains(body_span) { |
| 109 | + return; |
| 110 | + } |
| 111 | + self.const_span = None; |
| 112 | + } |
| 113 | + |
| 114 | + fn check_expr_post(&mut self, _: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
| 115 | + if Some(expr.span) == self.expr_span { |
| 116 | + self.expr_span = None; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments