1 use crate::utils::is_adjusted;
2 use crate::utils::span_lint;
3 use rustc::hir::def::Def;
4 use rustc::hir::{Expr, ExprKind};
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_tool_lint, lint_array};
9 /// **What it does:** Checks for construction of a structure or tuple just to
10 /// assign a value in it.
12 /// **Why is this bad?** Readability. If the structure is only created to be
13 /// updated, why not write the structure you want in the first place?
15 /// **Known problems:** None.
21 pub TEMPORARY_ASSIGNMENT,
23 "assignments to temporaries"
26 fn is_temporary(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
28 ExprKind::Struct(..) | ExprKind::Tup(..) => true,
29 ExprKind::Path(qpath) => {
30 if let Def::Const(..) = cx.tables.qpath_def(qpath, expr.hir_id) {
40 #[derive(Copy, Clone)]
43 impl LintPass for Pass {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(TEMPORARY_ASSIGNMENT)
48 fn name(&self) -> &'static str {
53 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
54 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
55 if let ExprKind::Assign(target, _) = &expr.node {
56 let mut base = target;
57 while let ExprKind::Field(f, _) | ExprKind::Index(f, _) = &base.node {
60 if is_temporary(cx, base) && !is_adjusted(cx, base) {
61 span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");