about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/from_over_into.fixed
blob: 7229e5a2d3589b69ad89bba1cdfc5fbcea50c981 (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
#![feature(type_alias_impl_trait)]
#![warn(clippy::from_over_into)]
#![allow(non_local_definitions)]
#![allow(unused)]

// this should throw an error
struct StringWrapper(String);

impl From<String> for StringWrapper {
    //~^ from_over_into
    fn from(val: String) -> Self {
        StringWrapper(val)
    }
}

struct SelfType(String);

impl From<String> for SelfType {
    //~^ from_over_into
    fn from(val: String) -> Self {
        SelfType(String::new())
    }
}

#[derive(Default)]
struct X;

impl X {
    const FOO: &'static str = "a";
}

struct SelfKeywords;

impl From<X> for SelfKeywords {
    //~^ from_over_into
    fn from(val: X) -> Self {
        let _ = X;
        let _ = X::FOO;
        let _: X = val;

        SelfKeywords
    }
}

struct ExplicitPaths(bool);

impl core::convert::From<crate::ExplicitPaths> for bool {
    //~^ from_over_into
    fn from(mut val: crate::ExplicitPaths) -> Self {
        let in_closure = || val.0;

        val.0 = false;
        val.0
    }
}

// this is fine
struct A(String);

impl From<String> for A {
    fn from(s: String) -> A {
        A(s)
    }
}

struct PathInExpansion;

impl From<PathInExpansion> for String {
    //~^ from_over_into
    fn from(val: PathInExpansion) -> Self {
        // non self/Self paths in expansions are fine
        panic!()
    }
}

#[clippy::msrv = "1.40"]
fn msrv_1_40() {
    struct FromOverInto<T>(Vec<T>);

    impl<T> Into<FromOverInto<T>> for Vec<T> {
        fn into(self) -> FromOverInto<T> {
            FromOverInto(self)
        }
    }
}

#[clippy::msrv = "1.41"]
fn msrv_1_41() {
    struct FromOverInto<T>(Vec<T>);

    impl<T> From<Vec<T>> for FromOverInto<T> {
        //~^ from_over_into
        fn from(val: Vec<T>) -> Self {
            FromOverInto(val)
        }
    }
}

fn issue_12138() {
    struct Hello;

    impl From<Hello> for () {
        //~^ from_over_into
        fn from(val: Hello) {}
    }
}

fn issue_112502() {
    struct MyInt(i64);

    impl From<MyInt> for i64 {
        //~^ from_over_into
        fn from(val: MyInt) -> Self {
            val.0
        }
    }
}

fn main() {}