Skip to content

Commit e70817e

Browse files
committed
Update tests and add known problems to docs
1 parent 4a4f998 commit e70817e

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15241524
LintId::of(&types::UNIT_CMP),
15251525
LintId::of(&types::UNNECESSARY_CAST),
15261526
LintId::of(&types::VEC_BOX),
1527+
LintId::of(&undropped_manually_drops::UNDROPPED_MANUALLY_DROPS),
15271528
LintId::of(&unicode::INVISIBLE_CHARACTERS),
15281529
LintId::of(&unit_return_expecting_ord::UNIT_RETURN_EXPECTING_ORD),
15291530
LintId::of(&unnamed_address::FN_ADDRESS_COMPARISONS),

clippy_lints/src/undropped_manually_drops.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
use rustc_lint::{LateLintPass, LateContext};
1+
use crate::utils::{is_type_lang_item, match_function_call, paths, span_lint_and_help};
2+
use rustc_hir::{lang_items, Expr};
3+
use rustc_lint::{LateContext, LateLintPass};
24
use rustc_session::{declare_lint_pass, declare_tool_lint};
3-
use rustc_hir::*;
4-
use crate::utils::{match_function_call, is_type_lang_item, paths, span_lint_and_help};
55

66
declare_clippy_lint! {
77
/// **What it does:** Prevents the safe `std::mem::drop` function from being called on `std::mem::ManuallyDrop`.
88
///
99
/// **Why is this bad?** The safe `drop` function does not drop the inner value of a `ManuallyDrop`.
1010
///
11-
/// **Known problems:** None.
11+
/// **Known problems:** Does not catch cases if the user binds `std::mem::drop`
12+
/// to a different name and calls it that way.
1213
///
1314
/// **Example:**
1415
///
@@ -20,7 +21,7 @@ declare_clippy_lint! {
2021
/// ```rust
2122
/// struct S;
2223
/// unsafe {
23-
/// std::mem::ManuallyDrop::drop(std::mem::ManuallyDrop::new(S));
24+
/// std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
2425
/// }
2526
/// ```
2627
pub UNDROPPED_MANUALLY_DROPS,
@@ -41,9 +42,9 @@ impl LateLintPass<'tcx> for UndroppedManuallyDrops {
4142
expr.span,
4243
"the inner value of this ManuallyDrop will not be dropped",
4344
None,
44-
"to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop"
45+
"to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop",
4546
);
4647
}
4748
}
4849
}
49-
}
50+
}

tests/ui/undropped_manually_drops.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
struct S;
44

55
fn main() {
6-
let f = drop;
7-
let manual = std::mem::ManuallyDrop::new(S);
6+
let f = std::mem::drop;
7+
let g = std::mem::ManuallyDrop::drop;
8+
let mut manual1 = std::mem::ManuallyDrop::new(S);
9+
let mut manual2 = std::mem::ManuallyDrop::new(S);
10+
let mut manual3 = std::mem::ManuallyDrop::new(S);
11+
let mut manual4 = std::mem::ManuallyDrop::new(S);
812

9-
// These lines will not drop `S`
13+
// These lines will not drop `S` and should be linted
1014
drop(std::mem::ManuallyDrop::new(S));
11-
f(manual);
15+
drop(manual1);
1216

13-
// These lines will
17+
// FIXME: this line is not linted, though it should be
18+
f(manual2);
19+
20+
// These lines will drop `S` and should be okay.
1421
unsafe {
15-
std::mem::ManuallyDrop::drop(std::mem::ManuallyDrop::new(S));
16-
std::mem::ManuallyDrop::drop(manual);
22+
std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
23+
std::mem::ManuallyDrop::drop(&mut manual3);
24+
g(&mut manual4);
1725
}
1826
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: the inner value of this ManuallyDrop will not be dropped
2+
--> $DIR/undropped_manually_drops.rs:14:5
3+
|
4+
LL | drop(std::mem::ManuallyDrop::new(S));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::undropped-manually-drops` implied by `-D warnings`
8+
= help: to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop
9+
10+
error: the inner value of this ManuallyDrop will not be dropped
11+
--> $DIR/undropped_manually_drops.rs:15:5
12+
|
13+
LL | drop(manual1);
14+
| ^^^^^^^^^^^^^
15+
|
16+
= help: to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)