Skip to content

Commit 85da245

Browse files
committed
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
2 parents dfabe4b + af09bf9 commit 85da245

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/librustc_typeck/check/mod.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -4726,7 +4726,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
47264726
if receiver.ends_with(&method_call) {
47274727
None // do not suggest code that is already there (#53348)
47284728
} else {
4729-
Some(format!("{}{}", receiver, method_call))
4729+
/*
4730+
methods defined in `method_call_list` will overwrite
4731+
`.clone()` in copy of `receiver`
4732+
*/
4733+
let method_call_list = [".to_vec()", ".to_string()"];
4734+
if receiver.ends_with(".clone()")
4735+
&& method_call_list.contains(&method_call.as_str()){
4736+
// created copy of `receiver` because we don't want other
4737+
// suggestion to get affected
4738+
let mut new_receiver = receiver.clone();
4739+
let max_len = new_receiver.rfind(".").unwrap();
4740+
new_receiver.truncate(max_len);
4741+
Some(format!("{}{}", new_receiver, method_call))
4742+
}
4743+
else {
4744+
Some(format!("{}{}", receiver, method_call))
4745+
}
47304746
}
47314747
}) .collect::<Vec<_>>();
47324748
if !suggestions.is_empty() {

src/test/ui/issue-53692.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
fn main() {
11+
let items = vec![1, 2, 3];
12+
let ref_items: &[i32] = &items;
13+
let items_clone: Vec<i32> = ref_items.clone();
14+
15+
// in that case no suggestion will be triggered
16+
let items_clone_2:Vec<i32> = items.clone();
17+
18+
let s = "hi";
19+
let string: String = s.clone();
20+
21+
// in that case no suggestion will be triggered
22+
let s2 = "hi";
23+
let string_2: String = s2.to_string();
24+
}

src/test/ui/issue-53692.stderr

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-53692.rs:13:37
3+
|
4+
LL | let items_clone: Vec<i32> = ref_items.clone();
5+
| ^^^^^^^^^^^^^^^^^
6+
| |
7+
| expected struct `std::vec::Vec`, found &[i32]
8+
| help: try using a conversion method: `ref_items.to_vec()`
9+
|
10+
= note: expected type `std::vec::Vec<i32>`
11+
found type `&[i32]`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-53692.rs:19:30
15+
|
16+
LL | let string: String = s.clone();
17+
| ^^^^^^^^^
18+
| |
19+
| expected struct `std::string::String`, found &str
20+
| help: try using a conversion method: `s.to_string()`
21+
|
22+
= note: expected type `std::string::String`
23+
found type `&str`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)