Skip to content

Commit 945ccbd

Browse files
committed
Refactor where predicates, and reserve for attributes support
1 parent 728826f commit 945ccbd

7 files changed

+31
-30
lines changed

clippy_lints/src/extra_unused_type_parameters.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::Applicability;
66
use rustc_hir::intravisit::{Visitor, walk_impl_item, walk_item, walk_param_bound, walk_ty};
77
use rustc_hir::{
88
BodyId, ExprKind, GenericBound, GenericParam, GenericParamKind, Generics, ImplItem, ImplItemKind, Item, ItemKind,
9-
PredicateOrigin, Ty, WherePredicate,
9+
PredicateOrigin, Ty, WherePredicate, WherePredicateKind
1010
};
1111
use rustc_lint::{LateContext, LateLintPass, LintContext};
1212
use rustc_middle::hir::nested_filter;
@@ -205,12 +205,13 @@ impl<'tcx> Visitor<'tcx> for TypeWalker<'_, 'tcx> {
205205
}
206206

207207
fn visit_where_predicate(&mut self, predicate: &'tcx WherePredicate<'tcx>) {
208-
if let WherePredicate::BoundPredicate(predicate) = predicate {
208+
let span = predicate.span;
209+
if let WherePredicateKind::BoundPredicate(predicate) = predicate.kind {
209210
// Collect spans for any bounds on type parameters.
210211
if let Some((def_id, _)) = predicate.bounded_ty.peel_refs().as_generic_param() {
211212
match predicate.origin {
212213
PredicateOrigin::GenericParam => {
213-
self.inline_bounds.insert(def_id, predicate.span);
214+
self.inline_bounds.insert(def_id, span);
214215
},
215216
PredicateOrigin::WhereClause => {
216217
self.where_bounds.insert(def_id);

clippy_lints/src/implied_bounds_in_impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::{Applicability, SuggestionStyle};
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{
66
AssocItemConstraint, GenericArg, GenericBound, GenericBounds, PredicateOrigin, TraitBoundModifiers, TyKind,
7-
WherePredicate,
7+
WherePredicateKind,
88
};
99
use rustc_hir_analysis::lower_ty;
1010
use rustc_lint::{LateContext, LateLintPass};
@@ -324,7 +324,7 @@ fn check<'tcx>(cx: &LateContext<'tcx>, bounds: GenericBounds<'tcx>) {
324324
impl<'tcx> LateLintPass<'tcx> for ImpliedBoundsInImpls {
325325
fn check_generics(&mut self, cx: &LateContext<'tcx>, generics: &rustc_hir::Generics<'tcx>) {
326326
for predicate in generics.predicates {
327-
if let WherePredicate::BoundPredicate(predicate) = predicate
327+
if let WherePredicateKind::BoundPredicate(predicate) = predicate.kind
328328
// In theory, the origin doesn't really matter,
329329
// we *could* also lint on explicit where clauses written out by the user,
330330
// not just impl trait desugared ones, but that contradicts with the lint name...

clippy_lints/src/lifetimes.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::intravisit::{
1212
use rustc_hir::{
1313
BareFnTy, BodyId, FnDecl, FnSig, GenericArg, GenericArgs, GenericBound, GenericParam, GenericParamKind, Generics,
1414
Impl, ImplItem, ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, Node, PolyTraitRef,
15-
PredicateOrigin, TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WherePredicate, lang_items,
15+
PredicateOrigin, TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WherePredicate, WherePredicateKind, lang_items,
1616
};
1717
use rustc_lint::{LateContext, LateLintPass, LintContext};
1818
use rustc_middle::hir::map::Map;
@@ -442,9 +442,9 @@ impl<'tcx> Visitor<'tcx> for RefVisitor<'_, 'tcx> {
442442
/// reason about elision.
443443
fn has_where_lifetimes<'tcx>(cx: &LateContext<'tcx>, generics: &'tcx Generics<'_>) -> bool {
444444
for predicate in generics.predicates {
445-
match *predicate {
446-
WherePredicate::RegionPredicate(..) => return true,
447-
WherePredicate::BoundPredicate(ref pred) => {
445+
match *predicate.kind {
446+
WherePredicateKind::RegionPredicate(..) => return true,
447+
WherePredicateKind::BoundPredicate(ref pred) => {
448448
// a predicate like F: Trait or F: for<'a> Trait<'a>
449449
let mut visitor = RefVisitor::new(cx);
450450
// walk the type F, it may not contain LT refs
@@ -467,7 +467,7 @@ fn has_where_lifetimes<'tcx>(cx: &LateContext<'tcx>, generics: &'tcx Generics<'_
467467
}
468468
}
469469
},
470-
WherePredicate::EqPredicate(ref pred) => {
470+
WherePredicateKind::EqPredicate(ref pred) => {
471471
let mut visitor = RefVisitor::new(cx);
472472
walk_ty(&mut visitor, pred.lhs_ty);
473473
walk_ty(&mut visitor, pred.rhs_ty);

clippy_lints/src/multiple_bound_locations.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::visit::FnKind;
2-
use rustc_ast::{NodeId, WherePredicate};
2+
use rustc_ast::{NodeId, WherePredicateKind};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_lint::{EarlyContext, EarlyLintPass};
55
use rustc_session::declare_lint_pass;
@@ -51,8 +51,8 @@ impl EarlyLintPass for MultipleBoundLocations {
5151
}
5252
}
5353
for clause in &generics.where_clause.predicates {
54-
match clause {
55-
WherePredicate::BoundPredicate(pred) => {
54+
match &clause.kind {
55+
WherePredicateKind::BoundPredicate(pred) => {
5656
if (!pred.bound_generic_params.is_empty() || !pred.bounds.is_empty())
5757
&& let Some(Some(bound_span)) = pred
5858
.bounded_ty
@@ -62,14 +62,14 @@ impl EarlyLintPass for MultipleBoundLocations {
6262
emit_lint(cx, *bound_span, pred.bounded_ty.span);
6363
}
6464
},
65-
WherePredicate::RegionPredicate(pred) => {
65+
WherePredicateKind::RegionPredicate(pred) => {
6666
if !pred.bounds.is_empty()
6767
&& let Some(bound_span) = generic_params_with_bounds.get(&pred.lifetime.ident.name.as_str())
6868
{
6969
emit_lint(cx, *bound_span, pred.lifetime.ident.span);
7070
}
7171
},
72-
WherePredicate::EqPredicate(_) => {},
72+
WherePredicateKind::EqPredicate(_) => {},
7373
}
7474
}
7575
}

clippy_lints/src/needless_maybe_sized.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_errors::Applicability;
33
use rustc_hir::def_id::{DefId, DefIdMap};
4-
use rustc_hir::{BoundPolarity, GenericBound, Generics, PolyTraitRef, TraitBoundModifiers, WherePredicate};
4+
use rustc_hir::{BoundPolarity, GenericBound, Generics, PolyTraitRef, TraitBoundModifiers, WherePredicateKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::ty::{ClauseKind, PredicatePolarity};
77
use rustc_session::declare_lint_pass;
@@ -52,7 +52,7 @@ fn type_param_bounds<'tcx>(generics: &'tcx Generics<'tcx>) -> impl Iterator<Item
5252
.iter()
5353
.enumerate()
5454
.filter_map(|(predicate_pos, predicate)| {
55-
let WherePredicate::BoundPredicate(bound_predicate) = predicate else {
55+
let WherePredicateKind::BoundPredicate(bound_predicate) = &predicate.kind else {
5656
return None;
5757
};
5858

clippy_lints/src/trait_bounds.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_errors::Applicability;
1111
use rustc_hir::def::Res;
1212
use rustc_hir::{
1313
BoundPolarity, GenericBound, Generics, Item, ItemKind, LangItem, Node, Path, PathSegment, PredicateOrigin, QPath,
14-
TraitBoundModifiers, TraitItem, TraitRef, Ty, TyKind, WherePredicate,
14+
TraitBoundModifiers, TraitItem, TraitRef, Ty, TyKind, WherePredicateKind,
1515
};
1616
use rustc_lint::{LateContext, LateLintPass};
1717
use rustc_session::impl_lint_pass;
@@ -124,9 +124,9 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
124124
let mut self_bounds_map = FxHashMap::default();
125125

126126
for predicate in item.generics.predicates {
127-
if let WherePredicate::BoundPredicate(bound_predicate) = predicate
127+
if let WherePredicateKind::BoundPredicate(bound_predicate) = predicate.kind
128128
&& bound_predicate.origin != PredicateOrigin::ImplTrait
129-
&& !bound_predicate.span.from_expansion()
129+
&& !predicate.span.from_expansion()
130130
&& let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind
131131
&& let Some(PathSegment {
132132
res: Res::SelfTyParam { trait_: def_id },
@@ -268,10 +268,10 @@ impl TraitBounds {
268268
let mut map: UnhashMap<SpanlessTy<'_, '_>, Vec<&GenericBound<'_>>> = UnhashMap::default();
269269
let mut applicability = Applicability::MaybeIncorrect;
270270
for bound in generics.predicates {
271-
if let WherePredicate::BoundPredicate(p) = bound
271+
if let WherePredicateKind::BoundPredicate(p) = bound.kind
272272
&& p.origin != PredicateOrigin::ImplTrait
273273
&& p.bounds.len() as u64 <= self.max_trait_bounds
274-
&& !p.span.from_expansion()
274+
&& !bound.span.from_expansion()
275275
&& let bounds = p
276276
.bounds
277277
.iter()
@@ -295,7 +295,7 @@ impl TraitBounds {
295295
span_lint_and_help(
296296
cx,
297297
TYPE_REPETITION_IN_BOUNDS,
298-
p.span,
298+
bound.span,
299299
"this type has already been used as a bound predicate",
300300
None,
301301
hint_string,
@@ -322,8 +322,8 @@ fn check_trait_bound_duplication<'tcx>(cx: &LateContext<'tcx>, generics: &'_ Gen
322322
.predicates
323323
.iter()
324324
.filter_map(|pred| {
325-
if pred.in_where_clause()
326-
&& let WherePredicate::BoundPredicate(bound_predicate) = pred
325+
if pred.kind.in_where_clause()
326+
&& let WherePredicateKind::BoundPredicate(bound_predicate) = pred.kind
327327
&& let TyKind::Path(QPath::Resolved(_, path)) = bound_predicate.bounded_ty.kind
328328
{
329329
return Some(
@@ -347,10 +347,10 @@ fn check_trait_bound_duplication<'tcx>(cx: &LateContext<'tcx>, generics: &'_ Gen
347347
// |
348348
// compare trait bounds keyed by generic name and comparable trait to collected where
349349
// predicates eg. (T, Clone)
350-
for predicate in generics.predicates.iter().filter(|pred| !pred.in_where_clause()) {
351-
if let WherePredicate::BoundPredicate(bound_predicate) = predicate
350+
for predicate in generics.predicates.iter().filter(|pred| !pred.kind.in_where_clause()) {
351+
if let WherePredicateKind::BoundPredicate(bound_predicate) = predicate.kind
352352
&& bound_predicate.origin != PredicateOrigin::ImplTrait
353-
&& !bound_predicate.span.from_expansion()
353+
&& !predicate.span.from_expansion()
354354
&& let TyKind::Path(QPath::Resolved(_, path)) = bound_predicate.bounded_ty.kind
355355
{
356356
let traits = rollup_traits(cx, bound_predicate.bounds, "these bounds contain repeated elements");

clippy_utils/src/ast_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,8 @@ pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
661661
}
662662

663663
pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
664-
use WherePredicate::*;
665-
match (l, r) {
664+
use WherePredicateKind::*;
665+
match (&l.kind, &r.kind) {
666666
(BoundPredicate(l), BoundPredicate(r)) => {
667667
over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
668668
eq_generic_param(l, r)

0 commit comments

Comments
 (0)