Skip to content

Commit 04884bc

Browse files
committed
Deduplicate some logic and reword output
1 parent 997351d commit 04884bc

21 files changed

+52
-68
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+11-27
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13251325
let tcx = self.tcx;
13261326
let def_kind = similar_candidate.kind.as_def_kind();
13271327
let an = self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id);
1328+
let msg = format!(
1329+
"there is {an} {} `{}` with a similar name",
1330+
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
1331+
similar_candidate.name,
1332+
);
13281333
// Methods are defined within the context of a struct and their first parameter
13291334
// is always `self`, which represents the instance of the struct the method is
13301335
// being called on Associated functions don’t take self as a parameter and they are
@@ -1341,7 +1346,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13411346
// call expression the user wrote.
13421347
err.span_suggestion_verbose(
13431348
span,
1344-
format!("there is {an} method with a similar name"),
1349+
msg,
13451350
similar_candidate.name,
13461351
Applicability::MaybeIncorrect,
13471352
);
@@ -1351,8 +1356,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13511356
err.span_help(
13521357
tcx.def_span(similar_candidate.def_id),
13531358
format!(
1354-
"there is {an} method `{}` with a similar name{}",
1355-
similar_candidate.name,
1359+
"{msg}{}",
13561360
if let None = args { "" } else { ", but with different arguments" },
13571361
),
13581362
);
@@ -1364,47 +1368,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13641368
// function we found.
13651369
err.span_suggestion_verbose(
13661370
span,
1367-
format!(
1368-
"there is {an} {} with a similar name",
1369-
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id)
1370-
),
1371+
msg,
13711372
similar_candidate.name,
13721373
Applicability::MaybeIncorrect,
13731374
);
13741375
} else {
1375-
err.span_help(
1376-
tcx.def_span(similar_candidate.def_id),
1377-
format!(
1378-
"there is {an} {} `{}` with a similar name",
1379-
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
1380-
similar_candidate.name,
1381-
),
1382-
);
1376+
err.span_help(tcx.def_span(similar_candidate.def_id), msg);
13831377
}
13841378
} else if let Mode::Path = mode
13851379
&& args.unwrap_or(&[]).is_empty()
13861380
{
13871381
// We have an associated item syntax and we found something that isn't an fn.
13881382
err.span_suggestion_verbose(
13891383
span,
1390-
format!(
1391-
"there is {an} {} with a similar name",
1392-
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id)
1393-
),
1384+
msg,
13941385
similar_candidate.name,
13951386
Applicability::MaybeIncorrect,
13961387
);
13971388
} else {
13981389
// The expression is a function or method call, but the item we found is an
13991390
// associated const or type.
1400-
err.span_help(
1401-
tcx.def_span(similar_candidate.def_id),
1402-
format!(
1403-
"there is {an} {} `{}` with a similar name",
1404-
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
1405-
similar_candidate.name,
1406-
),
1407-
);
1391+
err.span_help(tcx.def_span(similar_candidate.def_id), msg);
14081392
}
14091393
}
14101394

tests/ui/associated-item/associated-item-enum.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | enum Enum { Variant }
77
LL | Enum::mispellable();
88
| ^^^^^^^^^^^ variant or associated item not found in `Enum`
99
|
10-
help: there is an associated function with a similar name
10+
help: there is an associated function `misspellable` with a similar name
1111
|
1212
LL | Enum::misspellable();
1313
| ~~~~~~~~~~~~
@@ -21,7 +21,7 @@ LL | enum Enum { Variant }
2121
LL | Enum::mispellable_trait();
2222
| ^^^^^^^^^^^^^^^^^ variant or associated item not found in `Enum`
2323
|
24-
help: there is an associated function with a similar name
24+
help: there is an associated function `misspellable_trait` with a similar name
2525
|
2626
LL | Enum::misspellable_trait();
2727
| ~~~~~~~~~~~~~~~~~~
@@ -35,7 +35,7 @@ LL | enum Enum { Variant }
3535
LL | Enum::MISPELLABLE;
3636
| ^^^^^^^^^^^ variant or associated item not found in `Enum`
3737
|
38-
help: there is an associated constant with a similar name
38+
help: there is an associated constant `MISSPELLABLE` with a similar name
3939
|
4040
LL | Enum::MISSPELLABLE;
4141
| ~~~~~~~~~~~~

tests/ui/attributes/rustc_confusables.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
let x = BTreeSet {};
1212
x.inser();
1313
//~^ ERROR no method named
14-
//~| HELP there is a method with a similar name
14+
//~| HELP there is a method `insert` with a similar name
1515
x.foo();
1616
//~^ ERROR no method named
1717
x.push();

