Skip to content

Commit 903fe3b

Browse files
committed
Auto merge of #10894 - Centri3:type_repetition_in_bounds, r=blyxyas,xFrednet
[`type_repetition_in_bounds`]: Don't lint on derived code fixes #10504. changelog: [`type_repetition_in_bounds`]: Don't lint on derived code
2 parents 841f219 + 4191de3 commit 903fe3b

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

clippy_lints/src/trait_bounds.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::source::{snippet, snippet_opt, snippet_with_applicability};
3-
use clippy_utils::{SpanlessEq, SpanlessHash};
3+
use clippy_utils::{is_from_proc_macro, SpanlessEq, SpanlessHash};
44
use core::hash::{Hash, Hasher};
55
use if_chain::if_chain;
66
use itertools::Itertools;
@@ -260,7 +260,7 @@ impl TraitBounds {
260260
SpanlessTy { ty: p.bounded_ty, cx },
261261
p.bounds.iter().collect::<Vec<_>>()
262262
);
263-
263+
if !is_from_proc_macro(cx, p.bounded_ty);
264264
then {
265265
let trait_bounds = v
266266
.iter()
@@ -342,7 +342,7 @@ fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
342342
"this trait bound is already specified in the where clause",
343343
None,
344344
"consider removing this trait bound",
345-
);
345+
);
346346
}
347347
}
348348
}

clippy_utils/src/check_proc_macro.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_ast::{
1919
};
2020
use rustc_hir::{
2121
intravisit::FnKind, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, HirId,
22-
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, Node, QPath, TraitItem,
23-
TraitItemKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
22+
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, MutTy, Node, QPath, TraitItem,
23+
TraitItemKind, Ty, TyKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
2424
};
2525
use rustc_lint::{LateContext, LintContext};
2626
use rustc_middle::ty::TyCtxt;
@@ -319,6 +319,31 @@ fn attr_search_pat(attr: &Attribute) -> (Pat, Pat) {
319319
}
320320
}
321321

322+
fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
323+
match ty.kind {
324+
TyKind::Slice(..) | TyKind::Array(..) => (Pat::Str("["), Pat::Str("]")),
325+
TyKind::Ptr(MutTy { mutbl, ty }) => (
326+
if mutbl.is_mut() {
327+
Pat::Str("*const")
328+
} else {
329+
Pat::Str("*mut")
330+
},
331+
ty_search_pat(ty).1,
332+
),
333+
TyKind::Ref(_, MutTy { ty, .. }) => (Pat::Str("&"), ty_search_pat(ty).1),
334+
TyKind::BareFn(bare_fn) => (
335+
Pat::OwnedStr(format!("{}{} fn", bare_fn.unsafety.prefix_str(), bare_fn.abi.name())),
336+
ty_search_pat(ty).1,
337+
),
338+
TyKind::Never => (Pat::Str("!"), Pat::Str("")),
339+
TyKind::Tup(..) => (Pat::Str("("), Pat::Str(")")),
340+
TyKind::OpaqueDef(..) => (Pat::Str("impl"), Pat::Str("")),
341+
TyKind::Path(qpath) => qpath_search_pat(&qpath),
342+
// NOTE: This is missing `TraitObject`. It always return true then.
343+
_ => (Pat::Str(""), Pat::Str("")),
344+
}
345+
}
346+
322347
pub trait WithSearchPat {
323348
type Context: LintContext;
324349
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
@@ -345,6 +370,7 @@ impl_with_search_pat!(LateContext: TraitItem with trait_item_search_pat);
345370
impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat);
346371
impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat);
347372
impl_with_search_pat!(LateContext: Variant with variant_search_pat);
373+
impl_with_search_pat!(LateContext: Ty with ty_search_pat);
348374

349375
impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
350376
type Context = LateContext<'cx>;

tests/ui/type_repetition_in_bounds.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![deny(clippy::type_repetition_in_bounds)]
22
#![allow(clippy::extra_unused_type_parameters)]
33

4+
use serde::Deserialize;
45
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
56

67
pub fn foo<T>(_t: T)
@@ -70,6 +71,20 @@ mod issue4326 {
7071
}
7172
}
7273

74+
// Extern macros shouldn't lint, again (see #10504)
75+
mod issue10504 {
76+
use serde::{Deserialize, Serialize};
77+
use std::fmt::Debug;
78+
use std::hash::Hash;
79+
80+
#[derive(Debug, Serialize, Deserialize)]
81+
#[serde(bound(
82+
serialize = "T: Serialize + Hash + Eq",
83+
deserialize = "Box<T>: serde::de::DeserializeOwned + Hash + Eq"
84+
))]
85+
struct OpaqueParams<T: ?Sized + Debug>(std::marker::PhantomData<T>);
86+
}
87+
7388
// Issue #7360
7489
struct Foo<T, U>
7590
where

tests/ui/type_repetition_in_bounds.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this type has already been used as a bound predicate
2-
--> $DIR/type_repetition_in_bounds.rs:9:5
2+
--> $DIR/type_repetition_in_bounds.rs:10:5
33
|
44
LL | T: Clone,
55
| ^^^^^^^^
@@ -12,23 +12,23 @@ LL | #![deny(clippy::type_repetition_in_bounds)]
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error: this type has already been used as a bound predicate
15-
--> $DIR/type_repetition_in_bounds.rs:26:5
15+
--> $DIR/type_repetition_in_bounds.rs:27:5
1616
|
1717
LL | Self: Copy + Default + Ord,
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
|
2020
= help: consider combining the bounds: `Self: Clone + Copy + Default + Ord`
2121

2222
error: this type has already been used as a bound predicate
23-
--> $DIR/type_repetition_in_bounds.rs:86:5
23+
--> $DIR/type_repetition_in_bounds.rs:101:5
2424
|
2525
LL | T: Clone,
2626
| ^^^^^^^^
2727
|
2828
= help: consider combining the bounds: `T: ?Sized + Clone`
2929

3030
error: this type has already been used as a bound predicate
31-
--> $DIR/type_repetition_in_bounds.rs:91:5
31+
--> $DIR/type_repetition_in_bounds.rs:106:5
3232
|
3333
LL | T: ?Sized,
3434
| ^^^^^^^^^

0 commit comments

Comments
 (0)