about summary refs log tree commit diff
path: root/tests/ui/unnecessary_struct_initialization.fixed
diff options
context:
space:
mode:
authorPhilipp Krones <hello@philkrones.com>2023-03-24 14:04:35 +0100
committerPhilipp Krones <hello@philkrones.com>2023-03-24 14:26:19 +0100
commit8df896c076fd993bad58878ee8a6ed29d8e586ba (patch)
treec0edd67687a954bb38d66e77dae3dbd0db3909c5 /tests/ui/unnecessary_struct_initialization.fixed
parent58eb9964cc627470cdd9fdcdef872a45615227fe (diff)
downloadrust-8df896c076fd993bad58878ee8a6ed29d8e586ba.tar.gz
rust-8df896c076fd993bad58878ee8a6ed29d8e586ba.zip
Merge commit 'd5e2a7aca55ed49fc943b7a07a8eba05ab5a0079' into clippyup
Diffstat (limited to 'tests/ui/unnecessary_struct_initialization.fixed')
-rw-r--r--tests/ui/unnecessary_struct_initialization.fixed73
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/ui/unnecessary_struct_initialization.fixed b/tests/ui/unnecessary_struct_initialization.fixed
new file mode 100644
index 00000000000..b47129e4a36
--- /dev/null
+++ b/tests/ui/unnecessary_struct_initialization.fixed
@@ -0,0 +1,73 @@
+// run-rustfix
+
+#![allow(unused)]
+#![warn(clippy::unnecessary_struct_initialization)]
+
+struct S {
+    f: String,
+}
+
+#[derive(Clone, Copy)]
+struct T {
+    f: u32,
+}
+
+struct U {
+    f: u32,
+}
+
+impl Clone for U {
+    fn clone(&self) -> Self {
+        // Do not lint: `Self` does not implement `Copy`
+        Self { ..*self }
+    }
+}
+
+#[derive(Copy)]
+struct V {
+    f: u32,
+}
+
+impl Clone for V {
+    fn clone(&self) -> Self {
+        // Lint: `Self` implements `Copy`
+        *self
+    }
+}
+
+fn main() {
+    // Should lint: `a` would be consumed anyway
+    let a = S { f: String::from("foo") };
+    let mut b = a;
+
+    // Should lint: `b` would be consumed, and is mutable
+    let c = &mut b;
+
+    // Should not lint as `d` is not mutable
+    let d = S { f: String::from("foo") };
+    let e = &mut S { ..d };
+
+    // Should lint as `f` would be consumed anyway
+    let f = S { f: String::from("foo") };
+    let g = &f;
+
+    // Should lint: the result of an expression is mutable
+    let h = &mut *Box::new(S { f: String::from("foo") });
+
+    // Should not lint: `m` would be both alive and borrowed
+    let m = T { f: 17 };
+    let n = &T { ..m };
+
+    // Should not lint: `m` should not be modified
+    let o = &mut T { ..m };
+    o.f = 32;
+    assert_eq!(m.f, 17);
+
+    // Should not lint: `m` should not be modified
+    let o = &mut T { ..m } as *mut T;
+    unsafe { &mut *o }.f = 32;
+    assert_eq!(m.f, 17);
+
+    // Should lint: the result of an expression is mutable and temporary
+    let p = &mut *Box::new(T { f: 5 });
+}