Skip to content

Commit d7ad9d9

Browse files
committed
Auto merge of rust-lang#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: rust-lang#99696 (review) `@rustbot` label: +I-lang-nominated r? compiler ----- For Clippy: changelog: Moves: Uplifted `clippy::undropped_manually_drops` into rustc
2 parents 343ad6f + d9d1c76 commit d7ad9d9

14 files changed

+192
-141
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ lint_tykind = usage of `ty::TyKind`
492492
lint_tykind_kind = usage of `ty::TyKind::<kind>`
493493
.suggestion = try using `ty::<kind>` directly
494494
495+
lint_undropped_manually_drops = calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing
496+
.label = argument has type `{$arg_ty}`
497+
.suggestion = use `std::mem::ManuallyDrop::into_inner` to get the inner value
498+
495499
lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op
496500
.label = this function will not propagate the caller location
497501

Diff for: compiler/rustc_lint/src/drop_forget_useless.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use rustc_hir::{Arm, Expr, ExprKind, Node};
2+
use rustc_middle::ty;
23
use rustc_span::sym;
34

45
use crate::{
5-
lints::{DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag},
6+
lints::{
7+
DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, UndroppedManuallyDropsDiag,
8+
UndroppedManuallyDropsSuggestion,
9+
},
610
LateContext, LateLintPass, LintContext,
711
};
812

@@ -109,7 +113,29 @@ declare_lint! {
109113
"calls to `std::mem::forget` with a value that implements Copy"
110114
}
111115

112-
declare_lint_pass!(DropForgetUseless => [DROPPING_REFERENCES, FORGETTING_REFERENCES, DROPPING_COPY_TYPES, FORGETTING_COPY_TYPES]);
116+
declare_lint! {
117+
/// The `undropped_manually_drops` lint check for calls to `std::mem::drop` with
118+
/// a value of `std::mem::ManuallyDrop` which doesn't drop.
119+
///
120+
/// ### Example
121+
///
122+
/// ```rust,compile_fail
123+
/// struct S;
124+
/// drop(std::mem::ManuallyDrop::new(S));
125+
/// ```
126+
///
127+
/// {{produces}}
128+
///
129+
/// ### Explanation
130+
///
131+
/// `ManuallyDrop` does not drop it's inner value so calling `std::mem::drop` will
132+
/// not drop the inner value of the `ManuallyDrop` either.
133+
pub UNDROPPED_MANUALLY_DROPS,
134+
Deny,
135+
"calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of it's inner value"
136+
}
137+
138+
declare_lint_pass!(DropForgetUseless => [DROPPING_REFERENCES, FORGETTING_REFERENCES, DROPPING_COPY_TYPES, FORGETTING_COPY_TYPES, UNDROPPED_MANUALLY_DROPS]);
113139

114140
impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
115141
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
@@ -134,6 +160,20 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
134160
sym::mem_forget if is_copy => {
135161
cx.emit_spanned_lint(FORGETTING_COPY_TYPES, expr.span, ForgetCopyDiag { arg_ty, label: arg.span });
136162
}
163+
sym::mem_drop if let ty::Adt(adt, _) = arg_ty.kind() && adt.is_manually_drop() => {
164+
cx.emit_spanned_lint(
165+
UNDROPPED_MANUALLY_DROPS,
166+
expr.span,
167+
UndroppedManuallyDropsDiag {
168+
arg_ty,
169+
label: arg.span,
170+
suggestion: UndroppedManuallyDropsSuggestion {
171+
start_span: arg.span.shrink_to_lo(),
172+
end_span: arg.span.shrink_to_hi()
173+
}
174+
}
175+
);
176+
}
137177
_ => return,
138178
};
139179
}

Diff for: compiler/rustc_lint/src/lints.rs

+19
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,25 @@ pub struct ForgetCopyDiag<'a> {
699699
pub label: Span,
700700
}
701701

702+
#[derive(LintDiagnostic)]
703+
#[diag(lint_undropped_manually_drops)]
704+
pub struct UndroppedManuallyDropsDiag<'a> {
705+
pub arg_ty: Ty<'a>,
706+
#[label]
707+
pub label: Span,
708+
#[subdiagnostic]
709+
pub suggestion: UndroppedManuallyDropsSuggestion,
710+
}
711+
712+
#[derive(Subdiagnostic)]
713+
#[multipart_suggestion(lint_suggestion, applicability = "machine-applicable")]
714+
pub struct UndroppedManuallyDropsSuggestion {
715+
#[suggestion_part(code = "std::mem::ManuallyDrop::into_inner(")]
716+
pub start_span: Span,
717+
#[suggestion_part(code = ")")]
718+
pub end_span: Span,
719+
}
720+
702721
// invalid_from_utf8.rs
703722
#[derive(LintDiagnostic)]
704723
pub enum InvalidFromUtf8Diag {

Diff for: library/core/tests/manually_drop.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(not(bootstrap), allow(undropped_manually_drops))]
2+
13
use core::mem::ManuallyDrop;
24

35
#[test]

Diff for: src/tools/clippy/clippy_lints/src/declared_lints.rs

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
136136
crate::double_parens::DOUBLE_PARENS_INFO,
137137
crate::drop_forget_ref::DROP_NON_DROP_INFO,
138138
crate::drop_forget_ref::FORGET_NON_DROP_INFO,
139-
crate::drop_forget_ref::UNDROPPED_MANUALLY_DROPS_INFO,
140139
crate::duplicate_mod::DUPLICATE_MOD_INFO,
141140
crate::else_if_without_else::ELSE_IF_WITHOUT_ELSE_INFO,
142141
crate::empty_drop::EMPTY_DROP_INFO,

