|
| 1 | +use rustc_ast::token::LitKind; |
| 2 | +use rustc_ast::{BinOpKind, Expr, ExprKind, MethodCall, UnOp}; |
| 3 | +use rustc_span::source_map::Spanned; |
| 4 | + |
| 5 | +use crate::lints::{PrecedenceDiag, PrecedenceUnarySuggestion, PrecedenceUnwarySuggestion}; |
| 6 | +use crate::{EarlyContext, EarlyLintPass, LintContext}; |
| 7 | + |
| 8 | +// List of functions `f(x)` where `f(-x)=-f(x)` so the |
| 9 | +// precedence doens't matter. |
| 10 | +const ALLOWED_ODD_FUNCTIONS: [&str; 14] = [ |
| 11 | + "asin", |
| 12 | + "asinh", |
| 13 | + "atan", |
| 14 | + "atanh", |
| 15 | + "cbrt", |
| 16 | + "fract", |
| 17 | + "round", |
| 18 | + "signum", |
| 19 | + "sin", |
| 20 | + "sinh", |
| 21 | + "tan", |
| 22 | + "tanh", |
| 23 | + "to_degrees", |
| 24 | + "to_radians", |
| 25 | +]; |
| 26 | + |
| 27 | +declare_lint! { |
| 28 | + /// The `ambiguous_precedence` lint checks for operations where |
| 29 | + /// precedence may be unclear and suggests adding parentheses. |
| 30 | + /// |
| 31 | + /// ### Example |
| 32 | + /// |
| 33 | + /// ```rust |
| 34 | + /// # #[allow(unused)] |
| 35 | + /// 1 << 2 + 3; // equals 32, while `(1 << 2) + 3` equals 7 |
| 36 | + /// -1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1 |
| 37 | + /// ``` |
| 38 | + /// |
| 39 | + /// {{produces}} |
| 40 | + /// |
| 41 | + /// ### Explanation |
| 42 | + /// |
| 43 | + /// Unary operations take precedence on binary operations and method |
| 44 | + /// calls take precedence over unary precedence. Setting the precedence |
| 45 | + /// explicitly makes the code clearer and avoid potential bugs. |
| 46 | + pub AMBIGUOUS_PRECEDENCE, |
| 47 | + Warn, |
| 48 | + "operations where precedence may be unclear" |
| 49 | +} |
| 50 | + |
| 51 | +declare_lint_pass!(Precedence => [AMBIGUOUS_PRECEDENCE]); |
| 52 | + |
| 53 | +impl EarlyLintPass for Precedence { |
| 54 | + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { |
| 55 | + if expr.span.from_expansion() { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.kind { |
| 60 | + if !is_bit_op(op) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + let suggestion = match (is_arith_expr(left), is_arith_expr(right)) { |
| 65 | + (true, true) => PrecedenceUnwarySuggestion::TwoExpr { |
| 66 | + start_span: left.span.shrink_to_lo(), |
| 67 | + end_span: left.span.shrink_to_hi(), |
| 68 | + start2_span: right.span.shrink_to_lo(), |
| 69 | + end2_span: right.span.shrink_to_hi(), |
| 70 | + }, |
| 71 | + (true, false) => PrecedenceUnwarySuggestion::OneExpr { |
| 72 | + start_span: left.span.shrink_to_lo(), |
| 73 | + end_span: left.span.shrink_to_hi(), |
| 74 | + }, |
| 75 | + (false, true) => PrecedenceUnwarySuggestion::OneExpr { |
| 76 | + start_span: right.span.shrink_to_lo(), |
| 77 | + end_span: right.span.shrink_to_hi(), |
| 78 | + }, |
| 79 | + (false, false) => return, |
| 80 | + }; |
| 81 | + |
| 82 | + cx.emit_spanned_lint( |
| 83 | + AMBIGUOUS_PRECEDENCE, |
| 84 | + expr.span, |
| 85 | + PrecedenceDiag::Unwary { suggestion }, |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind { |
| 90 | + let mut arg = operand; |
| 91 | + |
| 92 | + let mut all_odd = true; |
| 93 | + while let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &arg.kind { |
| 94 | + let seg_str = seg.ident.name.as_str(); |
| 95 | + all_odd &= |
| 96 | + ALLOWED_ODD_FUNCTIONS.iter().any(|odd_function| **odd_function == *seg_str); |
| 97 | + arg = receiver; |
| 98 | + } |
| 99 | + |
| 100 | + if !all_odd |
| 101 | + && let ExprKind::Lit(lit) = &arg.kind |
| 102 | + && let LitKind::Integer | LitKind::Float = &lit.kind |
| 103 | + { |
| 104 | + cx.emit_spanned_lint(AMBIGUOUS_PRECEDENCE, expr.span, PrecedenceDiag::Unary { |
| 105 | + suggestion: PrecedenceUnarySuggestion { |
| 106 | + start_span: operand.span.shrink_to_lo(), |
| 107 | + end_span: operand.span.shrink_to_hi(), |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +fn is_arith_expr(expr: &Expr) -> bool { |
| 116 | + match expr.kind { |
| 117 | + ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op), |
| 118 | + _ => false, |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +#[must_use] |
| 123 | +fn is_bit_op(op: BinOpKind) -> bool { |
| 124 | + use rustc_ast::ast::BinOpKind::{BitAnd, BitOr, BitXor, Shl, Shr}; |
| 125 | + matches!(op, BitXor | BitAnd | BitOr | Shl | Shr) |
| 126 | +} |
| 127 | + |
| 128 | +#[must_use] |
| 129 | +fn is_arith_op(op: BinOpKind) -> bool { |
| 130 | + use rustc_ast::ast::BinOpKind::{Add, Div, Mul, Rem, Sub}; |
| 131 | + matches!(op, Add | Sub | Mul | Div | Rem) |
| 132 | +} |
0 commit comments