[Jump to usage instructions](#usage)
##Lints
-There are 72 lints included in this crate:
+There are 73 lints included in this crate:
name | default | meaning
-------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[string_add](https://github.com/Manishearth/rust-clippy/wiki#string_add) | allow | using `x + ..` where x is a `String`; suggests using `push_str()` instead
[string_add_assign](https://github.com/Manishearth/rust-clippy/wiki#string_add_assign) | allow | using `x = x + ..` where x is a `String`; suggests using `push_str()` instead
[string_to_string](https://github.com/Manishearth/rust-clippy/wiki#string_to_string) | warn | calling `String.to_string()` which is a no-op
+[temporary_assignment](https://github.com/Manishearth/rust-clippy/wiki#temporary_assignment) | warn | assignments to temporaries
[toplevel_ref_arg](https://github.com/Manishearth/rust-clippy/wiki#toplevel_ref_arg) | warn | An entire binding was declared as `ref`, in a function argument (`fn foo(ref x: Bar)`), or a `let` statement (`let ref x = foo()`). In such cases, it is preferred to take references with `&`.
[type_complexity](https://github.com/Manishearth/rust-clippy/wiki#type_complexity) | warn | usage of very complex types; recommends factoring out parts into `type` definitions
[unicode_not_nfc](https://github.com/Manishearth/rust-clippy/wiki#unicode_not_nfc) | allow | using a unicode literal not in NFC normal form (see http://www.unicode.org/reports/tr15/ for further information)
pub mod needless_features;
pub mod needless_update;
pub mod no_effect;
+pub mod temporary_assignment;
mod reexport {
pub use syntax::ast::{Name, Ident, NodeId};
reg.register_late_lint_pass(box needless_update::NeedlessUpdatePass);
reg.register_late_lint_pass(box no_effect::NoEffectPass);
reg.register_late_lint_pass(box map_clone::MapClonePass);
+ reg.register_late_lint_pass(box temporary_assignment::TemporaryAssignmentPass);
reg.register_lint_group("clippy_pedantic", vec![
methods::OPTION_UNWRAP_USED,
ranges::RANGE_ZIP_WITH_LEN,
returns::LET_AND_RETURN,
returns::NEEDLESS_RETURN,
+ temporary_assignment::TEMPORARY_ASSIGNMENT,
types::BOX_VEC,
types::LET_UNIT_VALUE,
types::LINKEDLIST,
--- /dev/null
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc_front::hir::{Expr, ExprAssign, ExprField, ExprStruct, ExprTup, ExprTupField};
+
+use utils::is_adjusted;
+use utils::span_lint;
+
+declare_lint! {
+ pub TEMPORARY_ASSIGNMENT,
+ Warn,
+ "assignments to temporaries"
+}
+
+fn is_temporary(expr: &Expr) -> bool {
+ match expr.node {
+ ExprStruct(..) |
+ ExprTup(..) => true,
+ _ => false,
+ }
+}
+
+#[derive(Copy, Clone)]
+pub struct TemporaryAssignmentPass;
+
+impl LintPass for TemporaryAssignmentPass {
+ fn get_lints(&self) -> LintArray {
+ lint_array!(TEMPORARY_ASSIGNMENT)
+ }
+}
+
+impl LateLintPass for TemporaryAssignmentPass {
+ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
+ if let ExprAssign(ref target, _) = expr.node {
+ match target.node {
+ ExprField(ref base, _) | ExprTupField(ref base, _) => {
+ if is_temporary(base) && !is_adjusted(cx, base) {
+ span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span,
+ "assignment to temporary");
+ }
+ }
+ _ => ()
+ }
+ }
+ }
+}
--- /dev/null
+#![feature(plugin)]
+#![plugin(clippy)]
+
+#![deny(temporary_assignment)]
+
+use std::ops::{Deref, DerefMut};
+
+struct Struct {
+ field: i32
+}
+
+struct Wrapper<'a> {
+ inner: &'a mut Struct
+}
+
+impl<'a> Deref for Wrapper<'a> {
+ type Target = Struct;
+ fn deref(&self) -> &Struct { self.inner }
+}
+
+impl<'a> DerefMut for Wrapper<'a> {
+ fn deref_mut(&mut self) -> &mut Struct { self.inner }
+}
+
+fn main() {
+ let mut s = Struct { field: 0 };
+ let mut t = (0, 0);
+
+ Struct { field: 0 }.field = 1; //~ERROR assignment to temporary
+ (0, 0).0 = 1; //~ERROR assignment to temporary
+
+ // no error
+ s.field = 1;
+ t.0 = 1;
+ Wrapper { inner: &mut s }.field = 1;
+}