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;
9 /// **What it does:** Checks for operations where precedence may be unclear
10 /// and suggests to add parentheses. Currently it catches the following:
11 /// * mixed usage of arithmetic and bit shifting/combining operators without
13 /// * a "negative" numeric literal (which is really a unary `-` followed by a
15 /// followed by a method call
17 /// **Why is this bad?** Not everyone knows the precedence of those operators by
18 /// heart, so expressions like these may trip others trying to reason about the
21 /// **Known problems:** None.
24 /// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
25 /// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
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)
39 fn name(&self) -> &'static str {
44 impl EarlyLintPass for Precedence {
45 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
46 if in_macro(expr.span) {
50 if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.node {
51 let span_sugg = |expr: &Expr, sugg, appl| {
56 "operator precedence can trip the unwary",
57 "consider parenthesizing your expression",
66 let mut applicability = Applicability::MachineApplicable;
67 match (is_arith_expr(left), is_arith_expr(right)) {
71 snippet_with_applicability(cx, left.span, "..", &mut applicability),
73 snippet_with_applicability(cx, right.span, "..", &mut applicability)
75 span_sugg(expr, sugg, applicability);
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);
99 if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.node {
100 if let ExprKind::MethodCall(_, ref args) = rhs.node {
101 if let Some(slf) = args.first() {
102 if let ExprKind::Lit(ref lit) = slf.node {
104 LitKind::Int(..) | LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => {
105 let mut applicability = Applicability::MachineApplicable;
110 "unary minus has lower precedence than method call",
111 "consider adding parentheses to clarify your intent",
114 snippet_with_applicability(cx, rhs.span, "..", &mut applicability)
128 fn is_arith_expr(expr: &Expr) -> bool {
130 ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op),
135 fn is_bit_op(op: BinOpKind) -> bool {
136 use syntax::ast::BinOpKind::*;
138 BitXor | BitAnd | BitOr | Shl | Shr => true,
143 fn is_arith_op(op: BinOpKind) -> bool {
144 use syntax::ast::BinOpKind::*;
146 Add | Sub | Mul | Div | Rem => true,