2 get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq,
4 use crate::utils::{higher, sugg};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
8 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
9 use rustc_lint::{LateContext, LateLintPass};
10 use rustc_middle::hir::map::Map;
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
13 declare_clippy_lint! {
14 /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
17 /// **Why is this bad?** These can be written as the shorter `a op= b`.
19 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have
20 /// implementations that differ from the regular `Op` impl.
33 pub ASSIGN_OP_PATTERN,
35 "assigning the result of an operation on a variable to that same variable"
38 declare_clippy_lint! {
39 /// **What it does:** Checks for `a op= a op b` or `a op= b op a` patterns.
41 /// **Why is this bad?** Most likely these are bugs where one meant to write `a
44 /// **Known problems:** Clippy cannot know for sure if `a op= a op b` should have
45 /// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore, it suggests both.
46 /// If `a op= a op b` is really the correct behaviour it should be
47 /// written as `a = a op a op b` as it's less confusing.
56 pub MISREFACTORED_ASSIGN_OP,
58 "having a variable on both sides of an assign op"
61 declare_lint_pass!(AssignOps => [ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP]);
63 impl<'tcx> LateLintPass<'tcx> for AssignOps {
64 #[allow(clippy::too_many_lines)]
65 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
67 hir::ExprKind::AssignOp(op, lhs, rhs) => {
68 if let hir::ExprKind::Binary(binop, l, r) = &rhs.kind {
69 if op.node != binop.node {
73 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
74 lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, r);
76 // lhs op= l commutative_op r
77 if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
78 lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, l);
82 hir::ExprKind::Assign(assignee, e, _) => {
83 if let hir::ExprKind::Binary(op, l, r) = &e.kind {
84 let lint = |assignee: &hir::Expr<'_>, rhs: &hir::Expr<'_>| {
85 let ty = cx.tables().expr_ty(assignee);
86 let rty = cx.tables().expr_ty(rhs);
92 $($trait_name:ident),+) => {
94 $(hir::BinOpKind::$trait_name => {
95 let [krate, module] = crate::utils::paths::OPS_MODULE;
96 let path: [&str; 3] = [krate, module, concat!(stringify!($trait_name), "Assign")];
97 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
100 return; // useless if the trait doesn't exist
102 // check that we are not inside an `impl AssignOp` of this exact operation
103 let parent_fn = cx.tcx.hir().get_parent_item(e.hir_id);
105 if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
106 if trait_ref.path.res.def_id() == trait_id;
109 implements_trait($cx, $ty, trait_id, &[$rty])
137 "manual implementation of an assign operation",
139 if let (Some(snip_a), Some(snip_r)) =
140 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
142 diag.span_suggestion(
145 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
146 Applicability::MachineApplicable,
154 let mut visitor = ExprVisitor {
160 walk_expr(&mut visitor, e);
162 if visitor.counter == 1 {
164 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
167 // a = b commutative_op a
168 // Limited to primitive type as these ops are know to be commutative
169 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r)
170 && cx.tables().expr_ty(assignee).is_primitive_ty()
174 | hir::BinOpKind::Mul
175 | hir::BinOpKind::And
177 | hir::BinOpKind::BitXor
178 | hir::BinOpKind::BitAnd
179 | hir::BinOpKind::BitOr => {
193 fn lint_misrefactored_assign_op(
194 cx: &LateContext<'_>,
195 expr: &hir::Expr<'_>,
198 assignee: &hir::Expr<'_>,
199 rhs_other: &hir::Expr<'_>,
203 MISREFACTORED_ASSIGN_OP,
205 "variable appears on both sides of an assignment operation",
207 if let (Some(snip_a), Some(snip_r)) = (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span)) {
208 let a = &sugg::Sugg::hir(cx, assignee, "..");
209 let r = &sugg::Sugg::hir(cx, rhs, "..");
210 let long = format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
211 diag.span_suggestion(
214 "Did you mean `{} = {} {} {}` or `{}`? Consider replacing it with",
221 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
222 Applicability::MaybeIncorrect,
224 diag.span_suggestion(
228 Applicability::MaybeIncorrect, // snippet
236 fn is_commutative(op: hir::BinOpKind) -> bool {
237 use rustc_hir::BinOpKind::{
238 Add, And, BitAnd, BitOr, BitXor, Div, Eq, Ge, Gt, Le, Lt, Mul, Ne, Or, Rem, Shl, Shr, Sub,
241 Add | Mul | And | Or | BitXor | BitAnd | BitOr | Eq | Ne => true,
242 Sub | Div | Rem | Shl | Shr | Lt | Le | Ge | Gt => false,
246 struct ExprVisitor<'a, 'tcx> {
247 assignee: &'a hir::Expr<'a>,
249 cx: &'a LateContext<'tcx>,
252 impl<'a, 'tcx> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
253 type Map = Map<'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(&mut self) -> NestedVisitorMap<Self::Map> {
263 NestedVisitorMap::None