about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPramod Bisht <pramodpsb@outlook.com>2018-09-09 12:05:46 +0000
committerPramod Bisht <pramodpsb@outlook.com>2018-09-09 12:27:25 +0000
commita673203ec83746bc0bc7e4579fa97519a66f570e (patch)
tree33c946881d3b3491d461a0a6d794b21d7dcf548a /src
parent05cb29e96f718a94d31afd094b1efd7c05be4269 (diff)
downloadrust-a673203ec83746bc0bc7e4579fa97519a66f570e.tar.gz
rust-a673203ec83746bc0bc7e4579fa97519a66f570e.zip
Addressed #53692
Diffstat (limited to 'src')
-rw-r--r--src/librustc_typeck/check/mod.rs16
-rw-r--r--src/test/ui/issue-53692.rs17
-rw-r--r--src/test/ui/issue-53692.stderr27
3 files changed, 59 insertions, 1 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index a1e4c85f821..8f3300fac18 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -4726,7 +4726,21 @@ 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 will be overwrite `.clone()`
+                            // if suggesion is present in `method_call_list` (#53692)
+                            let method_call_list = [".to_vec()", ".to_string()"];
+                            if receiver.ends_with("clone()")
+                                    && method_call_list.contains(&method_call.as_str()){
+                                // needed to clone receiver, because we don't want other
+                                // suggestion to get affect because of that
+                                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..314362cf112
--- /dev/null
+++ b/src/test/ui/issue-53692.rs
@@ -0,0 +1,17 @@
+// 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();
+
+        let s = "hi";
+        let string: String = s.clone();
+}
diff --git a/src/test/ui/issue-53692.stderr b/src/test/ui/issue-53692.stderr
new file mode 100644
index 00000000000..1dc7621f1e4
--- /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:16: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`.