Skip to content

Commit

Permalink
Rollup merge of rust-lang#65194 - estebank:remove_str, r=petrochenkov
Browse files Browse the repository at this point in the history
Use structured suggestion for removal of `as_str()` call

Follow up to rust-lang#64739.
  • Loading branch information
Centril authored Oct 8, 2019
2 parents 88d75c3 + 5aa37a9 commit 4a6304f
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 @@ -538,13 +538,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 4a6304f

Please sign in to comment.