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::utils::{get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, SpanlessEq};
12 use crate::utils::{higher, sugg};
13 use crate::rustc::hir;
14 use crate::rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
15 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
16 use crate::rustc::{declare_tool_lint, lint_array};
17 use if_chain::if_chain;
18 use crate::syntax::ast;
19 use crate::rustc_errors::Applicability;
21 /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
24 /// **Why is this bad?** These can be written as the shorter `a op= b`.
26 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have
27 /// implementations that differ from the regular `Op` impl.
35 declare_clippy_lint! {
36 pub ASSIGN_OP_PATTERN,
38 "assigning the result of an operation on a variable to that same variable"
41 /// **What it does:** Checks for `a op= a op b` or `a op= b op a` patterns.
43 /// **Why is this bad?** Most likely these are bugs where one meant to write `a
46 /// **Known problems:** Clippy cannot know for sure if `a op= a op b` should have
47 /// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore it suggests both.
48 /// If `a op= a op b` is really the correct behaviour it should be
49 /// written as `a = a op a op b` as it's less confusing.
57 declare_clippy_lint! {
58 pub MISREFACTORED_ASSIGN_OP,
60 "having a variable on both sides of an assign op"
63 #[derive(Copy, Clone, Default)]
66 impl LintPass for AssignOps {
67 fn get_lints(&self) -> LintArray {
68 lint_array!(ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
72 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
73 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
75 hir::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
76 if let hir::ExprKind::Binary(binop, ref l, ref r) = rhs.node {
77 if op.node == binop.node {
78 let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {
81 MISREFACTORED_ASSIGN_OP,
83 "variable appears on both sides of an assignment operation",
85 if let (Some(snip_a), Some(snip_r)) =
86 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span))
88 let a = &sugg::Sugg::hir(cx, assignee, "..");
89 let r = &sugg::Sugg::hir(cx, rhs, "..");
91 format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
92 db.span_suggestion_with_applicability(
95 "Did you mean {} = {} {} {} or {}? Consider replacing it with",
102 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
103 Applicability::MachineApplicable,
105 db.span_suggestion_with_applicability(
109 Applicability::MachineApplicable, // snippet
116 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
119 // lhs op= l commutative_op r
120 if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
126 hir::ExprKind::Assign(ref assignee, ref e) => {
127 if let hir::ExprKind::Binary(op, ref l, ref r) = e.node {
128 #[allow(clippy::cyclomatic_complexity)]
129 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
130 let ty = cx.tables.expr_ty(assignee);
131 let rty = cx.tables.expr_ty(rhs);
137 $($trait_name:ident),+) => {
139 $(hir::BinOpKind::$trait_name => {
140 let [krate, module] = crate::utils::paths::OPS_MODULE;
141 let path = [krate, module, concat!(stringify!($trait_name), "Assign")];
142 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
145 return; // useless if the trait doesn't exist
147 // check that we are not inside an `impl AssignOp` of this exact operation
148 let parent_fn = cx.tcx.hir.get_parent(e.id);
149 let parent_impl = cx.tcx.hir.get_parent(parent_fn);
150 // the crate node is the only one that is not in the map
152 if parent_impl != ast::CRATE_NODE_ID;
153 if let hir::Node::Item(item) = cx.tcx.hir.get(parent_impl);
154 if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) =
156 if trait_ref.path.def.def_id() == trait_id;
159 implements_trait($cx, $ty, trait_id, &[$rty])
187 "manual implementation of an assign operation",
189 if let (Some(snip_a), Some(snip_r)) =
190 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
192 db.span_suggestion_with_applicability(
195 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
196 Applicability::MachineApplicable,
204 let mut visitor = ExprVisitor {
210 walk_expr(&mut visitor, e);
212 if visitor.counter == 1 {
214 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
217 // a = b commutative_op a
218 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
221 | hir::BinOpKind::Mul
222 | hir::BinOpKind::And
224 | hir::BinOpKind::BitXor
225 | hir::BinOpKind::BitAnd
226 | hir::BinOpKind::BitOr => {
240 fn is_commutative(op: hir::BinOpKind) -> bool {
241 use crate::rustc::hir::BinOpKind::*;
243 Add | Mul | And | Or | BitXor | BitAnd | BitOr | Eq | Ne => true,
244 Sub | Div | Rem | Shl | Shr | Lt | Le | Ge | Gt => false,
248 struct ExprVisitor<'a, 'tcx: 'a> {
249 assignee: &'a hir::Expr,
251 cx: &'a LateContext<'a, 'tcx>,
254 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
255 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
256 if SpanlessEq::new(self.cx).ignore_fn().eq_expr(self.assignee, expr) {
260 walk_expr(self, expr);
262 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
263 NestedVisitorMap::None