|
| 1 | +#![allow(unused)] |
| 2 | +use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; |
| 3 | +use clippy_utils::source::snippet; |
| 4 | +use rustc_data_structures::fx::FxHashMap; |
| 5 | +use rustc_errors::Applicability; |
| 6 | + |
| 7 | +use rustc_hir::{self as hir, ExprKind, Path, PathSegment, QPath}; |
| 8 | + |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | +use std::fmt::{self, Write as _}; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for struct literals that recreate a struct we already have an owned |
| 16 | + /// version of. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// This is essentially a no-op as all members are being moved into the new |
| 20 | + /// struct and the old one becomes inaccessible. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```no_run |
| 24 | + /// fn redundant () { |
| 25 | + /// struct Foo { a: i32, b: String } |
| 26 | + /// |
| 27 | + /// let foo = Foo { a: 42, b: "Hello, Foo!".into() }; |
| 28 | + /// |
| 29 | + /// let bar = Foo { a: foo.a, b: foo.b }; |
| 30 | + /// |
| 31 | + /// println!("redundant: bar = Foo {{ a: {}, b: \"{}\" }}", bar.a, bar.b); |
| 32 | + /// } |
| 33 | + /// ``` |
| 34 | + /// |
| 35 | + /// The struct literal in the assignment to ``bar`` could be replaced with |
| 36 | + /// ``let bar = foo``. |
| 37 | + /// |
| 38 | + /// Empty structs are ignored by the lint. |
| 39 | + #[clippy::version = "pre 1.29.0"] |
| 40 | + pub REDUNDANT_OWNED_STRUCT_RECREATION, |
| 41 | + //pedantic, |
| 42 | + correctness, |
| 43 | + "recreation of owned structs from identical parts" |
| 44 | +} |
| 45 | + |
| 46 | +declare_lint_pass!(RedundantOwnedStructRecreation => [REDUNDANT_OWNED_STRUCT_RECREATION]); |
| 47 | + |
| 48 | +impl<'tcx> LateLintPass<'tcx> for RedundantOwnedStructRecreation { |
| 49 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { |
| 50 | + if !expr.span.from_expansion() |
| 51 | + && let ExprKind::Struct(qpath, fields, base) = expr.kind |
| 52 | + { |
| 53 | + let ty = cx.typeck_results().expr_ty(expr); |
| 54 | + |
| 55 | + // Conditions that must be satisfied to trigger the lint: |
| 56 | + // - source of the assignment must be a struct |
| 57 | + // - type of struct in expr must match |
| 58 | + // - names of assignee struct fields (lhs) must match the names of the assigned on (rhs) |
| 59 | + // - all fields musts be assigned (no Default) |
| 60 | + |
| 61 | + if base.is_some() { |
| 62 | + // one or more fields assigned from `..Default` |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + let matching = fields.iter().filter(|f| !f.is_shorthand).try_fold( |
| 67 | + None, |
| 68 | + |seen_path, f| -> Result<Option<&'tcx Path<'tcx>>, ()> { |
| 69 | + // fields are assigned from expression |
| 70 | + if let ExprKind::Field(expr, ident) = f.expr.kind |
| 71 | + // expression type matches |
| 72 | + && ty == cx.typeck_results().expr_ty(expr) |
| 73 | + // field name matches |
| 74 | + && f.ident == ident |
| 75 | + && let ExprKind::Path(QPath::Resolved(None, path)) = expr.kind |
| 76 | + { |
| 77 | + match seen_path { |
| 78 | + None => { |
| 79 | + // first field assignment |
| 80 | + Ok(Some(path)) |
| 81 | + }, |
| 82 | + Some(p) if p.res == path.res => { |
| 83 | + // subsequent field assignment, same origin struct as before |
| 84 | + Ok(seen_path) |
| 85 | + }, |
| 86 | + Some(_) => { |
| 87 | + // subsequent field assignment, different origin struct as before |
| 88 | + Err(()) |
| 89 | + }, |
| 90 | + } |
| 91 | + } else { |
| 92 | + // source of field assignment not an expression |
| 93 | + Err(()) |
| 94 | + } |
| 95 | + }, |
| 96 | + ); |
| 97 | + |
| 98 | + let Ok(Some(path)) = matching else { return }; |
| 99 | + |
| 100 | + let sugg = format!("{}", snippet(cx, path.span, ".."),); |
| 101 | + |
| 102 | + span_lint_and_sugg( |
| 103 | + cx, |
| 104 | + REDUNDANT_OWNED_STRUCT_RECREATION, |
| 105 | + expr.span, |
| 106 | + "recreation of owned struct from identical parts", |
| 107 | + "try replacing it with", |
| 108 | + sugg, |
| 109 | + // the lint may be incomplete due to partial moves |
| 110 | + // of struct fields |
| 111 | + Applicability::MaybeIncorrect, |
| 112 | + ); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments