summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/unnecessary_struct_initialization.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
commit1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch)
tree3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /src/tools/clippy/tests/ui/unnecessary_struct_initialization.rs
parentReleasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz
rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/unnecessary_struct_initialization.rs')
-rw-r--r--src/tools/clippy/tests/ui/unnecessary_struct_initialization.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/unnecessary_struct_initialization.rs b/src/tools/clippy/tests/ui/unnecessary_struct_initialization.rs
new file mode 100644
index 000000000..63b11c626
--- /dev/null
+++ b/src/tools/clippy/tests/ui/unnecessary_struct_initialization.rs
@@ -0,0 +1,77 @@
+// 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 { ..*self }
+ }
+}
+
+fn main() {
+ // Should lint: `a` would be consumed anyway
+ let a = S { f: String::from("foo") };
+ let mut b = S { ..a };
+
+ // Should lint: `b` would be consumed, and is mutable
+ let c = &mut S { ..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 = &S { ..f };
+
+ // Should lint: the result of an expression is mutable
+ let h = &mut S {
+ ..*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 T {
+ ..*Box::new(T { f: 5 })
+ };
+}