diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index ae40cb29ed6..aa6817002b8 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -1427,6 +1427,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let tcx = self.tcx; let def_kind = similar_candidate.kind.as_def_kind(); let an = self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id); + let msg = format!( + "there is {an} {} `{}` with a similar name", + self.tcx.def_kind_descr(def_kind, similar_candidate.def_id), + similar_candidate.name, + ); // Methods are defined within the context of a struct and their first parameter // is always `self`, which represents the instance of the struct the method is // being called on Associated functions don’t take self as a parameter and they are @@ -1443,7 +1448,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // call expression the user wrote. err.span_suggestion_verbose( span, - format!("there is {an} method with a similar name"), + msg, similar_candidate.name, Applicability::MaybeIncorrect, ); @@ -1453,8 +1458,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err.span_help( tcx.def_span(similar_candidate.def_id), format!( - "there is {an} method `{}` with a similar name{}", - similar_candidate.name, + "{msg}{}", if let None = args { "" } else { ", but with different arguments" }, ), ); @@ -1466,22 +1470,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // function we found. err.span_suggestion_verbose( span, - format!( - "there is {an} {} with a similar name", - self.tcx.def_kind_descr(def_kind, similar_candidate.def_id) - ), + msg, similar_candidate.name, Applicability::MaybeIncorrect, ); } else { - err.span_help( - tcx.def_span(similar_candidate.def_id), - format!( - "there is {an} {} `{}` with a similar name", - self.tcx.def_kind_descr(def_kind, similar_candidate.def_id), - similar_candidate.name, - ), - ); + err.span_help(tcx.def_span(similar_candidate.def_id), msg); } } else if let Mode::Path = mode && args.unwrap_or(&[]).is_empty() @@ -1489,24 +1483,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // We have an associated item syntax and we found something that isn't an fn. err.span_suggestion_verbose( span, - format!( - "there is {an} {} with a similar name", - self.tcx.def_kind_descr(def_kind, similar_candidate.def_id) - ), + msg, similar_candidate.name, Applicability::MaybeIncorrect, ); } else { // The expression is a function or method call, but the item we found is an // associated const or type. - err.span_help( - tcx.def_span(similar_candidate.def_id), - format!( - "there is {an} {} `{}` with a similar name", - self.tcx.def_kind_descr(def_kind, similar_candidate.def_id), - similar_candidate.name, - ), - ); + err.span_help(tcx.def_span(similar_candidate.def_id), msg); } } diff --git a/tests/ui/associated-item/associated-item-enum.stderr b/tests/ui/associated-item/associated-item-enum.stderr index a966468e3dd..3b00588f1d1 100644 --- a/tests/ui/associated-item/associated-item-enum.stderr +++ b/tests/ui/associated-item/associated-item-enum.stderr @@ -7,7 +7,7 @@ LL | enum Enum { Variant } LL | Enum::mispellable(); | ^^^^^^^^^^^ variant or associated item not found in `Enum` | -help: there is an associated function with a similar name +help: there is an associated function `misspellable` with a similar name | LL | Enum::misspellable(); | ~~~~~~~~~~~~ @@ -21,7 +21,7 @@ LL | enum Enum { Variant } LL | Enum::mispellable_trait(); | ^^^^^^^^^^^^^^^^^ variant or associated item not found in `Enum` | -help: there is an associated function with a similar name +help: there is an associated function `misspellable_trait` with a similar name | LL | Enum::misspellable_trait(); | ~~~~~~~~~~~~~~~~~~ @@ -35,7 +35,7 @@ LL | enum Enum { Variant } LL | Enum::MISPELLABLE; | ^^^^^^^^^^^ variant or associated item not found in `Enum` | -help: there is an associated constant with a similar name +help: there is an associated constant `MISSPELLABLE` with a similar name | LL | Enum::MISSPELLABLE; | ~~~~~~~~~~~~ diff --git a/tests/ui/attributes/rustc_confusables.rs b/tests/ui/attributes/rustc_confusables.rs index fed6c49ce6c..93d9a7d572c 100644 --- a/tests/ui/attributes/rustc_confusables.rs +++ b/tests/ui/attributes/rustc_confusables.rs @@ -11,7 +11,7 @@ fn main() { let x = BTreeSet {}; x.inser(); //~^ ERROR no method named - //~| HELP there is a method with a similar name + //~| HELP there is a method `insert` with a similar name x.foo(); //~^ ERROR no method named x.push(); diff --git a/tests/ui/attributes/rustc_confusables.stderr b/tests/ui/attributes/rustc_confusables.stderr index 60dc0e396ba..9e37d5f5083 100644 --- a/tests/ui/attributes/rustc_confusables.stderr +++ b/tests/ui/attributes/rustc_confusables.stderr @@ -33,7 +33,7 @@ error[E0599]: no method named `inser` found for struct `rustc_confusables_across LL | x.inser(); | ^^^^^ | -help: there is a method with a similar name +help: there is a method `insert` with a similar name | LL | x.insert(); | ~~~~~~ diff --git a/tests/ui/block-result/issue-3563.stderr b/tests/ui/block-result/issue-3563.stderr index 3381ae5f657..22606a52f85 100644 --- a/tests/ui/block-result/issue-3563.stderr +++ b/tests/ui/block-result/issue-3563.stderr @@ -4,7 +4,7 @@ error[E0599]: no method named `b` found for reference `&Self` in the current sco LL | || self.b() | ^ | -help: there is a method with a similar name +help: there is a method `a` with a similar name | LL | || self.a() | ~ diff --git a/tests/ui/confuse-field-and-method/issue-33784.stderr b/tests/ui/confuse-field-and-method/issue-33784.stderr index f6678dc8543..8acd1f8ff1e 100644 --- a/tests/ui/confuse-field-and-method/issue-33784.stderr +++ b/tests/ui/confuse-field-and-method/issue-33784.stderr @@ -8,7 +8,7 @@ help: to call the function stored in `closure`, surround the field access with p | LL | (p.closure)(); | + + -help: there is a method with a similar name +help: there is a method `clone` with a similar name | LL | p.clone(); | ~~~~~ diff --git a/tests/ui/impl-trait/no-method-suggested-traits.stderr b/tests/ui/impl-trait/no-method-suggested-traits.stderr index b9a6a281b84..7a4dc366618 100644 --- a/tests/ui/impl-trait/no-method-suggested-traits.stderr +++ b/tests/ui/impl-trait/no-method-suggested-traits.stderr @@ -15,7 +15,7 @@ LL + use no_method_suggested_traits::foo::PubPub; | LL + use no_method_suggested_traits::qux::PrivPub; | -help: there is a method with a similar name +help: there is a method `method2` with a similar name | LL | 1u32.method2(); | ~~~~~~~ @@ -37,7 +37,7 @@ LL + use no_method_suggested_traits::foo::PubPub; | LL + use no_method_suggested_traits::qux::PrivPub; | -help: there is a method with a similar name +help: there is a method `method2` with a similar name | LL | std::rc::Rc::new(&mut Box::new(&1u32)).method2(); | ~~~~~~~ @@ -56,7 +56,7 @@ help: trait `Bar` which provides `method` is implemented but not in scope; perha | LL + use foo::Bar; | -help: there is a method with a similar name +help: there is a method `method2` with a similar name | LL | 'a'.method2(); | ~~~~~~~ @@ -72,7 +72,7 @@ help: trait `Bar` which provides `method` is implemented but not in scope; perha | LL + use foo::Bar; | -help: there is a method with a similar name +help: there is a method `method2` with a similar name | LL | std::rc::Rc::new(&mut Box::new(&'a')).method2(); | ~~~~~~~ @@ -93,7 +93,7 @@ help: trait `PubPub` which provides `method` is implemented but not in scope; pe | LL + use no_method_suggested_traits::foo::PubPub; | -help: there is a method with a similar name +help: there is a method `method3` with a similar name | LL | 1i32.method3(); | ~~~~~~~ @@ -109,7 +109,7 @@ help: trait `PubPub` which provides `method` is implemented but not in scope; pe | LL + use no_method_suggested_traits::foo::PubPub; | -help: there is a method with a similar name +help: there is a method `method3` with a similar name | LL | std::rc::Rc::new(&mut Box::new(&1i32)).method3(); | ~~~~~~~ diff --git a/tests/ui/issues/issue-56175.stderr b/tests/ui/issues/issue-56175.stderr index 882d4e99327..6ed35c3a3d3 100644 --- a/tests/ui/issues/issue-56175.stderr +++ b/tests/ui/issues/issue-56175.stderr @@ -14,7 +14,7 @@ help: trait `Trait` which provides `trait_method` is implemented but not in scop | LL + use reexported_trait::Trait; | -help: there is a method with a similar name +help: there is a method `trait_method_b` with a similar name | LL | reexported_trait::FooStruct.trait_method_b(); | ~~~~~~~~~~~~~~ @@ -35,7 +35,7 @@ help: trait `TraitB` which provides `trait_method_b` is implemented but not in s | LL + use reexported_trait::TraitBRename; | -help: there is a method with a similar name +help: there is a method `trait_method` with a similar name | LL | reexported_trait::FooStruct.trait_method(); | ~~~~~~~~~~~~ diff --git a/tests/ui/methods/issues/issue-105732.stderr b/tests/ui/methods/issues/issue-105732.stderr index 906bd6c8636..a4924b3e663 100644 --- a/tests/ui/methods/issues/issue-105732.stderr +++ b/tests/ui/methods/issues/issue-105732.stderr @@ -12,7 +12,7 @@ error[E0599]: no method named `g` found for reference `&Self` in the current sco LL | self.g(); | ^ | -help: there is a method with a similar name +help: there is a method `f` with a similar name | LL | self.f(); | ~ diff --git a/tests/ui/methods/method-not-found-but-doc-alias.stderr b/tests/ui/methods/method-not-found-but-doc-alias.stderr index d8c2ea00137..c49ffa8971f 100644 --- a/tests/ui/methods/method-not-found-but-doc-alias.stderr +++ b/tests/ui/methods/method-not-found-but-doc-alias.stderr @@ -7,7 +7,7 @@ LL | struct Foo; LL | Foo.quux(); | ^^^^ | -help: there is a method with a similar name +help: there is a method `bar` with a similar name | LL | Foo.bar(); | ~~~ diff --git a/tests/ui/object-pointer-types.stderr b/tests/ui/object-pointer-types.stderr index e581d2d40bd..7d915ebdab6 100644 --- a/tests/ui/object-pointer-types.stderr +++ b/tests/ui/object-pointer-types.stderr @@ -7,7 +7,7 @@ LL | fn owned(self: Box); LL | x.owned(); | ^^^^^ | -help: there is a method with a similar name +help: there is a method `to_owned` with a similar name | LL | x.to_owned(); | ~~~~~~~~ diff --git a/tests/ui/parser/emoji-identifiers.stderr b/tests/ui/parser/emoji-identifiers.stderr index cd306696dde..536afc53f0c 100644 --- a/tests/ui/parser/emoji-identifiers.stderr +++ b/tests/ui/parser/emoji-identifiers.stderr @@ -78,7 +78,7 @@ note: if you're trying to build a new `πŸ‘€`, consider using `πŸ‘€::full_of_✨` | LL | fn full_of_✨() -> πŸ‘€ { | ^^^^^^^^^^^^^^^^^^^^^ -help: there is an associated function with a similar name +help: there is an associated function `full_of_✨` with a similar name | LL | πŸ‘€::full_of_✨() | ~~~~~~~~~~ diff --git a/tests/ui/resolve/typo-suggestion-mistyped-in-path.stderr b/tests/ui/resolve/typo-suggestion-mistyped-in-path.stderr index b0061217500..f4fb7fd955f 100644 --- a/tests/ui/resolve/typo-suggestion-mistyped-in-path.stderr +++ b/tests/ui/resolve/typo-suggestion-mistyped-in-path.stderr @@ -16,7 +16,7 @@ LL | struct Struct; LL | Struct::fob(); | ^^^ function or associated item not found in `Struct` | -help: there is an associated function with a similar name +help: there is an associated function `foo` with a similar name | LL | Struct::foo(); | ~~~ diff --git a/tests/ui/rust-2018/trait-import-suggestions.stderr b/tests/ui/rust-2018/trait-import-suggestions.stderr index 9d7b68041e2..85262888579 100644 --- a/tests/ui/rust-2018/trait-import-suggestions.stderr +++ b/tests/ui/rust-2018/trait-import-suggestions.stderr @@ -12,7 +12,7 @@ help: trait `Foobar` which provides `foobar` is implemented but not in scope; pe | LL + use crate::foo::foobar::Foobar; | -help: there is a method with a similar name +help: there is a method `bar` with a similar name | LL | x.bar(); | ~~~ @@ -31,7 +31,7 @@ help: trait `Bar` which provides `bar` is implemented but not in scope; perhaps | LL + use crate::foo::Bar; | -help: there is a method with a similar name +help: there is a method `foobar` with a similar name | LL | x.foobar(); | ~~~~~~ @@ -42,7 +42,7 @@ error[E0599]: no method named `baz` found for type `u32` in the current scope LL | x.baz(); | ^^^ | -help: there is a method with a similar name +help: there is a method `bar` with a similar name | LL | x.bar(); | ~~~ @@ -58,7 +58,7 @@ help: trait `FromStr` which provides `from_str` is implemented but not in scope; | LL + use std::str::FromStr; | -help: there is an associated function with a similar name +help: there is an associated function `from` with a similar name | LL | let y = u32::from("33"); | ~~~~ diff --git a/tests/ui/rust-2021/future-prelude-collision-shadow.stderr b/tests/ui/rust-2021/future-prelude-collision-shadow.stderr index a8fcf43cd63..d9c0fa47eca 100644 --- a/tests/ui/rust-2021/future-prelude-collision-shadow.stderr +++ b/tests/ui/rust-2021/future-prelude-collision-shadow.stderr @@ -12,7 +12,7 @@ LL + use crate::m::TryIntoU32; | LL + use std::convert::TryInto; | -help: there is a method with a similar name +help: there is a method `into` with a similar name | LL | let _: u32 = 3u8.into().unwrap(); | ~~~~ diff --git a/tests/ui/self/arbitrary_self_type_mut_difference.stderr b/tests/ui/self/arbitrary_self_type_mut_difference.stderr index 2a7192a83f5..ffc61ee0d78 100644 --- a/tests/ui/self/arbitrary_self_type_mut_difference.stderr +++ b/tests/ui/self/arbitrary_self_type_mut_difference.stderr @@ -9,7 +9,7 @@ note: method is available for `Pin<&mut S>` | LL | fn x(self: Pin<&mut Self>) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ -help: there is a method with a similar name +help: there is a method `y` with a similar name | LL | Pin::new(&S).y(); | ~ @@ -25,7 +25,7 @@ note: method is available for `Pin<&S>` | LL | fn y(self: Pin<&Self>) {} | ^^^^^^^^^^^^^^^^^^^^^^ -help: there is a method with a similar name +help: there is a method `x` with a similar name | LL | Pin::new(&mut S).x(); | ~ diff --git a/tests/ui/suggestions/issue-109291.stderr b/tests/ui/suggestions/issue-109291.stderr index 7a2821a069b..a173bbbb490 100644 --- a/tests/ui/suggestions/issue-109291.stderr +++ b/tests/ui/suggestions/issue-109291.stderr @@ -10,7 +10,7 @@ note: if you're trying to build a new `Backtrace` consider using one of the foll Backtrace::disabled Backtrace::create --> $SRC_DIR/std/src/backtrace.rs:LL:COL -help: there is an associated function with a similar name +help: there is an associated function `force_capture` with a similar name | LL | println!("Custom backtrace: {}", std::backtrace::Backtrace::force_capture()); | ~~~~~~~~~~~~~ diff --git a/tests/ui/suggestions/suggest-methods.stderr b/tests/ui/suggestions/suggest-methods.stderr index 5115a072426..5bacad8c6e8 100644 --- a/tests/ui/suggestions/suggest-methods.stderr +++ b/tests/ui/suggestions/suggest-methods.stderr @@ -19,7 +19,7 @@ error[E0599]: no method named `is_emtpy` found for struct `String` in the curren LL | let _ = s.is_emtpy(); | ^^^^^^^^ | -help: there is a method with a similar name +help: there is a method `is_empty` with a similar name | LL | let _ = s.is_empty(); | ~~~~~~~~ @@ -30,7 +30,7 @@ error[E0599]: no method named `count_eos` found for type `u32` in the current sc LL | let _ = 63u32.count_eos(); | ^^^^^^^^^ | -help: there is a method with a similar name +help: there is a method `count_zeros` with a similar name | LL | let _ = 63u32.count_zeros(); | ~~~~~~~~~~~ @@ -41,7 +41,7 @@ error[E0599]: no method named `count_o` found for type `u32` in the current scop LL | let _ = 63u32.count_o(); | ^^^^^^^ | -help: there is a method with a similar name +help: there is a method `count_ones` with a similar name | LL | let _ = 63u32.count_ones(); | ~~~~~~~~~~ diff --git a/tests/ui/suggestions/suggest-tryinto-edition-change.stderr b/tests/ui/suggestions/suggest-tryinto-edition-change.stderr index a25a3f44ad2..db7c40101cd 100644 --- a/tests/ui/suggestions/suggest-tryinto-edition-change.stderr +++ b/tests/ui/suggestions/suggest-tryinto-edition-change.stderr @@ -62,7 +62,7 @@ help: trait `TryInto` which provides `try_into` is implemented but not in scope; | LL + use std::convert::TryInto; | -help: there is a method with a similar name +help: there is a method `into` with a similar name | LL | let _i: i16 = 0_i32.into().unwrap(); | ~~~~ diff --git a/tests/ui/traits/item-privacy.stderr b/tests/ui/traits/item-privacy.stderr index 00d75b14227..d08bb4745bf 100644 --- a/tests/ui/traits/item-privacy.stderr +++ b/tests/ui/traits/item-privacy.stderr @@ -12,7 +12,7 @@ help: trait `A` which provides `a` is implemented but not in scope; perhaps you | LL + use method::A; | -help: there is a method with a similar name +help: there is a method `b` with a similar name | LL | S.b(); | ~ @@ -34,7 +34,7 @@ help: trait `B` which provides `b` is implemented but not in scope; perhaps you | LL + use method::B; | -help: there is a method with a similar name +help: there is a method `c` with a similar name | LL | S.c(); | ~ @@ -111,7 +111,7 @@ help: trait `A` which provides `A` is implemented but not in scope; perhaps you | LL + use assoc_const::A; | -help: there is an associated constant with a similar name +help: there is an associated constant `B` with a similar name | LL | S::B; | ~ @@ -130,7 +130,7 @@ help: trait `B` which provides `B` is implemented but not in scope; perhaps you | LL + use assoc_const::B; | -help: there is a method with a similar name +help: there is a method `b` with a similar name | LL | S::b; | ~ diff --git a/tests/ui/traits/trait-upcasting/subtrait-method.stderr b/tests/ui/traits/trait-upcasting/subtrait-method.stderr index f3eb86c7681..0408be6986b 100644 --- a/tests/ui/traits/trait-upcasting/subtrait-method.stderr +++ b/tests/ui/traits/trait-upcasting/subtrait-method.stderr @@ -10,7 +10,7 @@ note: `Baz` defines an item `c`, perhaps you need to implement it | LL | trait Baz: Bar { | ^^^^^^^^^^^^^^ -help: there is a method with a similar name +help: there is a method `a` with a similar name | LL | bar.a(); | ~ @@ -27,7 +27,7 @@ note: `Bar` defines an item `b`, perhaps you need to implement it | LL | trait Bar: Foo { | ^^^^^^^^^^^^^^ -help: there is a method with a similar name +help: there is a method `a` with a similar name | LL | foo.a(); | ~ @@ -44,7 +44,7 @@ note: `Baz` defines an item `c`, perhaps you need to implement it | LL | trait Baz: Bar { | ^^^^^^^^^^^^^^ -help: there is a method with a similar name +help: there is a method `a` with a similar name | LL | foo.a(); | ~ @@ -61,7 +61,7 @@ note: `Bar` defines an item `b`, perhaps you need to implement it | LL | trait Bar: Foo { | ^^^^^^^^^^^^^^ -help: there is a method with a similar name +help: there is a method `a` with a similar name | LL | foo.a(); | ~ @@ -78,7 +78,7 @@ note: `Baz` defines an item `c`, perhaps you need to implement it | LL | trait Baz: Bar { | ^^^^^^^^^^^^^^ -help: there is a method with a similar name +help: there is a method `a` with a similar name | LL | foo.a(); | ~