-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add a lint to warn on T: Drop
bounds
#3776
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,79 @@ | ||
use crate::utils::{match_def_path, paths, span_lint}; | ||
use if_chain::if_chain; | ||
use rustc::hir::*; | ||
use rustc::lint::{LateLintPass, LintArray, LintPass}; | ||
use rustc::{declare_tool_lint, lint_array}; | ||
|
||
/// **What it does:** Checks for generics with `std::ops::Drop` as bounds. | ||
/// | ||
/// **Why is this bad?** `Drop` bounds do not really accomplish anything. | ||
/// A type may have compiler-generated drop glue without implementing the | ||
/// `Drop` trait itself. The `Drop` trait also only has one method, | ||
/// `Drop::drop`, and that function is by fiat not callable in user code. | ||
/// So there is really no use case for using `Drop` in trait bounds. | ||
/// | ||
/// The most likely use case of a drop bound is to distinguish between types | ||
/// that have destructors and types that don't. Combined with specialization, | ||
/// a naive coder would write an implementation that assumed a type could be | ||
/// trivially dropped, then write a specialization for `T: Drop` that actually | ||
/// calls the destructor. Except that doing so is not correct; String, for | ||
/// example, doesn't actually implement Drop, but because String contains a | ||
/// Vec, assuming it can be trivially dropped will leak memory. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// fn foo<T: Drop>() {} | ||
/// ``` | ||
declare_clippy_lint! { | ||
pub DROP_BOUNDS, | ||
correctness, | ||
notriddle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Bounds of the form `T: Drop` are useless" | ||
} | ||
|
||
const DROP_BOUNDS_SUMMARY: &str = "Bounds of the form `T: Drop` are useless. \ | ||
Use `std::mem::needs_drop` to detect if a type has drop glue."; | ||
|
||
pub struct Pass; | ||
|
||
impl LintPass for Pass { | ||
fn get_lints(&self) -> LintArray { | ||
lint_array!(DROP_BOUNDS) | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"DropBounds" | ||
} | ||
} | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { | ||
fn check_generic_param(&mut self, cx: &rustc::lint::LateContext<'a, 'tcx>, p: &'tcx GenericParam) { | ||
for bound in &p.bounds { | ||
lint_bound(cx, bound); | ||
} | ||
} | ||
fn check_where_predicate(&mut self, cx: &rustc::lint::LateContext<'a, 'tcx>, p: &'tcx WherePredicate) { | ||
if let WherePredicate::BoundPredicate(WhereBoundPredicate { bounds, .. }) = p { | ||
for bound in bounds { | ||
lint_bound(cx, bound); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn lint_bound<'a, 'tcx>(cx: &rustc::lint::LateContext<'a, 'tcx>, bound: &'tcx GenericBound) { | ||
if_chain! { | ||
if let GenericBound::Trait(t, _) = bound; | ||
if let Some(def_id) = t.trait_ref.path.def.opt_def_id(); | ||
if match_def_path(cx.tcx, def_id, &paths::DROP_TRAIT); | ||
then { | ||
span_lint( | ||
cx, | ||
DROP_BOUNDS, | ||
t.span, | ||
DROP_BOUNDS_SUMMARY | ||
); | ||
} | ||
} | ||
} |
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,8 @@ | ||
#![allow(unused)] | ||
fn foo<T: Drop>() {} | ||
fn bar<T>() | ||
where | ||
T: Drop, | ||
{ | ||
} | ||
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,16 @@ | ||
error: Bounds of the form `T: Drop` are useless. Use `std::mem::needs_drop` to detect if a type has drop glue. | ||
--> $DIR/drop_bounds.rs:2:11 | ||
| | ||
LL | fn foo<T: Drop>() {} | ||
| ^^^^ | ||
| | ||
= note: #[deny(clippy::drop_bounds)] on by default | ||
|
||
error: Bounds of the form `T: Drop` are useless. Use `std::mem::needs_drop` to detect if a type has drop glue. | ||
--> $DIR/drop_bounds.rs:5:8 | ||
| | ||
LL | T: Drop, | ||
| ^^^^ | ||
|
||
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.