Skip to content

Commit cd159fd

Browse files
committed
Uplift drop-bounds lint from clippy
1 parent 7820135 commit cd159fd

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mod non_ascii_idents;
5353
mod nonstandard_style;
5454
mod passes;
5555
mod redundant_semicolon;
56+
mod traits;
5657
mod types;
5758
mod unused;
5859

@@ -75,6 +76,7 @@ use internal::*;
7576
use non_ascii_idents::*;
7677
use nonstandard_style::*;
7778
use redundant_semicolon::*;
79+
use traits::*;
7880
use types::*;
7981
use unused::*;
8082

@@ -157,6 +159,7 @@ macro_rules! late_lint_passes {
157159
MissingDebugImplementations: MissingDebugImplementations::default(),
158160
ArrayIntoIter: ArrayIntoIter,
159161
ClashingExternDeclarations: ClashingExternDeclarations::new(),
162+
DropTraitConstraints: DropTraitConstraints,
160163
]
161164
);
162165
};

compiler/rustc_lint/src/traits.rs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use crate::LateContext;
2+
use crate::LateLintPass;
3+
use crate::LintContext;
4+
use rustc_hir as hir;
5+
use rustc_span::symbol::sym;
6+
7+
declare_lint! {
8+
/// The `drop_bounds` lint checks for generics with `std::ops::Drop` as
9+
/// bounds.
10+
///
11+
/// ### Example
12+
///
13+
/// ```rust
14+
/// fn foo<T: Drop>() {}
15+
/// ```
16+
///
17+
/// {{produces}}
18+
///
19+
/// ### Explanation
20+
///
21+
/// `Drop` bounds do not really accomplish anything. A type may have
22+
/// compiler-generated drop glue without implementing the `Drop` trait
23+
/// itself. The `Drop` trait also only has one method, `Drop::drop`, and
24+
/// that function is by fiat not callable in user code. So there is really
25+
/// no use case for using `Drop` in trait bounds.
26+
///
27+
/// The most likely use case of a drop bound is to distinguish between
28+
/// types that have destructors and types that don't. Combined with
29+
/// specialization, a naive coder would write an implementation that
30+
/// assumed a type could be trivially dropped, then write a specialization
31+
/// for `T: Drop` that actually calls the destructor. Except that doing so
32+
/// is not correct; String, for example, doesn't actually implement Drop,
33+
/// but because String contains a Vec, assuming it can be trivially dropped
34+
/// will leak memory.
35+
pub DROP_BOUNDS,
36+
Warn,
37+
"bounds of the form `T: Drop` are useless"
38+
}
39+
40+
declare_lint_pass!(
41+
/// Lint for bounds of the form `T: Drop`, which usually
42+
/// indicate an attempt to emulate `std::mem::needs_drop`.
43+
DropTraitConstraints => [DROP_BOUNDS]
44+
);
45+
46+
impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
47+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
48+
use rustc_middle::ty::PredicateAtom::*;
49+
50+
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
51+
let predicates = cx.tcx.explicit_predicates_of(def_id);
52+
for &(predicate, span) in predicates.predicates {
53+
let trait_predicate = match predicate.skip_binders() {
54+
Trait(trait_predicate, _constness) => trait_predicate,
55+
_ => continue,
56+
};
57+
let def_id = trait_predicate.trait_ref.def_id;
58+
if cx.tcx.lang_items().drop_trait() == Some(def_id) {
59+
// Explicitly allow `impl Drop`, a drop-guards-as-Voldemort-type pattern.
60+
if trait_predicate.trait_ref.self_ty().is_impl_trait() {
61+
continue;
62+
}
63+
cx.struct_span_lint(DROP_BOUNDS, span, |lint| {
64+
let needs_drop = match cx.tcx.get_diagnostic_item(sym::needs_drop) {
65+
Some(needs_drop) => needs_drop,
66+
None => return,
67+
};
68+
let msg = format!(
69+
"bounds on `{}` are useless, consider instead \
70+
using `{}` to detect if a type has a destructor",
71+
predicate,
72+
cx.tcx.def_path_str(needs_drop)
73+
);
74+
lint.build(&msg).emit()
75+
});
76+
}
77+
}
78+
}
79+
}

