|
| 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] = &["Saturating", "String", "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 | + match expr.kind { |
| 40 | + hir::ExprKind::Call(local_expr, _) => self.is_allowed_ty(cx, local_expr), |
| 41 | + hir::ExprKind::Path(ref qpath) => self.manage_qpath(cx, qpath), |
| 42 | + hir::ExprKind::Struct(qpath, ..) => self.manage_qpath(cx, qpath), |
| 43 | + _ => false, |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + fn issue_lint(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) { |
| 48 | + span_lint(cx, ARITHMETIC, expr.span, "arithmetic detected"); |
| 49 | + self.expr_span = Some(expr.span); |
| 50 | + } |
| 51 | + |
| 52 | + /// The actual checker used by `is_allowed_ty`. |
| 53 | + fn manage_qpath(&self, cx: &LateContext<'_>, qpath: &hir::QPath<'_>) -> bool { |
| 54 | + let path = if let hir::QPath::Resolved(_, path) = qpath { |
| 55 | + if let hir::def::Res::Local(hid) = path.res |
| 56 | + && let parent_node_id = cx.tcx.hir().get_parent_node(hid) |
| 57 | + && let Some(hir::Node::Local(local)) = cx.tcx.hir().find(parent_node_id) |
| 58 | + && let Some(ty) = local.ty |
| 59 | + && let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind |
| 60 | + { |
| 61 | + path |
| 62 | + } |
| 63 | + else { |
| 64 | + path |
| 65 | + } |
| 66 | + } else if let hir::QPath::TypeRelative(ty, _) = qpath |
| 67 | + && let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind |
| 68 | + { |
| 69 | + path |
| 70 | + } |
| 71 | + else { |
| 72 | + return false; |
| 73 | + }; |
| 74 | + for segment in path.segments { |
| 75 | + if self.allowed.contains(segment.ident.as_str()) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + } |
| 79 | + false |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl<'tcx> LateLintPass<'tcx> for Arithmetic { |
| 84 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
| 85 | + if self.expr_span.is_some() { |
| 86 | + return; |
| 87 | + } |
| 88 | + if let Some(span) = self.const_span && span.contains(expr.span) { |
| 89 | + return; |
| 90 | + } |
| 91 | + match &expr.kind { |
| 92 | + hir::ExprKind::Binary(op, lhs, rhs) | hir::ExprKind::AssignOp(op, lhs, rhs) => { |
| 93 | + let ( |
| 94 | + hir::BinOpKind::Add |
| 95 | + | hir::BinOpKind::Sub |
| 96 | + | hir::BinOpKind::Mul |
| 97 | + | hir::BinOpKind::Div |
| 98 | + | hir::BinOpKind::Rem |
| 99 | + | hir::BinOpKind::Shl |
| 100 | + | hir::BinOpKind::Shr |
| 101 | + ) = op.node else { |
| 102 | + return; |
| 103 | + }; |
| 104 | + if self.is_allowed_ty(cx, lhs) || self.is_allowed_ty(cx, rhs) { |
| 105 | + return; |
| 106 | + } |
| 107 | + self.issue_lint(cx, expr); |
| 108 | + }, |
| 109 | + hir::ExprKind::Unary(hir::UnOp::Neg, _) => { |
| 110 | + // CTFE already takes care of things like `-1` that do not overflow. |
| 111 | + if constant_simple(cx, cx.typeck_results(), expr).is_none() { |
| 112 | + self.issue_lint(cx, expr); |
| 113 | + } |
| 114 | + }, |
| 115 | + _ => {}, |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { |
| 120 | + let body_owner = cx.tcx.hir().body_owner_def_id(body.id()); |
| 121 | + match cx.tcx.hir().body_owner_kind(body_owner) { |
| 122 | + hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => { |
| 123 | + let body_span = cx.tcx.def_span(body_owner); |
| 124 | + if let Some(span) = self.const_span && span.contains(body_span) { |
| 125 | + return; |
| 126 | + } |
| 127 | + self.const_span = Some(body_span); |
| 128 | + }, |
| 129 | + hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => {}, |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { |
| 134 | + let body_owner = cx.tcx.hir().body_owner(body.id()); |
| 135 | + let body_span = cx.tcx.hir().span(body_owner); |
| 136 | + if let Some(span) = self.const_span && span.contains(body_span) { |
| 137 | + return; |
| 138 | + } |
| 139 | + self.const_span = None; |
| 140 | + } |
| 141 | + |
| 142 | + fn check_expr_post(&mut self, _: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
| 143 | + if Some(expr.span) == self.expr_span { |
| 144 | + self.expr_span = None; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments