]> git.lizzy.rs Git - rust.git/commitdiff
Update tests and add known problems to docs
authorcgm616 <cgm616@me.com>
Fri, 16 Oct 2020 20:20:03 +0000 (16:20 -0400)
committercgm616 <cgm616@me.com>
Thu, 22 Oct 2020 01:21:11 +0000 (21:21 -0400)
clippy_lints/src/lib.rs
clippy_lints/src/undropped_manually_drops.rs
tests/ui/undropped_manually_drops.rs
tests/ui/undropped_manually_drops.stderr [new file with mode: 0644]

index 49ff8ad366e6f7e29d3c3bfe35c2d7715a4bb58e..97e7cfd1bb26c15814bd543b18ca5719c5c780d6 100644 (file)
@@ -1524,6 +1524,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
         LintId::of(&types::UNIT_CMP),
         LintId::of(&types::UNNECESSARY_CAST),
         LintId::of(&types::VEC_BOX),
+        LintId::of(&undropped_manually_drops::UNDROPPED_MANUALLY_DROPS),
         LintId::of(&unicode::INVISIBLE_CHARACTERS),
         LintId::of(&unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD),
         LintId::of(&unnamed_address::FN_ADDRESS_COMPARISONS),
index 48a050777b77586202bcd5c156fc6bca1627fe52..5443f1601fcbb083c8fcef94219a56a26d67cc5c 100644 (file)
@@ -1,14 +1,15 @@
-use rustc_lint::{LateLintPass, LateContext};
+use crate::utils::{is_type_lang_item, match_function_call, paths, span_lint_and_help};
+use rustc_hir::{lang_items, Expr};
+use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
-use rustc_hir::*;
-use crate::utils::{match_function_call, is_type_lang_item, paths, span_lint_and_help};
 
 declare_clippy_lint! {
     /// **What it does:** Prevents the safe `std::mem::drop` function from being called on `std::mem::ManuallyDrop`.
     ///
     /// **Why is this bad?** The safe `drop` function does not drop the inner value of a `ManuallyDrop`.
     ///
-    /// **Known problems:** None.
+    /// **Known problems:** Does not catch cases if the user binds `std::mem::drop`
+    /// to a different name and calls it that way.
     ///
     /// **Example:**
     ///
@@ -20,7 +21,7 @@
     /// ```rust
     /// struct S;
     /// unsafe {
-    ///     std::mem::ManuallyDrop::drop(std::mem::ManuallyDrop::new(S));
+    ///     std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
     /// }
     /// ```
     pub UNDROPPED_MANUALLY_DROPS,
@@ -41,9 +42,9 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
                     expr.span,
                     "the inner value of this ManuallyDrop will not be dropped",
                     None,
-                    "to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop"
+                    "to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop",
                 );
             }
         }
     }
-}
\ No newline at end of file
+}
index bea62e1751e332950b2cde90d7a90ce78d226dde..f4cfc92e1cd0259eddc426dce698b54ee4c7b61d 100644 (file)
@@ -3,16 +3,24 @@
 struct S;
 
 fn main() {
-    let f = drop;
-    let manual = std::mem::ManuallyDrop::new(S);
+    let f = std::mem::drop;
+    let g = std::mem::ManuallyDrop::drop;
+    let mut manual1 = std::mem::ManuallyDrop::new(S);
+    let mut manual2 = std::mem::ManuallyDrop::new(S);
+    let mut manual3 = std::mem::ManuallyDrop::new(S);
+    let mut manual4 = std::mem::ManuallyDrop::new(S);
 
-    // These lines will not drop `S`
+    // These lines will not drop `S` and should be linted
     drop(std::mem::ManuallyDrop::new(S));
-    f(manual);
+    drop(manual1);
 
-    // These lines will
+    // FIXME: this line is not linted, though it should be
+    f(manual2);
+
+    // These lines will drop `S` and should be okay.
     unsafe {
-        std::mem::ManuallyDrop::drop(std::mem::ManuallyDrop::new(S));
-        std::mem::ManuallyDrop::drop(manual);
+        std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
+        std::mem::ManuallyDrop::drop(&mut manual3);
+        g(&mut manual4);
     }
 }
diff --git a/tests/ui/undropped_manually_drops.stderr b/tests/ui/undropped_manually_drops.stderr
new file mode 100644 (file)
index 0000000..2ac0fe9
--- /dev/null
@@ -0,0 +1,19 @@
+error: the inner value of this ManuallyDrop will not be dropped
+  --> $DIR/undropped_manually_drops.rs:14:5
+   |
+LL |     drop(std::mem::ManuallyDrop::new(S));
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: `-D clippy::undropped-manually-drops` implied by `-D warnings`
+   = help: to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop
+
+error: the inner value of this ManuallyDrop will not be dropped
+  --> $DIR/undropped_manually_drops.rs:15:5
+   |
+LL |     drop(manual1);
+   |     ^^^^^^^^^^^^^
+   |
+   = help: to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop
+
+error: aborting due to 2 previous errors
+