1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet_with_applicability;
3 use if_chain::if_chain;
4 use rustc_ast::ast::{BinOpKind, Expr, ExprKind, MethodCall, UnOp};
6 use rustc_errors::Applicability;
7 use rustc_lint::{EarlyContext, EarlyLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 use rustc_span::source_map::Spanned;
11 const ALLOWED_ODD_FUNCTIONS: [&str; 14] = [
28 declare_clippy_lint! {
30 /// Checks for operations where precedence may be unclear
31 /// and suggests to add parentheses. Currently it catches the following:
32 /// * mixed usage of arithmetic and bit shifting/combining operators without
34 /// * a "negative" numeric literal (which is really a unary `-` followed by a
36 /// followed by a method call
38 /// ### Why is this bad?
39 /// Not everyone knows the precedence of those operators by
40 /// heart, so expressions like these may trip others trying to reason about the
44 /// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
45 /// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
46 #[clippy::version = "pre 1.29.0"]
49 "operations where precedence may be unclear"
52 declare_lint_pass!(Precedence => [PRECEDENCE]);
54 impl EarlyLintPass for Precedence {
55 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
56 if expr.span.from_expansion() {
60 if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.kind {
61 let span_sugg = |expr: &Expr, sugg, appl| {
66 "operator precedence can trip the unwary",
67 "consider parenthesizing your expression",
76 let mut applicability = Applicability::MachineApplicable;
77 match (is_arith_expr(left), is_arith_expr(right)) {
81 snippet_with_applicability(cx, left.span, "..", &mut applicability),
83 snippet_with_applicability(cx, right.span, "..", &mut applicability)
85 span_sugg(expr, sugg, applicability);
90 snippet_with_applicability(cx, left.span, "..", &mut applicability),
92 snippet_with_applicability(cx, right.span, "..", &mut applicability)
94 span_sugg(expr, sugg, applicability);
99 snippet_with_applicability(cx, left.span, "..", &mut applicability),
101 snippet_with_applicability(cx, right.span, "..", &mut applicability)
103 span_sugg(expr, sugg, applicability);
105 (false, false) => (),
109 if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind {
110 let mut arg = operand;
112 let mut all_odd = true;
113 while let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &arg.kind {
114 let seg_str = seg.ident.name.as_str();
115 all_odd &= ALLOWED_ODD_FUNCTIONS
117 .any(|odd_function| **odd_function == *seg_str);
123 if let ExprKind::Lit(lit) = &arg.kind;
124 if let token::LitKind::Integer | token::LitKind::Float = &lit.kind;
126 let mut applicability = Applicability::MachineApplicable;
131 "unary minus has lower precedence than method call",
132 "consider adding parentheses to clarify your intent",
135 snippet_with_applicability(cx, operand.span, "..", &mut applicability)
145 fn is_arith_expr(expr: &Expr) -> bool {
147 ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op),
153 fn is_bit_op(op: BinOpKind) -> bool {
154 use rustc_ast::ast::BinOpKind::{BitAnd, BitOr, BitXor, Shl, Shr};
155 matches!(op, BitXor | BitAnd | BitOr | Shl | Shr)
159 fn is_arith_op(op: BinOpKind) -> bool {
160 use rustc_ast::ast::BinOpKind::{Add, Div, Mul, Rem, Sub};
161 matches!(op, Add | Sub | Mul | Div | Rem)