-
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.
Emit "modifies receiver" diagnostic when no method is found
If no method is found when checking method call, we check if we called a method with signature (&mut T, ...) -> (). If this is the case then we emit a diagnostic message
- Loading branch information
Showing
5 changed files
with
102 additions
and
31 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 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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
fn main() {} | ||
fn main() { | ||
let x: Vec<i32> = vec![1, 2, 3].into_iter().collect::<Vec<i32>>().sort_by_key(|i| i); //~ ERROR mismatched types | ||
vec![1, 2, 3].into_iter().collect::<Vec<i32>>().sort_by_key(|i| i).sort(); //~ ERROR no method named `sort` found for unit type `()` in the current scope | ||
} | ||
|
||
fn foo(mut s: String) -> String { | ||
s.push_str("asdf") //~ ERROR mismatched types | ||
} |
37 changes: 33 additions & 4 deletions
37
tests/ui/suggestions/chain-method-call-mutation-in-place.stderr
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 |
---|---|---|
@@ -1,20 +1,49 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/chain-method-call-mutation-in-place.rs:3:5 | ||
--> $DIR/chain-method-call-mutation-in-place.rs:2:23 | ||
| | ||
LL | let x: Vec<i32> = vec![1, 2, 3].into_iter().collect::<Vec<i32>>().sort_by_key(|i| i); | ||
| -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Vec<i32>`, found `()` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected struct `Vec<i32>` | ||
found unit type `()` | ||
note: method `sort_by_key` modifies its receiver in-place, it is not meant to be used in method chains. | ||
--> $DIR/chain-method-call-mutation-in-place.rs:2:71 | ||
| | ||
LL | let x: Vec<i32> = vec![1, 2, 3].into_iter().collect::<Vec<i32>>().sort_by_key(|i| i); | ||
| ^^^^^^^^^^^ this call modifies its receiver in-place | ||
|
||
error[E0599]: no method named `sort` found for unit type `()` in the current scope | ||
--> $DIR/chain-method-call-mutation-in-place.rs:3:72 | ||
| | ||
LL | vec![1, 2, 3].into_iter().collect::<Vec<i32>>().sort_by_key(|i| i).sort(); | ||
| ^^^^ method not found in `()` | ||
| | ||
note: method `sort_by_key` modifies its receiver in-place, it is not meant to be used in method chains. | ||
--> $DIR/chain-method-call-mutation-in-place.rs:3:53 | ||
| | ||
LL | vec![1, 2, 3].into_iter().collect::<Vec<i32>>().sort_by_key(|i| i).sort(); | ||
| ^^^^^^^^^^^ this call modifies its receiver in-place | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/chain-method-call-mutation-in-place.rs:7:5 | ||
| | ||
LL | fn foo(mut s: String) -> String { | ||
| ------ expected `String` because of return type | ||
LL | s.push_str("asdf") | ||
| ^^^^^^^^^^^^^^^^^^ expected `String`, found `()` | ||
| | ||
note: method `push_str` modifies its receiver in-place | ||
--> $DIR/chain-method-call-mutation-in-place.rs:3:7 | ||
--> $DIR/chain-method-call-mutation-in-place.rs:7:7 | ||
| | ||
LL | s.push_str("asdf") | ||
| - ^^^^^^^^ this call modifies `s` in-place | ||
| | | ||
| you probably want to use this value after calling the method... | ||
= note: ...instead of the `()` output of method `push_str` | ||
|
||
error: aborting due to previous error | ||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. | ||
Some errors have detailed explanations: E0308, E0599. | ||
For more information about an error, try `rustc --explain E0308`. |