1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::rustc::hir::{Expr, ExprKind};
14 use crate::utils::is_adjusted;
15 use crate::utils::span_lint;
17 /// **What it does:** Checks for construction of a structure or tuple just to
18 /// assign a value in it.
20 /// **Why is this bad?** Readability. If the structure is only created to be
21 /// updated, why not write the structure you want in the first place?
23 /// **Known problems:** None.
29 declare_clippy_lint! {
30 pub TEMPORARY_ASSIGNMENT,
32 "assignments to temporaries"
35 fn is_temporary(expr: &Expr) -> bool {
37 ExprKind::Struct(..) | ExprKind::Tup(..) => true,
42 #[derive(Copy, Clone)]
45 impl LintPass for Pass {
46 fn get_lints(&self) -> LintArray {
47 lint_array!(TEMPORARY_ASSIGNMENT)
51 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
52 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
53 if let ExprKind::Assign(ref target, _) = expr.node {
54 if let ExprKind::Field(ref base, _) = target.node {
55 if is_temporary(base) && !is_adjusted(cx, base) {
56 span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");