]> git.proxmox.com Git - rustc.git/blobdiff - src/tools/clippy/clippy_lints/src/temporary_assignment.rs
Update upstream source from tag 'upstream/1.52.1+dfsg1'
[rustc.git] / src / tools / clippy / clippy_lints / src / temporary_assignment.rs
diff --git a/src/tools/clippy/clippy_lints/src/temporary_assignment.rs b/src/tools/clippy/clippy_lints/src/temporary_assignment.rs
new file mode 100644 (file)
index 0000000..fb89186
--- /dev/null
@@ -0,0 +1,42 @@
+use crate::utils::{is_adjusted, span_lint};
+use rustc_hir::{Expr, ExprKind};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+
+declare_clippy_lint! {
+    /// **What it does:** Checks for construction of a structure or tuple just to
+    /// assign a value in it.
+    ///
+    /// **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?
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// (0, 0).0 = 1
+    /// ```
+    pub TEMPORARY_ASSIGNMENT,
+    complexity,
+    "assignments to temporaries"
+}
+
+fn is_temporary(expr: &Expr<'_>) -> bool {
+    matches!(&expr.kind, ExprKind::Struct(..) | ExprKind::Tup(..))
+}
+
+declare_lint_pass!(TemporaryAssignment => [TEMPORARY_ASSIGNMENT]);
+
+impl<'tcx> LateLintPass<'tcx> for TemporaryAssignment {
+    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
+        if let ExprKind::Assign(target, ..) = &expr.kind {
+            let mut base = target;
+            while let ExprKind::Field(f, _) | ExprKind::Index(f, _) = &base.kind {
+                base = f;
+            }
+            if is_temporary(base) && !is_adjusted(cx, base) {
+                span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span, "assignment to temporary");
+            }
+        }
+    }
+}