1 use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg};
2 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3 use rustc::{declare_tool_lint, lint_array};
4 use rustc_errors::Applicability;
6 use syntax::source_map::Spanned;
8 /// **What it does:** Checks for operations where precedence may be unclear
9 /// and suggests to add parentheses. Currently it catches the following:
10 /// * mixed usage of arithmetic and bit shifting/combining operators without
12 /// * a "negative" numeric literal (which is really a unary `-` followed by a
14 /// followed by a method call
16 /// **Why is this bad?** Not everyone knows the precedence of those operators by
17 /// heart, so expressions like these may trip others trying to reason about the
20 /// **Known problems:** None.
23 /// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
24 /// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
25 declare_clippy_lint! {
28 "operations where precedence may be unclear"
31 #[derive(Copy, Clone)]
32 pub struct Precedence;
34 impl LintPass for Precedence {
35 fn get_lints(&self) -> LintArray {
36 lint_array!(PRECEDENCE)
40 impl EarlyLintPass for Precedence {
41 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
42 if in_macro(expr.span) {
46 if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.node {
47 let span_sugg = |expr: &Expr, sugg, appl| {
52 "operator precedence can trip the unwary",
53 "consider parenthesizing your expression",
62 let mut applicability = Applicability::MachineApplicable;
63 match (is_arith_expr(left), is_arith_expr(right)) {
67 snippet_with_applicability(cx, left.span, "..", &mut applicability),
69 snippet_with_applicability(cx, right.span, "..", &mut applicability)
71 span_sugg(expr, sugg, applicability);
76 snippet_with_applicability(cx, left.span, "..", &mut applicability),
78 snippet_with_applicability(cx, right.span, "..", &mut applicability)
80 span_sugg(expr, sugg, applicability);
85 snippet_with_applicability(cx, left.span, "..", &mut applicability),
87 snippet_with_applicability(cx, right.span, "..", &mut applicability)
89 span_sugg(expr, sugg, applicability);
95 if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.node {
96 if let ExprKind::MethodCall(_, ref args) = rhs.node {
97 if let Some(slf) = args.first() {
98 if let ExprKind::Lit(ref lit) = slf.node {
100 LitKind::Int(..) | LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => {
101 let mut applicability = Applicability::MachineApplicable;
106 "unary minus has lower precedence than method call",
107 "consider adding parentheses to clarify your intent",
110 snippet_with_applicability(cx, rhs.span, "..", &mut applicability)
124 fn is_arith_expr(expr: &Expr) -> bool {
126 ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op),
131 fn is_bit_op(op: BinOpKind) -> bool {
132 use syntax::ast::BinOpKind::*;
134 BitXor | BitAnd | BitOr | Shl | Shr => true,
139 fn is_arith_op(op: BinOpKind) -> bool {
140 use syntax::ast::BinOpKind::*;
142 Add | Sub | Mul | Div | Rem => true,