tests/ui/attributes/rustc_confusables.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ error[E0599]: no method named `inser` found for struct `rustc_confusables_across
3333
LL | x.inser();
3434
| ^^^^^
3535
|
36-
help: there is a method with a similar name
36+
help: there is a method `insert` with a similar name
3737
|
3838
LL | x.insert();
3939
| ~~~~~~

tests/ui/block-result/issue-3563.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `b` found for reference `&Self` in the current sco
44
LL | || self.b()
55
| ^
66
|
7-
help: there is a method with a similar name
7+
help: there is a method `a` with a similar name
88
|
99
LL | || self.a()
1010
| ~

tests/ui/confuse-field-and-method/issue-33784.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ help: to call the function stored in `closure`, surround the field access with p
88
|
99
LL | (p.closure)();
1010
| + +
11-
help: there is a method with a similar name
11+
help: there is a method `clone` with a similar name
1212
|
1313
LL | p.clone();
1414
| ~~~~~

tests/ui/impl-trait/no-method-suggested-traits.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL + use no_method_suggested_traits::foo::PubPub;
1515
|
1616
LL + use no_method_suggested_traits::qux::PrivPub;
1717
|
18-
help: there is a method with a similar name
18+
help: there is a method `method2` with a similar name
1919
|
2020
LL | 1u32.method2();
2121
| ~~~~~~~
@@ -37,7 +37,7 @@ LL + use no_method_suggested_traits::foo::PubPub;
3737
|
3838
LL + use no_method_suggested_traits::qux::PrivPub;
3939
|
40-
help: there is a method with a similar name
40+
help: there is a method `method2` with a similar name
4141
|
4242
LL | std::rc::Rc::new(&mut Box::new(&1u32)).method2();
4343
| ~~~~~~~
@@ -56,7 +56,7 @@ help: trait `Bar` which provides `method` is implemented but not in scope; perha
5656
|
5757
LL + use foo::Bar;
5858
|
59-
help: there is a method with a similar name
59+
help: there is a method `method2` with a similar name
6060
|
6161
LL | 'a'.method2();
6262
| ~~~~~~~
@@ -72,7 +72,7 @@ help: trait `Bar` which provides `method` is implemented but not in scope; perha
7272
|
7373
LL + use foo::Bar;
7474
|
75-
help: there is a method with a similar name
75+
help: there is a method `method2` with a similar name
7676
|
7777
LL | std::rc::Rc::new(&mut Box::new(&'a')).method2();
7878
| ~~~~~~~
@@ -93,7 +93,7 @@ help: trait `PubPub` which provides `method` is implemented but not in scope; pe
9393
|
9494
LL + use no_method_suggested_traits::foo::PubPub;
9595
|
96-
help: there is a method with a similar name
96+
help: there is a method `method3` with a similar name
9797
|
9898
LL | 1i32.method3();
9999
| ~~~~~~~
@@ -109,7 +109,7 @@ help: trait `PubPub` which provides `method` is implemented but not in scope; pe
109109
|
110110
LL + use no_method_suggested_traits::foo::PubPub;
111111
|
112-
help: there is a method with a similar name
112+
help: there is a method `method3` with a similar name
113113
|
114114
LL | std::rc::Rc::new(&mut Box::new(&1i32)).method3();
115115
| ~~~~~~~

tests/ui/issues/issue-56175.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ help: trait `Trait` which provides `trait_method` is implemented but not in scop
1414
|
1515
LL + use reexported_trait::Trait;
1616
|
17-
help: there is a method with a similar name
17+
help: there is a method `trait_method_b` with a similar name
1818
|
1919
LL | reexported_trait::FooStruct.trait_method_b();
2020
| ~~~~~~~~~~~~~~
@@ -35,7 +35,7 @@ help: trait `TraitB` which provides `trait_method_b` is implemented but not in s
3535
|
3636
LL + use reexported_trait::TraitBRename;
3737
|
38-
help: there is a method with a similar name
38+
help: there is a method `trait_method` with a similar name
3939
|
4040
LL | reexported_trait::FooStruct.trait_method();
4141
| ~~~~~~~~~~~~

tests/ui/methods/issues/issue-105732.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error[E0599]: no method named `g` found for reference `&Self` in the current sco
1212
LL | self.g();
1313
| ^
1414
|
15-
help: there is a method with a similar name
15+
help: there is a method `f` with a similar name
1616
|
1717
LL | self.f();
1818
| ~

tests/ui/methods/method-not-found-but-doc-alias.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | struct Foo;
77
LL | Foo.quux();
88
| ^^^^
99
|
10-
help: there is a method with a similar name
10+
help: there is a method `bar` with a similar name
1111
|
1212
LL | Foo.bar();
1313
| ~~~

tests/ui/object-pointer-types.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn owned(self: Box<Self>);
77
LL | x.owned();
88
| ^^^^^
99
|
10-
help: there is a method with a similar name
10+
help: there is a method `to_owned` with a similar name
1111
|
1212
LL | x.to_owned();
1313
| ~~~~~~~~

tests/ui/parser/emoji-identifiers.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ note: if you're trying to build a new `👀`, consider using `👀::full_of_✨`
7878
|
7979
LL | fn full_of_✨() -> 👀 {
8080
| ^^^^^^^^^^^^^^^^^^^^^
81-
help: there is an associated function with a similar name
81+
help: there is an associated function `full_of_✨` with a similar name
8282
|
8383
LL | 👀::full_of_✨()
8484
| ~~~~~~~~~~

tests/ui/resolve/typo-suggestion-mistyped-in-path.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | struct Struct;
1616
LL | Struct::fob();
1717
| ^^^ function or associated item not found in `Struct`
1818
|
19-
help: there is an associated function with a similar name
19+
help: there is an associated function `foo` with a similar name
2020
|
2121
LL | Struct::foo();
2222
| ~~~

tests/ui/rust-2018/trait-import-suggestions.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ help: trait `Foobar` which provides `foobar` is implemented but not in scope; pe
1212
|
1313
LL + use crate::foo::foobar::Foobar;
1414
|
15-
help: there is a method with a similar name
15+
help: there is a method `bar` with a similar name
1616
|
1717
LL | x.bar();
1818
| ~~~
@@ -31,7 +31,7 @@ help: trait `Bar` which provides `bar` is implemented but not in scope; perhaps
3131
|
3232
LL + use crate::foo::Bar;
3333
|
34-
help: there is a method with a similar name
34+
help: there is a method `foobar` with a similar name
3535
|
3636
LL | x.foobar();
3737
| ~~~~~~
@@ -42,7 +42,7 @@ error[E0599]: no method named `baz` found for type `u32` in the current scope
4242
LL | x.baz();
4343
| ^^^
4444
|
45-
help: there is a method with a similar name
45+
help: there is a method `bar` with a similar name
4646
|
4747
LL | x.bar();
4848
| ~~~
@@ -58,7 +58,7 @@ help: trait `FromStr` which provides `from_str` is implemented but not in scope;
5858
|
5959
LL + use std::str::FromStr;
6060
|
61-
help: there is an associated function with a similar name
61+
help: there is an associated function `from` with a similar name
6262
|
6363
LL | let y = u32::from("33");
6464
| ~~~~

tests/ui/rust-2021/future-prelude-collision-shadow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL + use crate::m::TryIntoU32;
1212
|
1313
LL + use std::convert::TryInto;
1414
|
15-
help: there is a method with a similar name
15+
help: there is a method `into` with a similar name
1616
|
1717
LL | let _: u32 = 3u8.into().unwrap();
1818
| ~~~~

tests/ui/self/arbitrary_self_type_mut_difference.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: method is available for `Pin<&mut S>`
99
|
1010
LL | fn x(self: Pin<&mut Self>) {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
help: there is a method with a similar name
12+
help: there is a method `y` with a similar name
1313
|
1414
LL | Pin::new(&S).y();
1515
| ~
@@ -25,7 +25,7 @@ note: method is available for `Pin<&S>`
2525
|
2626
LL | fn y(self: Pin<&Self>) {}
2727
| ^^^^^^^^^^^^^^^^^^^^^^
28-
help: there is a method with a similar name
28+
help: there is a method `x` with a similar name
2929
|
3030
LL | Pin::new(&mut S).x();
3131
| ~

tests/ui/suggestions/issue-109291.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: if you're trying to build a new `Backtrace` consider using one of the foll
1010
Backtrace::disabled
1111
Backtrace::create
1212
--> $SRC_DIR/std/src/backtrace.rs:LL:COL
13-
help: there is an associated function with a similar name
13+
help: there is an associated function `force_capture` with a similar name
1414
|
1515
LL | println!("Custom backtrace: {}", std::backtrace::Backtrace::force_capture());
1616
| ~~~~~~~~~~~~~

tests/ui/suggestions/suggest-methods.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ error[E0599]: no method named `is_emtpy` found for struct `String` in the curren
1919
LL | let _ = s.is_emtpy();
2020
| ^^^^^^^^
2121
|
22-
help: there is a method with a similar name
22+
help: there is a method `is_empty` with a similar name
2323
|
2424
LL | let _ = s.is_empty();
2525
| ~~~~~~~~
@@ -30,7 +30,7 @@ error[E0599]: no method named `count_eos` found for type `u32` in the current sc
3030
LL | let _ = 63u32.count_eos();
3131
| ^^^^^^^^^
3232
|
33-
help: there is a method with a similar name
33+
help: there is a method `count_zeros` with a similar name
3434
|
3535
LL | let _ = 63u32.count_zeros();
3636
| ~~~~~~~~~~~
@@ -41,7 +41,7 @@ error[E0599]: no method named `count_o` found for type `u32` in the current scop
4141
LL | let _ = 63u32.count_o();
4242
| ^^^^^^^
4343
|
44-
help: there is a method with a similar name
44+
help: there is a method `count_ones` with a similar name
4545
|
4646
LL | let _ = 63u32.count_ones();
4747
| ~~~~~~~~~~

tests/ui/suggestions/suggest-tryinto-edition-change.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ help: trait `TryInto` which provides `try_into` is implemented but not in scope;
6262
|
6363
LL + use std::convert::TryInto;
6464
|
65-
help: there is a method with a similar name
65+
help: there is a method `into` with a similar name
6666
|
6767
LL | let _i: i16 = 0_i32.into().unwrap();
6868
| ~~~~

0 commit comments

Comments
 (0)