]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/self_assignment.rs
Auto merge of #6914 - camsteffen:source-utils, r=Manishearth
[rust.git] / clippy_lints / src / self_assignment.rs
1 use crate::utils::{eq_expr_value, span_lint};
2 use clippy_utils::source::snippet;
3 use rustc_hir::{Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for explicit self-assignments.
9     ///
10     /// **Why is this bad?** Self-assignments are redundant and unlikely to be
11     /// intentional.
12     ///
13     /// **Known problems:** If expression contains any deref coercions or
14     /// indexing operations they are assumed not to have any side effects.
15     ///
16     /// **Example:**
17     ///
18     /// ```rust
19     /// struct Event {
20     ///     id: usize,
21     ///     x: i32,
22     ///     y: i32,
23     /// }
24     ///
25     /// fn copy_position(a: &mut Event, b: &Event) {
26     ///     a.x = b.x;
27     ///     a.y = a.y;
28     /// }
29     /// ```
30     pub SELF_ASSIGNMENT,
31     correctness,
32     "explicit self-assignment"
33 }
34
35 declare_lint_pass!(SelfAssignment => [SELF_ASSIGNMENT]);
36
37 impl<'tcx> LateLintPass<'tcx> for SelfAssignment {
38     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
39         if let ExprKind::Assign(lhs, rhs, _) = &expr.kind {
40             if eq_expr_value(cx, lhs, rhs) {
41                 let lhs = snippet(cx, lhs.span, "<lhs>");
42                 let rhs = snippet(cx, rhs.span, "<rhs>");
43                 span_lint(
44                     cx,
45                     SELF_ASSIGNMENT,
46                     expr.span,
47                     &format!("self-assignment of `{}` to `{}`", rhs, lhs),
48                 );
49             }
50         }
51     }
52 }