1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::is_adjusted;
3 use rustc_hir::{Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// Checks for construction of a structure or tuple just to
10 /// assign a value in it.
12 /// ### Why is this bad?
13 /// Readability. If the structure is only created to be
14 /// updated, why not write the structure you want in the first place?
20 #[clippy::version = "pre 1.29.0"]
21 pub TEMPORARY_ASSIGNMENT,
23 "assignments to temporaries"
26 fn is_temporary(expr: &Expr<'_>) -> bool {
27 matches!(&expr.kind, ExprKind::Struct(..) | ExprKind::Tup(..))
30 declare_lint_pass!(TemporaryAssignment => [TEMPORARY_ASSIGNMENT]);
32 impl<'tcx> LateLintPass<'tcx> for TemporaryAssignment {
33 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
34 if let ExprKind::Assign(target, ..) = &expr.kind {
35 let mut base = target;
36 while let ExprKind::Field(f, _) | ExprKind::Index(f, _) = &base.kind {
39 if is_temporary(base) && !is_adjusted(cx, base) {
40 span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");