Skip to content

Commit 5a0a38a

Browse files
committed
Do not suggest conversion method that is already there
1 parent 0f4b498 commit 5a0a38a

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Diff for: src/librustc_typeck/check/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -4629,8 +4629,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
46294629
let methods = self.get_conversion_methods(expr.span, expected, found);
46304630
if let Ok(expr_text) = self.sess().codemap().span_to_snippet(expr.span) {
46314631
let suggestions = iter::repeat(expr_text).zip(methods.iter())
4632-
.map(|(receiver, method)| format!("{}.{}()", receiver, method.ident))
4633-
.collect::<Vec<_>>();
4632+
.filter_map(|(receiver, method)| {
4633+
let method_call = format!(".{}()", method.ident);
4634+
if receiver.ends_with(&method_call) {
4635+
None // do not suggest code that is already there (#53348)
4636+
} else {
4637+
Some(format!("{}{}", receiver, method_call))
4638+
}
4639+
}) .collect::<Vec<_>>();
46344640
if !suggestions.is_empty() {
46354641
err.span_suggestions(expr.span, "try using a conversion method", suggestions);
46364642
}

Diff for: src/test/ui/issues/issue-53348.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
11+
fn main() {
12+
let mut v = vec!["hello", "this", "is", "a", "test"];
13+
14+
let v2 = Vec::new();
15+
16+
v.into_iter().map(|s|s.to_owned()).collect::<Vec<_>>();
17+
18+
let mut a = String::new();
19+
for i in v {
20+
a = *i.to_string();
21+
//~^ ERROR mismatched types
22+
//~| NOTE expected struct `std::string::String`, found str
23+
//~| NOTE expected type
24+
v2.push(a);
25+
}
26+
}

Diff for: src/test/ui/issues/issue-53348.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-53348.rs:20:13
3+
|
4+
LL | a = *i.to_string();
5+
| ^^^^^^^^^^^^^^ expected struct `std::string::String`, found str
6+
|
7+
= note: expected type `std::string::String`
8+
found type `str`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)