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.
11 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::rustc_errors::Applicability;
14 use crate::syntax::ast::*;
15 use crate::syntax::source_map::Spanned;
16 use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg};
18 /// **What it does:** Checks for operations where precedence may be unclear
19 /// and suggests to add parentheses. Currently it catches the following:
20 /// * mixed usage of arithmetic and bit shifting/combining operators without
22 /// * a "negative" numeric literal (which is really a unary `-` followed by a
24 /// followed by a method call
26 /// **Why is this bad?** Not everyone knows the precedence of those operators by
27 /// heart, so expressions like these may trip others trying to reason about the
30 /// **Known problems:** None.
33 /// * `1 << 2 + 3` equals 32, while `(1 << 2) + 3` equals 7
34 /// * `-1i32.abs()` equals -1, while `(-1i32).abs()` equals 1
35 declare_clippy_lint! {
38 "operations where precedence may be unclear"
41 #[derive(Copy, Clone)]
42 pub struct Precedence;
44 impl LintPass for Precedence {
45 fn get_lints(&self) -> LintArray {
46 lint_array!(PRECEDENCE)
50 impl EarlyLintPass for Precedence {
51 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
52 if in_macro(expr.span) {
56 if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.node {
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.node {
106 if let ExprKind::MethodCall(_, ref args) = rhs.node {
107 if let Some(slf) = args.first() {
108 if let ExprKind::Lit(ref lit) = slf.node {
110 LitKind::Int(..) | LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => {
111 let mut applicability = Applicability::MachineApplicable;
116 "unary minus has lower precedence than method call",
117 "consider adding parentheses to clarify your intent",
118 format!("-({})", snippet_with_applicability(cx, rhs.span, "..", &mut applicability)),
131 fn is_arith_expr(expr: &Expr) -> bool {
133 ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op),
138 fn is_bit_op(op: BinOpKind) -> bool {
139 use crate::syntax::ast::BinOpKind::*;
141 BitXor | BitAnd | BitOr | Shl | Shr => true,
146 fn is_arith_op(op: BinOpKind) -> bool {
147 use crate::syntax::ast::BinOpKind::*;
149 Add | Sub | Mul | Div | Rem => true,