1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc::hir::{Expr, ExprAssign, ExprField, ExprStruct, ExprTup, ExprTupField};
3 use utils::is_adjusted;
6 /// **What it does:** This lint checks for construction of a structure or tuple just to assign a
9 /// **Why is this bad?** Readability. If the structure is only created to be updated, why not write
10 /// the structure you want in the first place?
12 /// **Known problems:** None.
19 pub TEMPORARY_ASSIGNMENT,
21 "assignments to temporaries"
24 fn is_temporary(expr: &Expr) -> bool {
26 ExprStruct(..) | ExprTup(..) => true,
31 #[derive(Copy, Clone)]
34 impl LintPass for Pass {
35 fn get_lints(&self) -> LintArray {
36 lint_array!(TEMPORARY_ASSIGNMENT)
40 impl LateLintPass for Pass {
41 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
42 if let ExprAssign(ref target, _) = expr.node {
44 ExprField(ref base, _) |
45 ExprTupField(ref base, _) => {
46 if is_temporary(base) && !is_adjusted(cx, base) {
47 span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");