about summary refs log tree commit diff
path: root/tests/ui/resolve/suggestions/suggest-builder-fn.rs
blob: 959675ef2c998cfa37d64f91d11fad3e13eef0de (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
// Tests that we suggest the right alternatives when
// a builder method cannot be resolved

use std::net::TcpStream;

trait SomeTrait {}

struct Foo<T> {
    v : T
}

impl<T: SomeTrait + Default> Foo<T> {
    // Should not be suggested if constraint on the impl not met
    fn new() -> Self {
        Self { v: T::default() }
    }
}

struct Bar;

impl Bar {
    // Should be suggested
    fn build() -> Self {
        Self {}
    }

    // Method with self can't be a builder.
    // Should not be suggested
    fn build_with_self(&self) -> Self {
        Self {}
    }
}

mod SomeMod {
    use crate::Bar;

    impl Bar {
        // Public method. Should be suggested
        pub fn build_public() -> Self {
            Self {}
        }

        // Private method. Should not be suggested
        fn build_private() -> Self {
            Self {}
        }
    }
}

fn main() {
   // `new` not found on `TcpStream` and `connect` should be suggested
   let _stream = TcpStream::new();
   //~^ ERROR no function or associated item named `new` found

    // Although `new` is found on `<impl Foo<T>>` it should not be
    // suggested because `u8` does not meet the `T: SomeTrait` constraint
    let _foo = Foo::<u8>::new();
   //~^ ERROR the function or associated item `new` exists for struct `Foo<u8>`, but its trait bounds were not satisfied

   // Should suggest only `<impl Bar>::build()` and `SomeMod::<impl Bar>::build_public()`.
   // Other methods should not suggested because they are private or are not a builder
    let _bar = Bar::new();
   //~^ ERROR no function or associated item named `new` found
}