Diff for: src/tools/clippy/clippy_lints/src/drop_forget_ref.rs

+2-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
1+
use clippy_utils::diagnostics::span_lint_and_note;
22
use clippy_utils::get_parent_node;
33
use clippy_utils::is_must_use_func_call;
44
use clippy_utils::ty::{is_copy, is_must_use_ty, is_type_lang_item};
@@ -47,35 +47,6 @@ declare_clippy_lint! {
4747
"call to `std::mem::forget` with a value which does not implement `Drop`"
4848
}
4949

50-
declare_clippy_lint! {
51-
/// ### What it does
52-
/// Prevents the safe `std::mem::drop` function from being called on `std::mem::ManuallyDrop`.
53-
///
54-
/// ### Why is this bad?
55-
/// The safe `drop` function does not drop the inner value of a `ManuallyDrop`.
56-
///
57-
/// ### Known problems
58-
/// Does not catch cases if the user binds `std::mem::drop`
59-
/// to a different name and calls it that way.
60-
///
61-
/// ### Example
62-
/// ```rust
63-
/// struct S;
64-
/// drop(std::mem::ManuallyDrop::new(S));
65-
/// ```
66-
/// Use instead:
67-
/// ```rust
68-
/// struct S;
69-
/// unsafe {
70-
/// std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
71-
/// }
72-
/// ```
73-
#[clippy::version = "1.49.0"]
74-
pub UNDROPPED_MANUALLY_DROPS,
75-
correctness,
76-
"use of safe `std::mem::drop` function to drop a std::mem::ManuallyDrop, which will not drop the inner value"
77-
}
78-
7950
const DROP_NON_DROP_SUMMARY: &str = "call to `std::mem::drop` with a value that does not implement `Drop`. \
8051
Dropping such a type only extends its contained lifetimes";
8152
const FORGET_NON_DROP_SUMMARY: &str = "call to `std::mem::forget` with a value that does not implement `Drop`. \
@@ -84,7 +55,6 @@ const FORGET_NON_DROP_SUMMARY: &str = "call to `std::mem::forget` with a value t
8455
declare_lint_pass!(DropForgetRef => [
8556
DROP_NON_DROP,
8657
FORGET_NON_DROP,
87-
UNDROPPED_MANUALLY_DROPS
8858
]);
8959

9060
impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
@@ -103,17 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
10373
sym::mem_forget if arg_ty.is_ref() => return,
10474
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => return,
10575
sym::mem_forget if is_copy => return,
106-
sym::mem_drop if is_type_lang_item(cx, arg_ty, LangItem::ManuallyDrop) => {
107-
span_lint_and_help(
108-
cx,
109-
UNDROPPED_MANUALLY_DROPS,
110-
expr.span,
111-
"the inner value of this ManuallyDrop will not be dropped",
112-
None,
113-
"to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop",
114-
);
115-
return;
116-
}
76+
sym::mem_drop if is_type_lang_item(cx, arg_ty, LangItem::ManuallyDrop) => return,
11777
sym::mem_drop
11878
if !(arg_ty.needs_drop(cx.tcx, cx.param_env)
11979
|| is_must_use_func_call(cx, arg)

Diff for: src/tools/clippy/clippy_lints/src/renamed_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
5050
("clippy::panic_params", "non_fmt_panics"),
5151
("clippy::positional_named_format_parameters", "named_arguments_used_positionally"),
5252
("clippy::temporary_cstring_as_ptr", "temporary_cstring_as_ptr"),
53+
("clippy::undropped_manually_drops", "undropped_manually_drops"),
5354
("clippy::unknown_clippy_lints", "unknown_lints"),
5455
("clippy::unused_label", "unused_labels"),
5556
];

Diff for: src/tools/clippy/tests/ui/rename.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#![allow(named_arguments_used_positionally)]
4848
#![allow(suspicious_double_ref_op)]
4949
#![allow(temporary_cstring_as_ptr)]
50+
#![allow(undropped_manually_drops)]
5051
#![allow(unknown_lints)]
5152
#![allow(unused_labels)]
5253
#![warn(clippy::almost_complete_range)]
@@ -97,6 +98,7 @@
9798
#![warn(non_fmt_panics)]
9899
#![warn(named_arguments_used_positionally)]
99100
#![warn(temporary_cstring_as_ptr)]
101+
#![warn(undropped_manually_drops)]
100102
#![warn(unknown_lints)]
101103
#![warn(unused_labels)]
102104

Diff for: src/tools/clippy/tests/ui/rename.rs

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#![allow(named_arguments_used_positionally)]
4848
#![allow(suspicious_double_ref_op)]
4949
#![allow(temporary_cstring_as_ptr)]
50+
#![allow(undropped_manually_drops)]
5051
#![allow(unknown_lints)]
5152
#![allow(unused_labels)]
5253
#![warn(clippy::almost_complete_letter_range)]
@@ -97,6 +98,7 @@
9798
#![warn(clippy::panic_params)]
9899
#![warn(clippy::positional_named_format_parameters)]
99100
#![warn(clippy::temporary_cstring_as_ptr)]
101+
#![warn(clippy::undropped_manually_drops)]
100102
#![warn(clippy::unknown_clippy_lints)]
101103
#![warn(clippy::unused_label)]
102104

0 commit comments

Comments
 (0)