1 use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
2 use rustc_ast::ast::{BinOpKind, Expr, ExprKind, LitKind, UnOp};
3 use rustc_errors::Applicability;
4 use rustc_lint::{EarlyContext, EarlyLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::source_map::Spanned;
8 const ODD_FUNCTIONS_WHITELIST: [&str; 14] = [
25 declare_clippy_lint! {
26 /// **What it does:** Checks for operations where precedence may be unclear
27 /// and suggests to add parentheses. Currently it catches the following:
28 /// * mixed usage of arithmetic and bit shifting/combining operators without
30 /// * a "negative" numeric literal (which is really a unary `-` followed by a
32 /// followed by a method call
34 /// **Why is this bad?** Not everyone knows the precedence of those operators by
35 /// heart, so expressions like these may trip others trying to reason about the
38 /// **Known problems:** None.
41 /// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
42 /// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
45 "operations where precedence may be unclear"
48 declare_lint_pass!(Precedence => [PRECEDENCE]);
50 impl EarlyLintPass for Precedence {
51 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
52 if expr.span.from_expansion() {
56 if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.kind {
57 let span_sugg = |expr: &Expr, sugg, appl| {
62 "operator precedence can trip the unwary",
63 "consider parenthesizing your expression",
72 let mut applicability = Applicability::MachineApplicable;
73 match (is_arith_expr(left), is_arith_expr(right)) {
77 snippet_with_applicability(cx, left.span, "..", &mut applicability),
79 snippet_with_applicability(cx, right.span, "..", &mut applicability)
81 span_sugg(expr, sugg, applicability);
86 snippet_with_applicability(cx, left.span, "..", &mut applicability),
88 snippet_with_applicability(cx, right.span, "..", &mut applicability)
90 span_sugg(expr, sugg, applicability);
95 snippet_with_applicability(cx, left.span, "..", &mut applicability),
97 snippet_with_applicability(cx, right.span, "..", &mut applicability)
99 span_sugg(expr, sugg, applicability);
101 (false, false) => (),
105 if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.kind {
106 if let ExprKind::MethodCall(ref path_segment, ref args) = rhs.kind {
107 let path_segment_str = path_segment.ident.name.as_str();
108 if let Some(slf) = args.first() {
109 if let ExprKind::Lit(ref lit) = slf.kind {
111 LitKind::Int(..) | LitKind::Float(..) => {
112 if ODD_FUNCTIONS_WHITELIST
114 .any(|odd_function| **odd_function == *path_segment_str)
118 let mut applicability = Applicability::MachineApplicable;
123 "unary minus has lower precedence than method call",
124 "consider adding parentheses to clarify your intent",
127 snippet_with_applicability(cx, rhs.span, "..", &mut applicability)
141 fn is_arith_expr(expr: &Expr) -> bool {
143 ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op),
149 fn is_bit_op(op: BinOpKind) -> bool {
150 use rustc_ast::ast::BinOpKind::{BitAnd, BitOr, BitXor, Shl, Shr};
152 BitXor | BitAnd | BitOr | Shl | Shr => true,
158 fn is_arith_op(op: BinOpKind) -> bool {
159 use rustc_ast::ast::BinOpKind::{Add, Div, Mul, Rem, Sub};
161 Add | Sub | Mul | Div | Rem => true,