about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-09-14 13:06:23 +0000
committerbors <bors@rust-lang.org>2018-09-14 13:06:23 +0000
commit85da24527adc45adc6b503d92fca2a29331ddab5 (patch)
tree7e154ba47dc2359a085353852d840e582eb0c7a6
parentdfabe4b88509bfff1eca9060ccc9a4d28ee394e8 (diff)
parentaf09bf9293ed89b93662bad02006bcabde587a16 (diff)
downloadrust-85da24527adc45adc6b503d92fca2a29331ddab5.tar.gz
rust-85da24527adc45adc6b503d92fca2a29331ddab5.zip
Auto merge of #54080 - PramodBisht:issue/53692, r=estebank
Addressed #53692

@sunjay @estebank  @csmoe hopefully this answer #53692
Please let me know if you have any suggestion
-rw-r--r--src/librustc_typeck/check/mod.rs18
-rw-r--r--src/test/ui/issue-53692.rs24
-rw-r--r--src/test/ui/issue-53692.stderr27
3 files changed, 68 insertions, 1 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index 0305489b707..aa27fe528e1 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -4726,7 +4726,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                         if receiver.ends_with(&method_call) {
                             None  // do not suggest code that is already there (#53348)
                         } else {
-                            Some(format!("{}{}", receiver, method_call))
+                            /*
+                            methods defined in `method_call_list` will overwrite
+                            `.clone()` in copy of `receiver`
+                            */
+                            let method_call_list = [".to_vec()", ".to_string()"];
+                            if receiver.ends_with(".clone()")
+                                    && method_call_list.contains(&method_call.as_str()){
+                                // created copy of `receiver` because we don't want other
+                                // suggestion to get affected
+                                let mut new_receiver = receiver.clone();
+                                let max_len = new_receiver.rfind(".").unwrap();
+                                new_receiver.truncate(max_len);
+                                Some(format!("{}{}", new_receiver, method_call))
+                            }
+                            else {
+                                Some(format!("{}{}", receiver, method_call))
+                            }
                         }
                     }) .collect::<Vec<_>>();
                 if !suggestions.is_empty() {
diff --git a/src/test/ui/issue-53692.rs b/src/test/ui/issue-53692.rs
new file mode 100644
index 00000000000..0b6cc36fa52
--- /dev/null
+++ b/src/test/ui/issue-53692.rs
@@ -0,0 +1,24 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+fn main() {
+        let items = vec![1, 2, 3];
+        let ref_items: &[i32] = &items;
+        let items_clone: Vec<i32> = ref_items.clone();
+
+        // in that case no suggestion will be triggered
+        let items_clone_2:Vec<i32> = items.clone();
+
+        let s = "hi";
+        let string: String = s.clone();
+
+        // in that case no suggestion will be triggered
+        let s2 = "hi";
+        let string_2: String = s2.to_string();
+}
diff --git a/src/test/ui/issue-53692.stderr b/src/test/ui/issue-53692.stderr
new file mode 100644
index 00000000000..9cd8a536155
--- /dev/null
+++ b/src/test/ui/issue-53692.stderr
@@ -0,0 +1,27 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-53692.rs:13:37
+   |
+LL |         let items_clone: Vec<i32> = ref_items.clone();
+   |                                     ^^^^^^^^^^^^^^^^^
+   |                                     |
+   |                                     expected struct `std::vec::Vec`, found &[i32]
+   |                                     help: try using a conversion method: `ref_items.to_vec()`
+   |
+   = note: expected type `std::vec::Vec<i32>`
+              found type `&[i32]`
+
+error[E0308]: mismatched types
+  --> $DIR/issue-53692.rs:19:30
+   |
+LL |         let string: String = s.clone();
+   |                              ^^^^^^^^^
+   |                              |
+   |                              expected struct `std::string::String`, found &str
+   |                              help: try using a conversion method: `s.to_string()`
+   |
+   = note: expected type `std::string::String`
+              found type `&str`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0308`.