about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs2
-rw-r--r--tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.fixed16
-rw-r--r--tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs16
-rw-r--r--tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.stderr14
4 files changed, 48 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())
diff --git a/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.fixed b/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.fixed
new file mode 100644
index 00000000000..570d91d949b
--- /dev/null
+++ b/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.fixed
@@ -0,0 +1,16 @@
+// run-rustfix
+use std::io::stdin;
+
+fn get_name() -> String {
+    let mut your_name = String::new();
+    stdin()
+        .read_line(&mut your_name)
+        .expect("Failed to read the line for some reason");
+    your_name.trim().to_string() //~ ERROR E0308
+}
+
+fn main() {
+    println!("Hello, What is your name? ");
+    let your_name = get_name();
+    println!("Hello, {}", your_name)
+}
diff --git a/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs b/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs
new file mode 100644
index 00000000000..93e8c0af032
--- /dev/null
+++ b/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs
@@ -0,0 +1,16 @@
+// run-rustfix
+use std::io::stdin;
+
+fn get_name() -> String {
+    let mut your_name = String::new();
+    stdin()
+        .read_line(&mut your_name)
+        .expect("Failed to read the line for some reason");
+    your_name.trim() //~ ERROR E0308
+}
+
+fn main() {
+    println!("Hello, What is your name? ");
+    let your_name = get_name();
+    println!("Hello, {}", your_name)
+}
diff --git a/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.stderr b/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.stderr
new file mode 100644
index 00000000000..c721ceb1146
--- /dev/null
+++ b/tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.stderr
@@ -0,0 +1,14 @@
+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`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0308`.