Skip to content

Commit

Permalink
Use structured suggestion for removal of as_str() call
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 7, 2019
1 parent f3c9cec commit 5aa37a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
25 changes: 19 additions & 6 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

let mut fallback_span = true;
let msg = "remove this method call";
if item_name.as_str() == "as_str" && actual.peel_refs().is_str() {
// FIXME: the span is not quite correct, it should point to ".as_str()" instead
// of just "as_str".
err.span_label(
span,
"try removing `as_str`"
);
if let SelfSource::MethodCall(expr) = source {
let call_expr = self.tcx.hir().expect_expr(
self.tcx.hir().get_parent_node(expr.hir_id),
);
if let Some(span) = call_expr.span.trim_start(expr.span) {
err.span_suggestion(
span,
msg,
String::new(),
Applicability::MachineApplicable,
);
fallback_span = false;
}
}
if fallback_span {
err.span_label(span, msg);
}
} else if let Some(lev_candidate) = lev_candidate {
let def_kind = lev_candidate.def_kind();
err.span_suggestion(
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/suggestions/remove-as_str.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ error[E0599]: no method named `as_str` found for type `&str` in the current scop
--> $DIR/remove-as_str.rs:2:7
|
LL | s.as_str();
| ^^^^^^ try removing `as_str`
| -^^^^^^-- help: remove this method call

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

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

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

error: aborting due to 4 previous errors

Expand Down

0 comments on commit 5aa37a9

Please sign in to comment.