1
Fork 0

Rollup merge of #65194 - estebank:remove_str, r=petrochenkov

Use structured suggestion for removal of `as_str()` call

Follow up to #64739.
This commit is contained in:
Mazdak Farrokhzad 2019-10-08 23:31:27 +02:00 committed by GitHub
commit 4a6304fb05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View file

@ -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(

View file

@ -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