Skip to content

Commit 1d018ce

Browse files
committed
Auto merge of rust-lang#96770 - flip1995:fix-trait-type-in-bounds, r=cjgillot
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 AST->HIR lowering. r? `@cjgillot` cc `@estebank` (as the reviewer of rust-lang#93803)
2 parents 6d03789 + bca3d8a commit 1d018ce

6 files changed

+14
-26
lines changed

clippy_lints/src/lib.register_nursery.rs

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
2929
LintId::of(strings::STRING_LIT_AS_BYTES),
3030
LintId::of(suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS),
3131
LintId::of(trailing_empty_array::TRAILING_EMPTY_ARRAY),
32-
LintId::of(trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS),
33-
LintId::of(trait_bounds::TYPE_REPETITION_IN_BOUNDS),
3432
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
3533
LintId::of(transmute::USELESS_TRANSMUTE),
3634
LintId::of(use_self::USE_SELF),

clippy_lints/src/lib.register_pedantic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
8484
LintId::of(semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED),
8585
LintId::of(stable_sort_primitive::STABLE_SORT_PRIMITIVE),
8686
LintId::of(strings::STRING_ADD_ASSIGN),
87+
LintId::of(trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS),
88+
LintId::of(trait_bounds::TYPE_REPETITION_IN_BOUNDS),
8789
LintId::of(transmute::TRANSMUTE_PTR_TO_PTR),
8890
LintId::of(types::LINKEDLIST),
8991
LintId::of(types::OPTION_OPTION),

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

+7-3
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};
@@ -35,7 +36,7 @@ declare_clippy_lint! {
3536
/// ```
3637
#[clippy::version = "1.38.0"]
3738
pub TYPE_REPETITION_IN_BOUNDS,
38-
nursery,
39+
pedantic,
3940
"Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
4041
}
4142

@@ -65,7 +66,7 @@ declare_clippy_lint! {
6566
/// ```
6667
#[clippy::version = "1.47.0"]
6768
pub TRAIT_DUPLICATION_IN_BOUNDS,
68-
nursery,
69+
pedantic,
6970
"Check if the same trait bounds are specified twice during a function declaration"
7071
}
7172

@@ -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();

tests/ui/trait_duplication_in_bounds.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,5 @@ LL | Self: Iterator<Item = Foo>,
6767
|
6868
= help: consider removing this trait bound
6969

70-
error: this trait bound is already specified in the where clause
71-
--> $DIR/trait_duplication_in_bounds.rs:99:23
72-
|
73-
LL | fn impl_trait(_: impl AsRef<str>, _: impl AsRef<str>) {}
74-
| ^^^^^^^^^^
75-
|
76-
= help: consider removing this trait bound
77-
78-
error: aborting due to 9 previous errors
70+
error: aborting due to 8 previous errors
7971

tests/ui/type_repetition_in_bounds.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,5 @@ LL | Self: Copy + Default + Ord,
1919
|
2020
= help: consider combining the bounds: `Self: Clone + Copy + Default + Ord`
2121

22-
error: this type has already been used as a bound predicate
23-
--> $DIR/type_repetition_in_bounds.rs:83:43
24-
|
25-
LL | fn impl_trait(_: impl AsRef<str>, _: impl AsRef<str>) {}
26-
| ^^^^^^^^^^
27-
|
28-
= help: consider combining the bounds: `impl AsRef<str>: AsRef<str> + AsRef<str>`
29-
30-
error: aborting due to 3 previous errors
22+
error: aborting due to 2 previous errors
3123

0 commit comments

Comments
 (0)