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;
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 declare_lint_pass!(Precedence => [PRECEDENCE]);
33 impl EarlyLintPass for Precedence {
34 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
35 if expr.span.from_expansion() {
39 if let ExprKind::Binary(Spanned { node: op, .. }, ref left, ref right) = expr.kind {
40 let span_sugg = |expr: &Expr, sugg, appl| {
45 "operator precedence can trip the unwary",
46 "consider parenthesizing your expression",
55 let mut applicability = Applicability::MachineApplicable;
56 match (is_arith_expr(left), is_arith_expr(right)) {
60 snippet_with_applicability(cx, left.span, "..", &mut applicability),
62 snippet_with_applicability(cx, right.span, "..", &mut applicability)
64 span_sugg(expr, sugg, applicability);
69 snippet_with_applicability(cx, left.span, "..", &mut applicability),
71 snippet_with_applicability(cx, right.span, "..", &mut applicability)
73 span_sugg(expr, sugg, applicability);
78 snippet_with_applicability(cx, left.span, "..", &mut applicability),
80 snippet_with_applicability(cx, right.span, "..", &mut applicability)
82 span_sugg(expr, sugg, applicability);
88 if let ExprKind::Unary(UnOp::Neg, ref rhs) = expr.kind {
89 if let ExprKind::MethodCall(_, ref args) = rhs.kind {
90 if let Some(slf) = args.first() {
91 if let ExprKind::Lit(ref lit) = slf.kind {
93 LitKind::Int(..) | LitKind::Float(..) => {
94 let mut applicability = Applicability::MachineApplicable;
99 "unary minus has lower precedence than method call",
100 "consider adding parentheses to clarify your intent",
103 snippet_with_applicability(cx, rhs.span, "..", &mut applicability)
117 fn is_arith_expr(expr: &Expr) -> bool {
119 ExprKind::Binary(Spanned { node: op, .. }, _, _) => is_arith_op(op),
125 fn is_bit_op(op: BinOpKind) -> bool {
126 use rustc_ast::ast::BinOpKind::{BitAnd, BitOr, BitXor, Shl, Shr};
128 BitXor | BitAnd | BitOr | Shl | Shr => true,
134 fn is_arith_op(op: BinOpKind) -> bool {
135 use rustc_ast::ast::BinOpKind::{Add, Div, Mul, Rem, Sub};
137 Add | Sub | Mul | Div | Rem => true,