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