about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-06-09 12:44:23 +0000
committerbors <bors@rust-lang.org>2023-06-09 12:44:23 +0000
commitd7ad9d9797e595e9daed98c291a8eb6e4be838ff (patch)
tree6fe254364a22c6d35a5b45db60e8cab0d7fb78cf /tests
parent343ad6f0596fa3222f3168b74b3e8571066e77bb (diff)
parentd9d1c76ded359089e97b3bb30c5b26ffd5c17396 (diff)
downloadrust-d7ad9d9797e595e9daed98c291a8eb6e4be838ff.tar.gz
rust-d7ad9d9797e595e9daed98c291a8eb6e4be838ff.zip
Auto merge of #111530 - Urgau:uplift_undropped_manually_drops, r=compiler-errors
Uplift `clippy::undropped_manually_drops` lint

This PR aims at uplifting the `clippy::undropped_manually_drops` lint.

## `undropped_manually_drops`

(warn-by-default)

The `undropped_manually_drops` lint check for calls to `std::mem::drop` with a value of `std::mem::ManuallyDrop` which doesn't drop.

### Example

```rust
struct S;
drop(std::mem::ManuallyDrop::new(S));
```

### Explanation

`ManuallyDrop` does not drop it's inner value so calling `std::mem::drop` will not drop the inner value of the `ManuallyDrop` either.

-----

Mostly followed the instructions for uplifting an clippy lint described here: https://github.com/rust-lang/rust/pull/99696#pullrequestreview-1134072751

`@rustbot` label: +I-lang-nominated
r? compiler

-----

For Clippy:

changelog: Moves: Uplifted `clippy::undropped_manually_drops` into rustc
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/lint/undropped_manually_drops.rs19
-rw-r--r--tests/ui/lint/undropped_manually_drops.stderr42
2 files changed, 61 insertions, 0 deletions
diff --git a/tests/ui/lint/undropped_manually_drops.rs b/tests/ui/lint/undropped_manually_drops.rs
new file mode 100644
index 00000000000..7286121a404
--- /dev/null
+++ b/tests/ui/lint/undropped_manually_drops.rs
@@ -0,0 +1,19 @@
+// check-fail
+
+struct S;
+
+fn main() {
+    let mut manual1 = std::mem::ManuallyDrop::new(S);
+    let mut manual2 = std::mem::ManuallyDrop::new(S);
+    let mut manual3 = std::mem::ManuallyDrop::new(S);
+
+    drop(std::mem::ManuallyDrop::new(S)); //~ ERROR calls to `std::mem::drop`
+    drop(manual1); //~ ERROR calls to `std::mem::drop`
+    drop({ manual3 }); //~ ERROR calls to `std::mem::drop`
+
+    // These lines will drop `S` and should be okay.
+    unsafe {
+        std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
+        std::mem::ManuallyDrop::drop(&mut manual2);
+    }
+}
diff --git a/tests/ui/lint/undropped_manually_drops.stderr b/tests/ui/lint/undropped_manually_drops.stderr
new file mode 100644
index 00000000000..156b647ebd3
--- /dev/null
+++ b/tests/ui/lint/undropped_manually_drops.stderr
@@ -0,0 +1,42 @@
+error: calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing
+  --> $DIR/undropped_manually_drops.rs:10:5
+   |
+LL |     drop(std::mem::ManuallyDrop::new(S));
+   |     ^^^^^------------------------------^
+   |          |
+   |          argument has type `ManuallyDrop<S>`
+   |
+   = note: `#[deny(undropped_manually_drops)]` on by default
+help: use `std::mem::ManuallyDrop::into_inner` to get the inner value
+   |
+LL |     drop(std::mem::ManuallyDrop::into_inner(std::mem::ManuallyDrop::new(S)));
+   |          +++++++++++++++++++++++++++++++++++                              +
+
+error: calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing
+  --> $DIR/undropped_manually_drops.rs:11:5
+   |
+LL |     drop(manual1);
+   |     ^^^^^-------^
+   |          |
+   |          argument has type `ManuallyDrop<S>`
+   |
+help: use `std::mem::ManuallyDrop::into_inner` to get the inner value
+   |
+LL |     drop(std::mem::ManuallyDrop::into_inner(manual1));
+   |          +++++++++++++++++++++++++++++++++++       +
+
+error: calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing
+  --> $DIR/undropped_manually_drops.rs:12:5
+   |
+LL |     drop({ manual3 });
+   |     ^^^^^-----------^
+   |          |
+   |          argument has type `ManuallyDrop<S>`
+   |
+help: use `std::mem::ManuallyDrop::into_inner` to get the inner value
+   |
+LL |     drop(std::mem::ManuallyDrop::into_inner({ manual3 }));
+   |          +++++++++++++++++++++++++++++++++++           +
+
+error: aborting due to 3 previous errors
+