-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add empty_drop
#8571
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
Merged
Merged
add empty_drop
#8571
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, peel_blocks}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Body, ExprKind, Impl, ImplItemKind, Item, ItemKind, Node}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for empty `Drop` implementations. | ||
/// | ||
/// ### Why is this bad? | ||
/// Empty `Drop` implementations have no effect when dropping an instance of the type. They are | ||
/// most likely useless. However, an empty `Drop` implementation prevents a type from being | ||
/// destructured, which might be the intention behind adding the implementation as a marker. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// struct S; | ||
/// | ||
/// impl Drop for S { | ||
/// fn drop(&mut self) {} | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// struct S; | ||
/// ``` | ||
#[clippy::version = "1.61.0"] | ||
pub EMPTY_DROP, | ||
restriction, | ||
"empty `Drop` implementations" | ||
} | ||
declare_lint_pass!(EmptyDrop => [EMPTY_DROP]); | ||
|
||
impl LateLintPass<'_> for EmptyDrop { | ||
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { | ||
if_chain! { | ||
if let ItemKind::Impl(Impl { | ||
of_trait: Some(ref trait_ref), | ||
items: [child], | ||
.. | ||
}) = item.kind; | ||
if trait_ref.trait_def_id() == cx.tcx.lang_items().drop_trait(); | ||
if let impl_item_hir = child.id.hir_id(); | ||
if let Some(Node::ImplItem(impl_item)) = cx.tcx.hir().find(impl_item_hir); | ||
if let ImplItemKind::Fn(_, b) = &impl_item.kind; | ||
if let Body { value: func_expr, .. } = cx.tcx.hir().body(*b); | ||
let func_expr = peel_blocks(func_expr); | ||
if let ExprKind::Block(block, _) = func_expr.kind; | ||
if block.stmts.is_empty() && block.expr.is_none(); | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
EMPTY_DROP, | ||
item.span, | ||
"empty drop implementation", | ||
"try removing this impl", | ||
String::new(), | ||
Applicability::MaybeIncorrect | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-rustfix | ||
#![warn(clippy::empty_drop)] | ||
#![allow(unused)] | ||
|
||
// should cause an error | ||
struct Foo; | ||
|
||
|
||
|
||
// shouldn't cause an error | ||
struct Bar; | ||
|
||
impl Drop for Bar { | ||
fn drop(&mut self) { | ||
println!("dropping bar!"); | ||
} | ||
} | ||
|
||
// should error | ||
struct Baz; | ||
|
||
|
||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// run-rustfix | ||
#![warn(clippy::empty_drop)] | ||
PyroTechniac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![allow(unused)] | ||
|
||
// should cause an error | ||
struct Foo; | ||
|
||
impl Drop for Foo { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
// shouldn't cause an error | ||
struct Bar; | ||
|
||
impl Drop for Bar { | ||
fn drop(&mut self) { | ||
println!("dropping bar!"); | ||
} | ||
} | ||
|
||
// should error | ||
struct Baz; | ||
|
||
impl Drop for Baz { | ||
fn drop(&mut self) { | ||
{} | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: empty drop implementation | ||
--> $DIR/empty_drop.rs:8:1 | ||
| | ||
LL | / impl Drop for Foo { | ||
LL | | fn drop(&mut self) {} | ||
LL | | } | ||
| |_^ help: try removing this impl | ||
| | ||
= note: `-D clippy::empty-drop` implied by `-D warnings` | ||
|
||
error: empty drop implementation | ||
--> $DIR/empty_drop.rs:24:1 | ||
| | ||
LL | / impl Drop for Baz { | ||
LL | | fn drop(&mut self) { | ||
LL | | {} | ||
LL | | } | ||
LL | | } | ||
| |_^ help: try removing this impl | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.