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, LitKind, UnOp};
5 use rustc_errors::Applicability;
6 use rustc_lint::{EarlyContext, EarlyLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::source_map::Spanned;
10 const ALLOWED_ODD_FUNCTIONS: [&str; 14] = [
27 declare_clippy_lint! {
29 /// Checks for operations where precedence may be unclear
30 /// and suggests to add parentheses. Currently it catches the following:
31 /// * mixed usage of arithmetic and bit shifting/combining operators without
33 /// * a "negative" numeric literal (which is really a unary `-` followed by a
35 /// followed by a method call
37 /// ### Why is this bad?
38 /// Not everyone knows the precedence of those operators by
39 /// heart, so expressions like these may trip others trying to reason about the
43 /// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
44 /// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
45 #[clippy::version = "pre 1.29.0"]
48 "operations where precedence may be unclear"
51 declare_lint_pass!(Precedence => [PRECEDENCE]);
53 impl EarlyLintPass for Precedence {
54 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
55 if expr.span.from_expansion() {
59 if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.kind {
60 let span_sugg = |expr: &Expr, sugg, appl| {
65 "operator precedence can trip the unwary",
66 "consider parenthesizing your expression",
75 let mut applicability = Applicability::MachineApplicable;
76 match (is_arith_expr(left), is_arith_expr(right)) {
80 snippet_with_applicability(cx, left.span, "..", &mut applicability),
82 snippet_with_applicability(cx, right.span, "..", &mut applicability)
84 span_sugg(expr, sugg, applicability);
89 snippet_with_applicability(cx, left.span, "..", &mut applicability),
91 snippet_with_applicability(cx, right.span, "..", &mut applicability)
93 span_sugg(expr, sugg, applicability);
98 snippet_with_applicability(cx, left.span, "..", &mut applicability),
100 snippet_with_applicability(cx, right.span, "..", &mut applicability)
102 span_sugg(expr, sugg, applicability);
104 (false, false) => (),
108 if let ExprKind::Unary(UnOp::Neg, operand) = &expr.kind {
109 let mut arg = operand;
111 let mut all_odd = true;
112 while let ExprKind::MethodCall(path_segment, args, _) = &arg.kind {
113 let path_segment_str = path_segment.ident.name.as_str();
114 all_odd &= ALLOWED_ODD_FUNCTIONS
116 .any(|odd_function| **odd_function == *path_segment_str);
117 arg = args.first().expect("A method always has a receiver.");
122 if let ExprKind::Lit(lit) = &arg.kind;
123 if let LitKind::Int(..) | LitKind::Float(..) = &lit.kind;
125 let mut applicability = Applicability::MachineApplicable;
130 "unary minus has lower precedence than method call",
131 "consider adding parentheses to clarify your intent",
134 snippet_with_applicability(cx, operand.span, "..", &mut applicability)
144 fn is_arith_expr(expr: &Expr) -> bool {
146 ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op),
152 fn is_bit_op(op: BinOpKind) -> bool {
153 use rustc_ast::ast::BinOpKind::{BitAnd, BitOr, BitXor, Shl, Shr};
154 matches!(op, BitXor | BitAnd | BitOr | Shl | Shr)
158 fn is_arith_op(op: BinOpKind) -> bool {
159 use rustc_ast::ast::BinOpKind::{Add, Div, Mul, Rem, Sub};
160 matches!(op, Add | Sub | Mul | Div | Rem)