Skip to content

Commit bca3d8a

Browse files
committed
Track if a where bound comes from a impl Trait desugar
With rust-lang#93803 `impl Trait` function arguments get desugared to hidden where bounds. However, Clippy needs to know if a bound was originally a impl Trait or an actual bound. This adds a field to the `WhereBoundPredicate` struct to keep track of this information during HIR lowering.
1 parent ed8458f commit bca3d8a

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

clippy_lints/src/lifetimes.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use rustc_hir::intravisit::{
99
use rustc_hir::FnRetTy::Return;
1010
use rustc_hir::{
1111
BareFnTy, BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, Impl, ImplItem,
12-
ImplItemKind, Item, ItemKind, LangItem, Lifetime, LifetimeName, ParamName, PolyTraitRef, TraitBoundModifier,
13-
TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
12+
ImplItemKind, Item, ItemKind, LangItem, Lifetime, LifetimeName, ParamName, PolyTraitRef, PredicateOrigin,
13+
TraitBoundModifier, TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass};
1616
use rustc_middle::hir::nested_filter as middle_nested_filter;
@@ -145,7 +145,7 @@ fn check_fn_inner<'tcx>(
145145
.filter(|param| matches!(param.kind, GenericParamKind::Type { .. }));
146146
for typ in types {
147147
for pred in generics.bounds_for_param(cx.tcx.hir().local_def_id(typ.hir_id)) {
148-
if pred.in_where_clause {
148+
if pred.origin == PredicateOrigin::WhereClause {
149149
// has_where_lifetimes checked that this predicate contains no lifetime.
150150
continue;
151151
}

clippy_lints/src/trait_bounds.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use rustc_data_structures::unhash::UnhashMap;
88
use rustc_errors::Applicability;
99
use rustc_hir::def::Res;
1010
use rustc_hir::{
11-
GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, QPath, TraitItem, Ty, TyKind, WherePredicate,
11+
GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath, TraitItem, Ty, TyKind,
12+
WherePredicate,
1213
};
1314
use rustc_lint::{LateContext, LateLintPass};
1415
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -95,6 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
9596
for predicate in item.generics.predicates {
9697
if_chain! {
9798
if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate;
99+
if bound_predicate.origin != PredicateOrigin::ImplTrait;
98100
if !bound_predicate.span.from_expansion();
99101
if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
100102
if let Some(PathSegment {
@@ -168,6 +170,7 @@ impl TraitBounds {
168170
for bound in gen.predicates {
169171
if_chain! {
170172
if let WherePredicate::BoundPredicate(ref p) = bound;
173+
if p.origin != PredicateOrigin::ImplTrait;
171174
if p.bounds.len() as u64 <= self.max_trait_bounds;
172175
if !p.span.from_expansion();
173176
if let Some(ref v) = map.insert(
@@ -223,6 +226,7 @@ fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
223226
for predicate in gen.predicates {
224227
if_chain! {
225228
if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate;
229+
if bound_predicate.origin != PredicateOrigin::ImplTrait;
226230
if !bound_predicate.span.from_expansion();
227231
if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
228232
if let Some(segment) = segments.first();

0 commit comments

Comments
 (0)