diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-02-04 19:42:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-04 19:42:10 +0100 |
| commit | b23945c9b2f51bd4d4aecc8a4e3bfeffc7c2a896 (patch) | |
| tree | 42ce6f66030b015b9e67397cf991d226875ac18e /compiler | |
| parent | 6af04583a6ede7b70b36026ecd6ba1ad9c481724 (diff) | |
| parent | 44d8ecbfb85d1649a4267e33c847f79a125b99f9 (diff) | |
| download | rust-b23945c9b2f51bd4d4aecc8a4e3bfeffc7c2a896.tar.gz rust-b23945c9b2f51bd4d4aecc8a4e3bfeffc7c2a896.zip | |
Rollup merge of #120473 - estebank:issue-114329, r=TaKO8Ki
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()
| ^^^^^^^^^^^^^^^^ 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.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 95c1139e43e..5395ffda1d1 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -261,6 +261,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expr.kind && let Some(recv_ty) = self.typeck_results.borrow().expr_ty_opt(recv_expr) && self.can_coerce(recv_ty, expected) + && let name = method.name.as_str() + && (name.starts_with("to_") || name.starts_with("as_") || name == "into") { let span = if let Some(recv_span) = recv_expr.span.find_ancestor_inside(expr.span) { expr.span.with_lo(recv_span.hi()) |
