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

Lines changed: 11 additions & 27 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
| ~~~

0 commit comments

Comments
 (0)