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};
8 /// **What it does:** Checks for explicit self-assignments.
10 /// **Why is this bad?** Self-assignments are redundant and unlikely to be
13 /// **Known problems:** If expression contains any deref coercions or
14 /// indexing operations they are assumed not to have any side effects.
25 /// fn copy_position(a: &mut Event, b: &Event) {
32 "explicit self-assignment"
35 declare_lint_pass!(SelfAssignment => [SELF_ASSIGNMENT]);
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>");
47 &format!("self-assignment of `{}` to `{}`", rhs, lhs),