|
| 1 | +use clippy_utils::{consts::constant_simple, diagnostics::span_lint}; |
| 2 | +use rustc_data_structures::fx::FxHashSet; |
| 3 | +use rustc_hir as hir; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 6 | +use rustc_span::source_map::Span; |
| 7 | + |
| 8 | +const HARD_CODED_ALLOWED: &[&str] = &["Saturating", "String", "Wrapping"]; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + #[clippy::version = "1.63.0"] |
| 12 | + pub ARITHMETIC, |
| 13 | + restriction, |
| 14 | + "any arithmetic expression that could overflow or panic" |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct Arithmetic { |
| 19 | + allowed: FxHashSet<String>, |
| 20 | + /// Used to check whether expressions are constants, such as in enum discriminants and consts |
| 21 | + const_span: Option<Span>, |
| 22 | + expr_span: Option<Span>, |
| 23 | +} |
| 24 | + |
| 25 | +impl_lint_pass!(Arithmetic => [ARITHMETIC]); |
| 26 | + |
| 27 | +impl Arithmetic { |
| 28 | + #[must_use] |
| 29 | + pub fn new(mut allowed: FxHashSet<String>) -> Self { |
| 30 | + allowed.extend(HARD_CODED_ALLOWED.iter().copied().map(String::from)); |
| 31 | + Self { |
| 32 | + allowed, |
| 33 | + const_span: None, |
| 34 | + expr_span: None, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Checks if the given `expr` has any of the inner `allowed` elements. |
| 39 | + fn is_allowed_ty(&self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool { |
| 40 | + match expr.kind { |
| 41 | + hir::ExprKind::Call(ref local_expr, _) => self.is_allowed_ty(cx, local_expr), |
| 42 | + hir::ExprKind::Path(ref qpath) => { |
| 43 | + let path = if let hir::QPath::Resolved(_, ref path) = qpath { |
| 44 | + if let hir::def::Res::Local(hid) = path.res |
| 45 | + && let parent_node_id = cx.tcx.hir().get_parent_node(hid) |
| 46 | + && let Some(hir::Node::Local(local)) = cx.tcx.hir().find(parent_node_id) |
| 47 | + && let Some(ty) = local.ty |
| 48 | + && let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = ty.kind |
| 49 | + { |
| 50 | + path |
| 51 | + } |
| 52 | + else { |
| 53 | + path |
| 54 | + } |
| 55 | + } else if let hir::QPath::TypeRelative(ref ty, _) = qpath |
| 56 | + && let hir::TyKind::Path(hir::QPath::Resolved(_, ref path)) = ty.kind |
| 57 | + { |
| 58 | + path |
| 59 | + } |
| 60 | + else { |
| 61 | + return false; |
| 62 | + }; |
| 63 | + for segment in path.segments { |
| 64 | + if self.allowed.contains(segment.ident.as_str()) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + false |
| 69 | + }, |
| 70 | + _ => false, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fn issue_lint(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) { |
| 75 | + span_lint(cx, ARITHMETIC, expr.span, "arithmetic detected"); |
| 76 | + self.expr_span = Some(expr.span); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl<'tcx> LateLintPass<'tcx> for Arithmetic { |
| 81 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
| 82 | + if self.expr_span.is_some() { |
| 83 | + return; |
| 84 | + } |
| 85 | + if let Some(span) = self.const_span && span.contains(expr.span) { |
| 86 | + return; |
| 87 | + } |
| 88 | + match &expr.kind { |
| 89 | + hir::ExprKind::Binary(op, lhs, rhs) | hir::ExprKind::AssignOp(op, lhs, rhs) => { |
| 90 | + let ( |
| 91 | + hir::BinOpKind::Add |
| 92 | + | hir::BinOpKind::Sub |
| 93 | + | hir::BinOpKind::Mul |
| 94 | + | hir::BinOpKind::Div |
| 95 | + | hir::BinOpKind::Rem |
| 96 | + | hir::BinOpKind::Shl |
| 97 | + | hir::BinOpKind::Shr |
| 98 | + ) = op.node else { |
| 99 | + return; |
| 100 | + }; |
| 101 | + if self.is_allowed_ty(cx, lhs) || self.is_allowed_ty(cx, rhs) { |
| 102 | + return; |
| 103 | + } |
| 104 | + self.issue_lint(cx, expr); |
| 105 | + }, |
| 106 | + hir::ExprKind::Unary(hir::UnOp::Neg, _) => { |
| 107 | + if constant_simple(cx, cx.typeck_results(), expr).is_none() { |
| 108 | + self.issue_lint(cx, expr); |
| 109 | + } |
| 110 | + }, |
| 111 | + _ => {}, |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { |
| 116 | + let body_owner = cx.tcx.hir().body_owner_def_id(body.id()); |
| 117 | + match cx.tcx.hir().body_owner_kind(body_owner) { |
| 118 | + hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => { |
| 119 | + let body_span = cx.tcx.def_span(body_owner); |
| 120 | + if let Some(span) = self.const_span && span.contains(body_span) { |
| 121 | + return; |
| 122 | + } |
| 123 | + self.const_span = Some(body_span); |
| 124 | + }, |
| 125 | + hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => {}, |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { |
| 130 | + let body_owner = cx.tcx.hir().body_owner(body.id()); |
| 131 | + let body_span = cx.tcx.hir().span(body_owner); |
| 132 | + if let Some(span) = self.const_span && span.contains(body_span) { |
| 133 | + return; |
| 134 | + } |
| 135 | + self.const_span = None; |
| 136 | + } |
| 137 | + |
| 138 | + fn check_expr_post(&mut self, _: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
| 139 | + if Some(expr.span) == self.expr_span { |
| 140 | + self.expr_span = None; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments