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};
10 /// Checks for explicit self-assignments.
12 /// ### Why is this bad?
13 /// Self-assignments are redundant and unlikely to be
16 /// ### Known problems
17 /// If expression contains any deref coercions or
18 /// indexing operations they are assumed not to have any side effects.
28 /// fn copy_position(a: &mut Event, b: &Event) {
33 #[clippy::version = "1.48.0"]
36 "explicit self-assignment"
39 declare_lint_pass!(SelfAssignment => [SELF_ASSIGNMENT]);
41 impl<'tcx> LateLintPass<'tcx> for SelfAssignment {
42 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
43 if let ExprKind::Assign(lhs, rhs, _) = &expr.kind {
44 if eq_expr_value(cx, lhs, rhs) {
45 let lhs = snippet(cx, lhs.span, "<lhs>");
46 let rhs = snippet(cx, rhs.span, "<rhs>");
51 &format!("self-assignment of `{}` to `{}`", rhs, lhs),