Skip to content

Commit b654555

Browse files
authored
Rollup merge of #75699 - notriddle:drop-bounds-lint, r=petrochenkov
Uplift drop-bounds lint from clippy Bounds on `T: Drop` do nothing, so they should warn.
2 parents 6e25418 + dceb81a commit b654555

File tree

15 files changed

+187
-111
lines changed

15 files changed

+187
-111
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+

src/tools/clippy/clippy_lints/src/deprecated_lints.rs

+9
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,12 @@ declare_deprecated_lint! {
163163
pub REGEX_MACRO,
164164
"the regex! macro has been removed from the regex crate in 2018"
165165
}
166+
167+
declare_deprecated_lint! {
168+
/// **What it does:** Nothing. This lint has been deprecated.
169+
///
170+
/// **Deprecation reason:** This lint has been uplifted to rustc and is now called
171+
/// `drop_bounds`.
172+
pub DROP_BOUNDS,
173+
"this lint has been uplifted to rustc and is now called `drop_bounds`"
174+
}

src/tools/clippy/clippy_lints/src/drop_bounds.rs

-73
This file was deleted.

src/tools/clippy/clippy_lints/src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ mod derive;
179179
mod doc;
180180
mod double_comparison;
181181
mod double_parens;
182-
mod drop_bounds;
183182
mod drop_forget_ref;
184183
mod duration_subsec;
185184
mod else_if_without_else;
@@ -478,6 +477,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
478477
"clippy::regex_macro",
479478
"the regex! macro has been removed from the regex crate in 2018",
480479
);
480+
store.register_removed(
481+
"clippy::drop_bounds",
482+
"this lint has been uplifted to rustc and is now called `drop_bounds`",
483+
);
481484
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
482485

483486
// begin register lints, do not remove this comment, it’s used in `update_lints`
@@ -532,7 +535,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
532535
&doc::NEEDLESS_DOCTEST_MAIN,
533536
&double_comparison::DOUBLE_COMPARISONS,
534537
&double_parens::DOUBLE_PARENS,
535-
&drop_bounds::DROP_BOUNDS,
536538
&drop_forget_ref::DROP_COPY,
537539
&drop_forget_ref::DROP_REF,
538540
&drop_forget_ref::FORGET_COPY,
@@ -959,7 +961,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
959961
store.register_late_pass(|| box strings::StringLitAsBytes);
960962
store.register_late_pass(|| box derive::Derive);
961963
store.register_late_pass(|| box types::CharLitAsU8);
962-
store.register_late_pass(|| box drop_bounds::DropBounds);
963964
store.register_late_pass(|| box get_last_with_len::GetLastWithLen);
964965
store.register_late_pass(|| box drop_forget_ref::DropForgetRef);
965966
store.register_late_pass(|| box empty_enum::EmptyEnum);
@@ -1282,7 +1283,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12821283
LintId::of(&doc::NEEDLESS_DOCTEST_MAIN),
12831284
LintId::of(&double_comparison::DOUBLE_COMPARISONS),
12841285
LintId::of(&double_parens::DOUBLE_PARENS),
1285-
LintId::of(&drop_bounds::DROP_BOUNDS),
12861286
LintId::of(&drop_forget_ref::DROP_COPY),
12871287
LintId::of(&drop_forget_ref::DROP_REF),
12881288
LintId::of(&drop_forget_ref::FORGET_COPY),
@@ -1714,7 +1714,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
17141714
LintId::of(&copies::IF_SAME_THEN_ELSE),
17151715
LintId::of(&derive::DERIVE_HASH_XOR_EQ),
17161716
LintId::of(&derive::DERIVE_ORD_XOR_PARTIAL_ORD),
1717-
LintId::of(&drop_bounds::DROP_BOUNDS),
17181717
LintId::of(&drop_forget_ref::DROP_COPY),
17191718
LintId::of(&drop_forget_ref::DROP_REF),
17201719
LintId::of(&drop_forget_ref::FORGET_COPY),

src/tools/clippy/clippy_lints/src/utils/paths.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub const DISPLAY_FMT_METHOD: [&str; 4] = ["core", "fmt", "Display", "fmt"];
3131
pub const DISPLAY_TRAIT: [&str; 3] = ["core", "fmt", "Display"];
3232
pub const DOUBLE_ENDED_ITERATOR: [&str; 4] = ["core", "iter", "traits", "DoubleEndedIterator"];
3333
pub const DROP: [&str; 3] = ["core", "mem", "drop"];
34-
pub const DROP_TRAIT: [&str; 4] = ["core", "ops", "drop", "Drop"];
3534
pub const DURATION: [&str; 3] = ["core", "time", "Duration"];
3635
pub const EARLY_CONTEXT: [&str; 4] = ["rustc", "lint", "context", "EarlyContext"];
3736
pub const EXIT: [&str; 3] = ["std", "process", "exit"];

src/tools/clippy/src/lintlist/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
423423
deprecation: None,
424424
module: "double_parens",
425425
},
426-
Lint {
427-
name: "drop_bounds",
428-
group: "correctness",
429-
desc: "bounds of the form `T: Drop` are useless",
430-
deprecation: None,
431-
module: "drop_bounds",
432-
},
433426
Lint {
434427
name: "drop_copy",
435428
group: "correctness",

src/tools/clippy/tests/ui/deprecated.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
#[warn(clippy::into_iter_on_array)]
99
#[warn(clippy::unused_label)]
1010
#[warn(clippy::regex_macro)]
11+
#[warn(clippy::drop_bounds)]
1112

1213
fn main() {}

src/tools/clippy/tests/ui/deprecated.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ error: lint `clippy::regex_macro` has been removed: `the regex! macro has been r
6060
LL | #[warn(clippy::regex_macro)]
6161
| ^^^^^^^^^^^^^^^^^^^
6262

63+
error: lint `clippy::drop_bounds` has been removed: `this lint has been uplifted to rustc and is now called `drop_bounds``
64+
--> $DIR/deprecated.rs:11:8
65+
|
66+
LL | #[warn(clippy::drop_bounds)]
67+
| ^^^^^^^^^^^^^^^^^^^
68+
6369
error: lint `clippy::str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
6470
--> $DIR/deprecated.rs:1:8
6571
|
6672
LL | #[warn(clippy::str_to_string)]
6773
| ^^^^^^^^^^^^^^^^^^^^^
6874

69-
error: aborting due to 11 previous errors
75+
error: aborting due to 12 previous errors
7076

src/tools/clippy/tests/ui/drop_bounds.rs

-8
This file was deleted.

0 commit comments

Comments
 (0)