library/core/src/mem/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ pub unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
568568
#[inline]
569569
#[stable(feature = "needs_drop", since = "1.21.0")]
570570
#[rustc_const_stable(feature = "const_needs_drop", since = "1.36.0")]
571+
#[rustc_diagnostic_item = "needs_drop"]
571572
pub const fn needs_drop<T>() -> bool {
572573
intrinsics::needs_drop::<T>()
573574
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
#![deny(drop_bounds)]
3+
// As a special exemption, `impl Drop` in the return position raises no error.
4+
// This allows a convenient way to return an unnamed drop guard.
5+
fn voldemort_type() -> impl Drop {
6+
struct Voldemort;
7+
impl Drop for Voldemort {
8+
fn drop(&mut self) {}
9+
}
10+
Voldemort
11+
}
12+
fn main() {
13+
let _ = voldemort_type();
14+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![deny(drop_bounds)]
2+
fn foo<T: Drop>() {} //~ ERROR
3+
fn bar<U>()
4+
where
5+
U: Drop, //~ ERROR
6+
{
7+
}
8+
fn baz(_x: impl Drop) {} //~ ERROR
9+
struct Foo<T: Drop> { //~ ERROR
10+
_x: T
11+
}
12+
struct Bar<U> where U: Drop { //~ ERROR
13+
_x: U
14+
}
15+
trait Baz: Drop { //~ ERROR
16+
}
17+
impl<T: Drop> Baz for T { //~ ERROR
18+
}
19+
fn main() {}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error: bounds on `T: Drop` are useless, consider instead using `std::mem::needs_drop` to detect if a type has a destructor
2+
--> $DIR/drop-bounds.rs:2:11
3+
|
4+
LL | fn foo<T: Drop>() {}
5+
| ^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/drop-bounds.rs:1:9
9+
|
10+
LL | #![deny(drop_bounds)]
11+
| ^^^^^^^^^^^
12+
13+
error: bounds on `U: Drop` are useless, consider instead using `std::mem::needs_drop` to detect if a type has a destructor
14+
--> $DIR/drop-bounds.rs:5:8
15+
|
16+
LL | U: Drop,
17+
| ^^^^
18+
19+
error: bounds on `impl Drop: Drop` are useless, consider instead using `std::mem::needs_drop` to detect if a type has a destructor
20+
--> $DIR/drop-bounds.rs:8:17
21+
|
22+
LL | fn baz(_x: impl Drop) {}
23+
| ^^^^
24+
25+
error: bounds on `T: Drop` are useless, consider instead using `std::mem::needs_drop` to detect if a type has a destructor
26+
--> $DIR/drop-bounds.rs:9:15
27+
|
28+
LL | struct Foo<T: Drop> {
29+
| ^^^^
30+
31+
error: bounds on `U: Drop` are useless, consider instead using `std::mem::needs_drop` to detect if a type has a destructor
32+
--> $DIR/drop-bounds.rs:12:24
33+
|
34+
LL | struct Bar<U> where U: Drop {
35+
| ^^^^
36+
37+
error: bounds on `Self: Drop` are useless, consider instead using `std::mem::needs_drop` to detect if a type has a destructor
38+
--> $DIR/drop-bounds.rs:15:12
39+
|
40+
LL | trait Baz: Drop {
41+
| ^^^^
42+
43+
error: bounds on `T: Drop` are useless, consider instead using `std::mem::needs_drop` to detect if a type has a destructor
44+
--> $DIR/drop-bounds.rs:17:9
45+
|
46+
LL | impl<T: Drop> Baz for T {
47+
| ^^^^
48+
49+
error: aborting due to 7 previous errors
50+

0 commit comments

Comments
 (0)