Skip to content

Commit 5aa37a9

Browse files
committed
Use structured suggestion for removal of as_str() call
1 parent f3c9cec commit 5aa37a9

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
@@ -518,13 +518,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
518518
}
519519
}
520520

521+
let mut fallback_span = true;
522+
let msg = "remove this method call";
521523
if item_name.as_str() == "as_str" && actual.peel_refs().is_str() {
522-
// FIXME: the span is not quite correct, it should point to ".as_str()" instead
523-
// of just "as_str".
524-
err.span_label(
525-
span,
526-
"try removing `as_str`"
527-
);
524+
if let SelfSource::MethodCall(expr) = source {
525+
let call_expr = self.tcx.hir().expect_expr(
526+
self.tcx.hir().get_parent_node(expr.hir_id),
527+
);
528+
if let Some(span) = call_expr.span.trim_start(expr.span) {
529+
err.span_suggestion(
530+
span,
531+
msg,
532+
String::new(),
533+
Applicability::MachineApplicable,
534+
);
535+
fallback_span = false;
536+
}
537+
}
538+
if fallback_span {
539+
err.span_label(span, msg);
540+
}
528541
} else if let Some(lev_candidate) = lev_candidate {
529542
let def_kind = lev_candidate.def_kind();
530543
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)