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 value in it.
8 /// **Why is this bad?** Readability. If the structure is only created to be updated, why not write the structure you want in the first place?
10 /// **Known problems:** None.
12 /// **Example:** `(0, 0).0 = 1`
14 pub TEMPORARY_ASSIGNMENT,
16 "assignments to temporaries"
19 fn is_temporary(expr: &Expr) -> bool {
21 ExprStruct(..) | ExprTup(..) => true,
26 #[derive(Copy, Clone)]
29 impl LintPass for Pass {
30 fn get_lints(&self) -> LintArray {
31 lint_array!(TEMPORARY_ASSIGNMENT)
35 impl LateLintPass for Pass {
36 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
37 if let ExprAssign(ref target, _) = expr.node {
39 ExprField(ref base, _) |
40 ExprTupField(ref base, _) => {
41 if is_temporary(base) && !is_adjusted(cx, base) {
42 span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");