Skip to content

Commit 44d8ecb

Browse files
committed
Only suggest removal of as_* and to_ conversion methods on E0308
Instead of ``` error[E0308]: mismatched types --> tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs:9:5 | 4 | fn get_name() -> String { | ------ expected `String` because of return type ... 9 | your_name.trim() //~ ERROR E0308 | ^^^^^^^^^^^^^^^^ expected `String`, found `&str` | help: try removing the method call | 9 - your_name.trim() 9 + your_name ``` output ``` error[E0308]: mismatched types --> $DIR/only-suggest-removal-of-conversion-method-calls.rs:9:5 | LL | fn get_name() -> String { | ------ expected `String` because of return type ... LL | your_name.trim() | ^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` ``` Fix #114329.
1 parent fb4bca0 commit 44d8ecb

4 files changed

+48
-0
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
261261
expr.kind
262262
&& let Some(recv_ty) = self.typeck_results.borrow().expr_ty_opt(recv_expr)
263263
&& self.can_coerce(recv_ty, expected)
264+
&& let name = method.name.as_str()
265+
&& (name.starts_with("to_") || name.starts_with("as_") || name == "into")
264266
{
265267
let span = if let Some(recv_span) = recv_expr.span.find_ancestor_inside(expr.span) {
266268
expr.span.with_lo(recv_span.hi())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-rustfix
2+
use std::io::stdin;
3+
4+
fn get_name() -> String {
5+
let mut your_name = String::new();
6+
stdin()
7+
.read_line(&mut your_name)
8+
.expect("Failed to read the line for some reason");
9+
your_name.trim().to_string() //~ ERROR E0308
10+
}
11+
12+
fn main() {
13+
println!("Hello, What is your name? ");
14+
let your_name = get_name();
15+
println!("Hello, {}", your_name)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-rustfix
2+
use std::io::stdin;
3+
4+
fn get_name() -> String {
5+
let mut your_name = String::new();
6+
stdin()
7+
.read_line(&mut your_name)
8+
.expect("Failed to read the line for some reason");
9+
your_name.trim() //~ ERROR E0308
10+
}
11+
12+
fn main() {
13+
println!("Hello, What is your name? ");
14+
let your_name = get_name();
15+
println!("Hello, {}", your_name)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/only-suggest-removal-of-conversion-method-calls.rs:9:5
3+
|
4+
LL | fn get_name() -> String {
5+
| ------ expected `String` because of return type
6+
...
7+
LL | your_name.trim()
8+
| ^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
9+
| |
10+
| expected `String`, found `&str`
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)