Skip to content

Commit 28c0287

Browse files
committed
Deduplicate some logic and reword output
1 parent b59bd7f commit 28c0287

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
@@ -1427,6 +1427,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14271427
let tcx = self.tcx;
14281428
let def_kind = similar_candidate.kind.as_def_kind();
14291429
let an = self.tcx.def_kind_descr_article(def_kind, similar_candidate.def_id);
1430+
let msg = format!(
1431+
"there is {an} {} `{}` with a similar name",
1432+
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
1433+
similar_candidate.name,
1434+
);
14301435
// Methods are defined within the context of a struct and their first parameter
14311436
// is always `self`, which represents the instance of the struct the method is
14321437
// 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> {
14431448
// call expression the user wrote.
14441449
err.span_suggestion_verbose(
14451450
span,
1446-
format!("there is {an} method with a similar name"),
1451+
msg,
14471452
similar_candidate.name,
14481453
Applicability::MaybeIncorrect,
14491454
);
@@ -1453,8 +1458,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14531458
err.span_help(
14541459
tcx.def_span(similar_candidate.def_id),
14551460
format!(
1456-
"there is {an} method `{}` with a similar name{}",
1457-
similar_candidate.name,
1461+
"{msg}{}",
14581462
if let None = args { "" } else { ", but with different arguments" },
14591463
),
14601464
);
@@ -1466,47 +1470,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14661470
// function we found.
14671471
err.span_suggestion_verbose(
14681472
span,
1469-
format!(
1470-
"there is {an} {} with a similar name",
1471-
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id)
1472-
),
1473+
msg,
14731474
similar_candidate.name,
14741475
Applicability::MaybeIncorrect,
14751476
);
14761477
} else {
1477-
err.span_help(
1478-
tcx.def_span(similar_candidate.def_id),
1479-
format!(
1480-
"there is {an} {} `{}` with a similar name",
1481-
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
1482-
similar_candidate.name,
1483-
),
1484-
);
1478+
err.span_help(tcx.def_span(similar_candidate.def_id), msg);
14851479
}
14861480
} else if let Mode::Path = mode
14871481
&& args.unwrap_or(&[]).is_empty()
14881482
{
14891483
// We have an associated item syntax and we found something that isn't an fn.
14901484
err.span_suggestion_verbose(
14911485
span,
1492-
format!(
1493-
"there is {an} {} with a similar name",
1494-
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id)
1495-
),
1486+
msg,
14961487
similar_candidate.name,
14971488
Applicability::MaybeIncorrect,
14981489
);
14991490
} else {
15001491
// The expression is a function or method call, but the item we found is an
15011492
// associated const or type.
1502-
err.span_help(
1503-
tcx.def_span(similar_candidate.def_id),
1504-
format!(
1505-
"there is {an} {} `{}` with a similar name",
1506-
self.tcx.def_kind_descr(def_kind, similar_candidate.def_id),
1507-
similar_candidate.name,
1508-
),
1509-
);
1493+
err.span_help(tcx.def_span(similar_candidate.def_id), msg);
15101494
}
15111495
}
15121496

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)