Skip to content

Commit 99f78d2

Browse files
committed
Don't suggest changing a method inside a expansion
1 parent d9ca9bd commit 99f78d2

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,8 +1723,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17231723
// Don't emit a suggestion if we found an actual method
17241724
// that had unsatisfied trait bounds
17251725
if unsatisfied_predicates.is_empty()
1726-
// ...or if we already suggested that name because of `rustc_confusable` annotation.
1726+
// ...or if we already suggested that name because of `rustc_confusable` annotation
17271727
&& Some(similar_candidate.name()) != confusable_suggested
1728+
// and if the we aren't in an expansion.
1729+
&& !span.from_expansion()
17281730
{
17291731
self.find_likely_intended_associated_item(
17301732
&mut err,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Make sure we don't suggest a method change inside the `write!` macro.
2+
3+
fn main() {
4+
let mut buf = String::new();
5+
let _ = write!(buf, "foo");
6+
//~^ ERROR cannot write into `String`
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0599]: cannot write into `String`
2+
--> $DIR/missing-writer-issue-139830.rs:5:20
3+
|
4+
LL | let _ = write!(buf, "foo");
5+
| ^^^
6+
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
7+
|
8+
= note: the method is available for `String` here
9+
|
10+
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
11+
--> $DIR/missing-writer-issue-139830.rs:5:20
12+
|
13+
LL | let _ = write!(buf, "foo");
14+
| ^^^
15+
= help: items from traits can only be used if the trait is in scope
16+
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
17+
|
18+
LL + use std::fmt::Write;
19+
|
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)