1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc_front::hir::{Expr, ExprAssign, ExprField, ExprStruct, ExprTup, ExprTupField};
4 use utils::is_adjusted;
7 /// **What it does:** This lint `Warn`s on creating a struct or tuple just to assign a value in it.
9 /// **Why is this bad?** Readability. If the struct is only created to be updated, why not write the struct you want in the first place?
11 /// **Known problems:** None.
13 /// **Example:** `(0, 0).0 = 1`
15 pub TEMPORARY_ASSIGNMENT,
17 "assignments to temporaries"
20 fn is_temporary(expr: &Expr) -> bool {
28 #[derive(Copy, Clone)]
29 pub struct TemporaryAssignmentPass;
31 impl LintPass for TemporaryAssignmentPass {
32 fn get_lints(&self) -> LintArray {
33 lint_array!(TEMPORARY_ASSIGNMENT)
37 impl LateLintPass for TemporaryAssignmentPass {
38 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
39 if let ExprAssign(ref target, _) = expr.node {
41 ExprField(ref base, _) | ExprTupField(ref base, _) => {
42 if is_temporary(base) && !is_adjusted(cx, base) {
43 span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span,
44 "assignment to temporary");