Skip to content

Commit 4a6304f

Browse files
authored
Rollup merge of #65194 - estebank:remove_str, r=petrochenkov
Use structured suggestion for removal of `as_str()` call Follow up to #64739.
2 parents 88d75c3 + 5aa37a9 commit 4a6304f

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/librustc_typeck/check/method/suggest.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
538538
}
539539
}
540540

541+
let mut fallback_span = true;
542+
let msg = "remove this method call";
541543
if item_name.as_str() == "as_str" && actual.peel_refs().is_str() {
542-
// FIXME: the span is not quite correct, it should point to ".as_str()" instead
543-
// of just "as_str".
544-
err.span_label(
545-
span,
546-
"try removing `as_str`"
547-
);
544+
if let SelfSource::MethodCall(expr) = source {
545+
let call_expr = self.tcx.hir().expect_expr(
546+
self.tcx.hir().get_parent_node(expr.hir_id),
547+
);
548+
if let Some(span) = call_expr.span.trim_start(expr.span) {
549+
err.span_suggestion(
550+
span,
551+
msg,
552+
String::new(),
553+
Applicability::MachineApplicable,
554+
);
555+
fallback_span = false;
556+
}
557+
}
558+
if fallback_span {
559+
err.span_label(span, msg);
560+
}
548561
} else if let Some(lev_candidate) = lev_candidate {
549562
let def_kind = lev_candidate.def_kind();
550563
err.span_suggestion(

src/test/ui/suggestions/remove-as_str.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ error[E0599]: no method named `as_str` found for type `&str` in the current scop
22
--> $DIR/remove-as_str.rs:2:7
33
|
44
LL | s.as_str();
5-
| ^^^^^^ try removing `as_str`
5+
| -^^^^^^-- help: remove this method call
66

77
error[E0599]: no method named `as_str` found for type `&'a str` in the current scope
88
--> $DIR/remove-as_str.rs:7:7
99
|
1010
LL | s.as_str();
11-
| ^^^^^^ try removing `as_str`
11+
| -^^^^^^-- help: remove this method call
1212

1313
error[E0599]: no method named `as_str` found for type `&mut str` in the current scope
1414
--> $DIR/remove-as_str.rs:12:7
1515
|
1616
LL | s.as_str();
17-
| ^^^^^^ try removing `as_str`
17+
| -^^^^^^-- help: remove this method call
1818

1919
error[E0599]: no method named `as_str` found for type `&&str` in the current scope
2020
--> $DIR/remove-as_str.rs:17:7
2121
|
2222
LL | s.as_str();
23-
| ^^^^^^ try removing `as_str`
23+
| -^^^^^^-- help: remove this method call
2424

2525
error: aborting due to 4 previous errors
2626

0 commit comments

Comments
 (0)