summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/type_repetition_in_bounds.rs
blob: 504a003940502286645eded51cae16f7eca281f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#![deny(clippy::type_repetition_in_bounds)]
#![allow(clippy::extra_unused_type_parameters)]

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

pub fn foo<T>(_t: T)
where
    T: Copy,
    T: Clone,
    //~^ ERROR: this type has already been used as a bound predicate
{
    unimplemented!();
}

pub fn bar<T, U>(_t: T, _u: U)
where
    T: Copy,
    U: Clone,
{
    unimplemented!();
}

// Threshold test (see #4380)
trait LintBounds
where
    Self: Clone,
    Self: Copy + Default + Ord,
    //~^ ERROR: this type has already been used as a bound predicate
    Self: Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign,
    Self: Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign,
{
}

trait LotsOfBounds
where
    Self: Clone + Copy + Default + Ord,
    Self: Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign,
    Self: Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign,
{
}

// Generic distinction (see #4323)
mod issue4323 {
    pub struct Foo<A>(A);
    pub struct Bar<A, B> {
        a: Foo<A>,
        b: Foo<B>,
    }

    impl<A, B> Unpin for Bar<A, B>
    where
        Foo<A>: Unpin,
        Foo<B>: Unpin,
    {
    }
}

// Extern macros shouldn't lint (see #4326)
extern crate serde;
mod issue4326 {
    use serde::{Deserialize, Serialize};

    trait Foo {}
    impl Foo for String {}

    #[derive(Debug, Serialize, Deserialize)]
    struct Bar<S>
    where
        S: Foo,
    {
        foo: S,
    }
}

// Extern macros shouldn't lint, again (see #10504)
mod issue10504 {
    use serde::{Deserialize, Serialize};
    use std::fmt::Debug;
    use std::hash::Hash;

    #[derive(Debug, Serialize, Deserialize)]
    #[serde(bound(
        serialize = "T: Serialize + Hash + Eq",
        deserialize = "Box<T>: serde::de::DeserializeOwned + Hash + Eq"
    ))]
    struct OpaqueParams<T: ?Sized + Debug>(std::marker::PhantomData<T>);
}

// Issue #7360
struct Foo<T, U>
where
    T: Clone,
    U: Clone,
{
    t: T,
    u: U,
}

// Check for the `?` in `?Sized`
pub fn f<T: ?Sized>()
where
    T: Clone,
    //~^ ERROR: this type has already been used as a bound predicate
{
}
pub fn g<T: Clone>()
where
    T: ?Sized,
    //~^ ERROR: this type has already been used as a bound predicate
{
}

// This should not lint
fn impl_trait(_: impl AsRef<str>, _: impl AsRef<str>) {}

#[clippy::msrv = "1.14.0"]
mod issue8772_fail {
    pub trait Trait<X, Y, Z> {}

    pub fn f<T: ?Sized, U>(arg: usize)
    where
        T: Trait<Option<usize>, Box<[String]>, bool> + 'static,
        U: Clone + Sync + 'static,
    {
    }
}

#[clippy::msrv = "1.15.0"]
mod issue8772_pass {
    pub trait Trait<X, Y, Z> {}

    pub fn f<T: ?Sized, U>(arg: usize)
    where
        T: Trait<Option<usize>, Box<[String]>, bool> + 'static,
        //~^ ERROR: this type has already been used as a bound predicate
        U: Clone + Sync + 'static,
    {
    }
}

fn main() {}