Auto merge of #6181 - cgm616:undropped-manually-drops, r=flip1995
Add new lint for undropped ManuallyDrop values
Adds a new lint for the following code:
```rust
struct S;
impl Drop for S {
fn drop(&mut self) {
println!("drip drop");
}
}
fn main() {
// This will not drop the `S`!!!
drop(std::mem::ManuallyDrop::new(S));
unsafe {
// This will.
std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
}
}
```
The inner value of a `ManuallyDrop` will not be dropped unless the proper, unsafe drop function is called on it. This lint makes sure that a user does not accidently use the wrong function and forget to drop a `ManuallyDrop` value.
Fixes #5581.
---
*Please keep the line below*
changelog: none
This commit is contained in:
commit
6b01c39e64
6 changed files with 108 additions and 0 deletions
|
|
@ -314,6 +314,7 @@ mod transmute;
|
|||
mod transmuting_null;
|
||||
mod try_err;
|
||||
mod types;
|
||||
mod undropped_manually_drops;
|
||||
mod unicode;
|
||||
mod unit_return_expecting_ord;
|
||||
mod unnamed_address;
|
||||
|
|
@ -865,6 +866,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
&types::UNIT_CMP,
|
||||
&types::UNNECESSARY_CAST,
|
||||
&types::VEC_BOX,
|
||||
&undropped_manually_drops::UNDROPPED_MANUALLY_DROPS,
|
||||
&unicode::INVISIBLE_CHARACTERS,
|
||||
&unicode::NON_ASCII_LITERAL,
|
||||
&unicode::UNICODE_NOT_NFC,
|
||||
|
|
@ -1141,6 +1143,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
store.register_late_pass(move || box disallowed_method::DisallowedMethod::new(&disallowed_methods));
|
||||
store.register_early_pass(|| box asm_syntax::InlineAsmX86AttSyntax);
|
||||
store.register_early_pass(|| box asm_syntax::InlineAsmX86IntelSyntax);
|
||||
store.register_late_pass(|| box undropped_manually_drops::UndroppedManuallyDrops);
|
||||
|
||||
|
||||
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
|
||||
|
|
@ -1528,6 +1531,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),
|
||||
|
|
@ -1800,6 +1804,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
LintId::of(&types::ABSURD_EXTREME_COMPARISONS),
|
||||
LintId::of(&types::CAST_REF_TO_MUT),
|
||||
LintId::of(&types::UNIT_CMP),
|
||||
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),
|
||||
|
|
|
|||
50
clippy_lints/src/undropped_manually_drops.rs
Normal file
50
clippy_lints/src/undropped_manually_drops.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
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};
|
||||
|
||||
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:** Does not catch cases if the user binds `std::mem::drop`
|
||||
/// to a different name and calls it that way.
|
||||
///
|
||||
/// **Example:**
|
||||
///
|
||||
/// ```rust
|
||||
/// struct S;
|
||||
/// drop(std::mem::ManuallyDrop::new(S));
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// struct S;
|
||||
/// unsafe {
|
||||
/// std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
|
||||
/// }
|
||||
/// ```
|
||||
pub UNDROPPED_MANUALLY_DROPS,
|
||||
correctness,
|
||||
"use of safe `std::mem::drop` function to drop a std::mem::ManuallyDrop, which will not drop the inner value"
|
||||
}
|
||||
|
||||
declare_lint_pass!(UndroppedManuallyDrops => [UNDROPPED_MANUALLY_DROPS]);
|
||||
|
||||
impl LateLintPass<'tcx> for UndroppedManuallyDrops {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if let Some(ref args) = match_function_call(cx, expr, &paths::DROP) {
|
||||
let ty = cx.typeck_results().expr_ty(&args[0]);
|
||||
if is_type_lang_item(cx, ty, lang_items::LangItem::ManuallyDrop) {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
UNDROPPED_MANUALLY_DROPS,
|
||||
expr.span,
|
||||
"the inner value of this ManuallyDrop will not be dropped",
|
||||
None,
|
||||
"to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue