Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uplift clippy::undropped_manually_drops lint #111530

Merged
merged 3 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Uplift clippy::undropped_manually_drops to rustc
  • Loading branch information
Urgau committed Jun 8, 2023
commit 52300bf8d859a81ce73b13e87deb408473a83324
4 changes: 4 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 492,10 @@ lint_tykind = usage of `ty::TyKind`
lint_tykind_kind = usage of `ty::TyKind::<kind>`
.suggestion = try using `ty::<kind>` directly

lint_undropped_manually_drops = calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing
.label = argument has type `{$arg_ty}`
.suggestion = use `std::mem::ManuallyDrop::into_inner` to get the inner value

lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op
.label = this function will not propagate the caller location

Expand Down
44 changes: 42 additions & 2 deletions compiler/rustc_lint/src/drop_forget_useless.rs
Original file line number Diff line number Diff line change
@@ -1,8 1,12 @@
use rustc_hir::{Arm, Expr, ExprKind, Node};
use rustc_middle::ty;
use rustc_span::sym;

use crate::{
lints::{DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag},
lints::{
DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, UndroppedManuallyDropsDiag,
UndroppedManuallyDropsSuggestion,
},
LateContext, LateLintPass, LintContext,
};

Expand Down Expand Up @@ -109,7 113,29 @@ declare_lint! {
"calls to `std::mem::forget` with a value that implements Copy"
}

declare_lint_pass!(DropForgetUseless => [DROPPING_REFERENCES, FORGETTING_REFERENCES, DROPPING_COPY_TYPES, FORGETTING_COPY_TYPES]);
declare_lint! {
/// 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,compile_fail
/// struct S;
/// drop(std::mem::ManuallyDrop::new(S));
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// `ManuallyDrop` does not drop it's inner value so calling `std::mem::drop` will
/// not drop the inner value of the `ManuallyDrop` either.
pub UNDROPPED_MANUALLY_DROPS,
Deny,
"calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of it's inner value"
}

declare_lint_pass!(DropForgetUseless => [DROPPING_REFERENCES, FORGETTING_REFERENCES, DROPPING_COPY_TYPES, FORGETTING_COPY_TYPES, UNDROPPED_MANUALLY_DROPS]);

impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
Expand All @@ -134,6 160,20 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
sym::mem_forget if is_copy => {
cx.emit_spanned_lint(FORGETTING_COPY_TYPES, expr.span, ForgetCopyDiag { arg_ty, label: arg.span });
}
sym::mem_drop if let ty::Adt(adt, _) = arg_ty.kind() && adt.is_manually_drop() => {
cx.emit_spanned_lint(
UNDROPPED_MANUALLY_DROPS,
expr.span,
UndroppedManuallyDropsDiag {
arg_ty,
label: arg.span,
suggestion: UndroppedManuallyDropsSuggestion {
start_span: arg.span.shrink_to_lo(),
end_span: arg.span.shrink_to_hi()
}
}
);
}
_ => return,
};
}
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 699,25 @@ pub struct ForgetCopyDiag<'a> {
pub label: Span,
}

#[derive(LintDiagnostic)]
#[diag(lint_undropped_manually_drops)]
pub struct UndroppedManuallyDropsDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub suggestion: UndroppedManuallyDropsSuggestion,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(lint_suggestion, applicability = "machine-applicable")]
pub struct UndroppedManuallyDropsSuggestion {
#[suggestion_part(code = "std::mem::ManuallyDrop::into_inner(")]
pub start_span: Span,
#[suggestion_part(code = ")")]
pub end_span: Span,
}

// invalid_from_utf8.rs
#[derive(LintDiagnostic)]
pub enum InvalidFromUtf8Diag {
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/lint/undropped_manually_drops.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
42 changes: 42 additions & 0 deletions tests/ui/lint/undropped_manually_drops.stderr
Original file line number Diff line number Diff line change
@@ -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