1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg};
11 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
12 use rustc::{declare_tool_lint, lint_array};
13 use rustc_errors::Applicability;
15 use syntax::source_map::Spanned;
17 /// **What it does:** Checks for operations where precedence may be unclear
18 /// and suggests to add parentheses. Currently it catches the following:
19 /// * mixed usage of arithmetic and bit shifting/combining operators without
21 /// * a "negative" numeric literal (which is really a unary `-` followed by a
23 /// followed by a method call
25 /// **Why is this bad?** Not everyone knows the precedence of those operators by
26 /// heart, so expressions like these may trip others trying to reason about the
29 /// **Known problems:** None.
32 /// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
33 /// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
34 declare_clippy_lint! {
37 "operations where precedence may be unclear"
40 #[derive(Copy, Clone)]
41 pub struct Precedence;
43 impl LintPass for Precedence {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(PRECEDENCE)
49 impl EarlyLintPass for Precedence {
50 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
51 if in_macro(expr.span) {
55 if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.node {
56 let span_sugg = |expr: &Expr, sugg, appl| {
61 "operator precedence can trip the unwary",
62 "consider parenthesizing your expression",
71 let mut applicability = Applicability::MachineApplicable;
72 match (is_arith_expr(left), is_arith_expr(right)) {
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);
94 snippet_with_applicability(cx, left.span, "..", &mut applicability),
96 snippet_with_applicability(cx, right.span, "..", &mut applicability)
98 span_sugg(expr, sugg, applicability);
100 (false, false) => (),
104 if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.node {
105 if let ExprKind::MethodCall(_, ref args) = rhs.node {
106 if let Some(slf) = args.first() {
107 if let ExprKind::Lit(ref lit) = slf.node {
109 LitKind::Int(..) | LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => {
110 let mut applicability = Applicability::MachineApplicable;
115 "unary minus has lower precedence than method call",
116 "consider adding parentheses to clarify your intent",
119 snippet_with_applicability(cx, rhs.span, "..", &mut applicability)
133 fn is_arith_expr(expr: &Expr) -> bool {
135 ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op),
140 fn is_bit_op(op: BinOpKind) -> bool {
141 use syntax::ast::BinOpKind::*;
143 BitXor | BitAnd | BitOr | Shl | Shr => true,
148 fn is_arith_op(op: BinOpKind) -> bool {
149 use syntax::ast::BinOpKind::*;
151 Add | Sub | Mul | Div | Rem => true,