Skip to content

Commit bbfe1c1

Browse files
committed
lint implied bounds in APIT
1 parent ec29b0d commit bbfe1c1

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

clippy_lints/src/implied_bounds_in_impls.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use rustc_errors::{Applicability, SuggestionStyle};
44
use rustc_hir::def_id::DefId;
5-
use rustc_hir::{GenericArg, GenericBound, GenericBounds, ItemKind, TraitBoundModifier, TyKind, TypeBinding};
5+
use rustc_hir::{
6+
GenericArg, GenericBound, GenericBounds, ItemKind, PredicateOrigin, TraitBoundModifier, TyKind, TypeBinding,
7+
WherePredicate,
8+
};
69
use rustc_hir_analysis::hir_ty_to_ty;
710
use rustc_lint::{LateContext, LateLintPass};
811
use rustc_middle::ty::{self, ClauseKind, Generics, Ty, TyCtxt};
@@ -326,6 +329,19 @@ fn check<'tcx>(cx: &LateContext<'tcx>, bounds: GenericBounds<'tcx>) {
326329
}
327330

328331
impl<'tcx> LateLintPass<'tcx> for ImpliedBoundsInImpls {
332+
fn check_generics(&mut self, cx: &LateContext<'tcx>, generics: &rustc_hir::Generics<'tcx>) {
333+
for predicate in generics.predicates {
334+
if let WherePredicate::BoundPredicate(predicate) = predicate
335+
// In theory, the origin doesn't really matter,
336+
// we *could* also lint on explicit where clauses written out by the user,
337+
// not just impl trait desugared ones, but that contradicts with the lint name...
338+
&& let PredicateOrigin::ImplTrait = predicate.origin
339+
{
340+
check(cx, predicate.bounds);
341+
}
342+
}
343+
}
344+
329345
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &rustc_hir::Ty<'_>) {
330346
if let TyKind::OpaqueDef(item_id, ..) = ty.kind
331347
&& let item = cx.tcx.hir().item(item_id)

tests/ui/implied_bounds_in_impls.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn issue11880() {
151151
fn f5() -> impl Y<T = u32, U = String> {}
152152
}
153153

154-
fn apit(_: impl Deref + DerefMut) {}
154+
fn apit(_: impl DerefMut) {}
155155

156156
trait Rpitit {
157157
fn f() -> impl DerefMut;

tests/ui/implied_bounds_in_impls.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,18 @@ LL - fn f5() -> impl X<U = String> + Y<T = u32> {}
228228
LL + fn f5() -> impl Y<T = u32, U = String> {}
229229
|
230230

231+
error: this bound is already specified as the supertrait of `DerefMut`
232+
--> tests/ui/implied_bounds_in_impls.rs:154:17
233+
|
234+
LL | fn apit(_: impl Deref + DerefMut) {}
235+
| ^^^^^
236+
|
237+
help: try removing this bound
238+
|
239+
LL - fn apit(_: impl Deref + DerefMut) {}
240+
LL + fn apit(_: impl DerefMut) {}
241+
|
242+
231243
error: this bound is already specified as the supertrait of `DerefMut`
232244
--> tests/ui/implied_bounds_in_impls.rs:157:20
233245
|
@@ -264,5 +276,5 @@ LL - type Tait = impl Deref + DerefMut;
264276
LL + type Tait = impl DerefMut;
265277
|
266278

267-
error: aborting due to 22 previous errors
279+
error: aborting due to 23 previous errors
268280

0 commit comments

Comments
 (0)