Skip to content

Commit 94fb1bb

Browse files
committed
Run lint on where bounds
1 parent 7680c79 commit 94fb1bb

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

clippy_lints/src/drop_bounds.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::utils::{match_def_path, paths, span_lint};
22
use if_chain::if_chain;
33
use rustc::hir::*;
4-
use rustc::hir::GenericBound::Trait;
54
use rustc::lint::{LateLintPass, LintArray, LintPass};
65
use rustc::{declare_tool_lint, lint_array};
76

@@ -43,19 +42,30 @@ impl LintPass for Pass {
4342
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
4443
fn check_generic_param(&mut self, cx: &rustc::lint::LateContext<'a, 'tcx>, p: &'tcx GenericParam) {
4544
for bound in &p.bounds {
46-
if_chain! {
47-
if let Trait(t, _) = bound;
48-
if let Some(def_id) = t.trait_ref.path.def.opt_def_id();
49-
if match_def_path(cx.tcx, def_id, &paths::DROP_TRAIT);
50-
then {
51-
span_lint(
52-
cx,
53-
DROP_BOUNDS,
54-
t.span,
55-
&DROP_BOUNDS_SUMMARY.to_string()
56-
);
57-
}
45+
lint_bound(cx, bound);
46+
}
47+
}
48+
fn check_where_predicate(&mut self, cx: &rustc::lint::LateContext<'a, 'tcx>, p: &'tcx WherePredicate) {
49+
if let WherePredicate::BoundPredicate(WhereBoundPredicate{bounds, ..}) = p {
50+
for bound in bounds {
51+
lint_bound(cx, bound);
5852
}
5953
}
6054
}
6155
}
56+
57+
fn lint_bound<'a, 'tcx>(cx: &rustc::lint::LateContext<'a, 'tcx>, bound: &'tcx GenericBound) {
58+
if_chain! {
59+
if let GenericBound::Trait(t, _) = bound;
60+
if let Some(def_id) = t.trait_ref.path.def.opt_def_id();
61+
if match_def_path(cx.tcx, def_id, &paths::DROP_TRAIT);
62+
then {
63+
span_lint(
64+
cx,
65+
DROP_BOUNDS,
66+
t.span,
67+
DROP_BOUNDS_SUMMARY
68+
);
69+
}
70+
}
71+
}

tests/ui/drop_bounds.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1+
#![allow(unused)]
12
fn foo<T: Drop>() {}
2-
struct K;
3-
impl Drop for K {
4-
fn drop(&mut self) {}
5-
}
6-
fn main() {
7-
foo::<K>();
8-
}
3+
fn bar<T>() where T: Drop {}
4+
fn main() {}

tests/ui/drop_bounds.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
error: Bounds of the form `T: Drop` are useless. Use `std::mem::needs_drop` to detect if a type has drop glue.
2-
--> $DIR/drop_bounds.rs:1:11
2+
--> $DIR/drop_bounds.rs:2:11
33
|
44
LL | fn foo<T: Drop>() {}
55
| ^^^^
66
|
77
= note: #[deny(clippy::drop_bounds)] on by default
88

9-
error: aborting due to previous error
9+
error: Bounds of the form `T: Drop` are useless. Use `std::mem::needs_drop` to detect if a type has drop glue.
10+
--> $DIR/drop_bounds.rs:3:22
11+
|
12+
LL | fn bar<T>() where T: Drop {}
13+
| ^^^^
14+
15+
error: aborting due to 2 previous errors
1016

0 commit comments

Comments
 (0)