1 use crate::utils::{is_adjusted, span_lint};
2 use rustc_hir::{Expr, ExprKind};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 /// **What it does:** Checks for construction of a structure or tuple just to
8 /// assign a value in it.
10 /// **Why is this bad?** Readability. If the structure is only created to be
11 /// updated, why not write the structure you want in the first place?
13 /// **Known problems:** None.
19 pub TEMPORARY_ASSIGNMENT,
21 "assignments to temporaries"
24 fn is_temporary(_cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
26 ExprKind::Struct(..) | ExprKind::Tup(..) => true,
31 declare_lint_pass!(TemporaryAssignment => [TEMPORARY_ASSIGNMENT]);
33 impl<'tcx> LateLintPass<'tcx> for TemporaryAssignment {
34 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
35 if let ExprKind::Assign(target, ..) = &expr.kind {
36 let mut base = target;
37 while let ExprKind::Field(f, _) | ExprKind::Index(f, _) = &base.kind {
40 if is_temporary(cx, base) && !is_adjusted(cx, base) {
41 span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");