-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not suggest unresolvable builder methods
- Loading branch information
Showing
10 changed files
with
141 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
error[E0599]: no function or associated item named `new` found for struct `TcpStream` in the current scope | ||
--> $DIR/suggest-builder-fn.rs:52:29 | ||
| | ||
LL | let _stream = TcpStream::new(); | ||
| ^^^ function or associated item not found in `TcpStream` | ||
| | ||
note: if you're trying to build a new `TcpStream` consider using one of the following associated functions: | ||
TcpStream::connect | ||
TcpStream::connect_timeout | ||
--> $SRC_DIR/std/src/net/tcp.rs:LL:COL | ||
|
||
error[E0599]: the function or associated item `new` exists for struct `Foo<u8>`, but its trait bounds were not satisfied | ||
--> $DIR/suggest-builder-fn.rs:57:27 | ||
| | ||
LL | struct Foo<T> { | ||
| ------------- function or associated item `new` not found for this struct | ||
... | ||
LL | let _foo = Foo::<u8>::new(); | ||
| ^^^ function or associated item cannot be called on `Foo<u8>` due to unsatisfied trait bounds | ||
| | ||
note: trait bound `u8: SomeTrait` was not satisfied | ||
--> $DIR/suggest-builder-fn.rs:12:9 | ||
| | ||
LL | impl<T: SomeTrait + Default> Foo<T> { | ||
| ^^^^^^^^^ ------ | ||
| | | ||
| unsatisfied trait bound introduced here | ||
|
||
error[E0599]: no function or associated item named `new` found for struct `Bar` in the current scope | ||
--> $DIR/suggest-builder-fn.rs:62:21 | ||
| | ||
LL | struct Bar; | ||
| ---------- function or associated item `new` not found for this struct | ||
... | ||
LL | let _bar = Bar::new(); | ||
| ^^^ function or associated item not found in `Bar` | ||
| | ||
note: if you're trying to build a new `Bar` consider using one of the following associated functions: | ||
Bar::build | ||
SomeMod::<impl Bar>::build_public | ||
--> $DIR/suggest-builder-fn.rs:23:5 | ||
| | ||
LL | fn build() -> Self { | ||
| ^^^^^^^^^^^^^^^^^^ | ||
... | ||
LL | pub fn build_public() -> Self { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters