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:** Checks for construction of a structure or tuple just to
7 /// assign a value in it.
9 /// **Why is this bad?** Readability. If the structure is only created to be
10 /// updated, why not write 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<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
41 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
42 if let ExprAssign(ref target, _) = expr.node {
44 ExprField(ref base, _) | ExprTupField(ref base, _) => if is_temporary(base) && !is_adjusted(cx, base) {
45 span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");