Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: remove unnecessary string searchings #100796

Merged
merged 1 commit into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> bool {
// It only make sense when suggesting dereferences for arguments
let ObligationCauseCode::FunctionArgumentObligation { .. } = obligation.cause.code() else {
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else {
return false;
};
let param_env = obligation.param_env;
Expand Down Expand Up @@ -702,19 +702,22 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
Some(steps).filter(|_| self.predicate_may_hold(&obligation))
}) {
if steps > 0 {
if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) {
// Don't care about `&mut` because `DerefMut` is used less
// often and user will not expect autoderef happens.
if src.starts_with('&') && !src.starts_with("&mut ") {
let derefs = "*".repeat(steps);
err.span_suggestion(
span,
"consider dereferencing here",
format!("&{}{}", derefs, &src[1..]),
Applicability::MachineApplicable,
);
return true;
}
// Don't care about `&mut` because `DerefMut` is used less
// often and user will not expect autoderef happens.
if let Some(hir::Node::Expr(hir::Expr {
kind:
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, expr),
..
})) = self.tcx.hir().find(*arg_hir_id)
{
let derefs = "*".repeat(steps);
err.span_suggestion_verbose(
expr.span.shrink_to_lo(),
"consider dereferencing here",
derefs,
Applicability::MachineApplicable,
);
return true;
}
}
} else if real_trait_pred != trait_pred {
Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/traits/suggest-deferences/issue-39029.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ error[E0277]: the trait bound `NoToSocketAddrs: ToSocketAddrs` is not satisfied
--> $DIR/issue-39029.rs:16:37
|
LL | let _errors = TcpListener::bind(&bad);
| ----------------- ^^^^
| | |
| | the trait `ToSocketAddrs` is not implemented for `NoToSocketAddrs`
| | help: consider dereferencing here: `&*bad`
| ----------------- ^^^^ the trait `ToSocketAddrs` is not implemented for `NoToSocketAddrs`
| |
| required by a bound introduced by this call
|
= note: required because of the requirements on the impl of `ToSocketAddrs` for `&NoToSocketAddrs`
Expand All @@ -14,6 +12,10 @@ note: required by a bound in `TcpListener::bind`
|
LL | pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
| ^^^^^^^^^^^^^ required by this bound in `TcpListener::bind`
help: consider dereferencing here
|
LL | let _errors = TcpListener::bind(&*bad);
| +

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/traits/suggest-deferences/issue-62530.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ error[E0277]: the trait bound `&String: SomeTrait` is not satisfied
--> $DIR/issue-62530.rs:13:26
|
LL | takes_type_parameter(&string); // Error
| -------------------- ^^^^^^^
| | |
| | the trait `SomeTrait` is not implemented for `&String`
| | help: consider dereferencing here: `&*string`
| -------------------- ^^^^^^^ the trait `SomeTrait` is not implemented for `&String`
| |
| required by a bound introduced by this call
|
note: required by a bound in `takes_type_parameter`
--> $DIR/issue-62530.rs:4:44
|
LL | fn takes_type_parameter<T>(_x: T) where T: SomeTrait {}
| ^^^^^^^^^ required by this bound in `takes_type_parameter`
help: consider dereferencing here
|
LL | takes_type_parameter(&*string); // Error
| +

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/traits/suggest-deferences/multiple-0.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ error[E0277]: the trait bound `&Baz: Happy` is not satisfied
--> $DIR/multiple-0.rs:34:9
|
LL | foo(&baz);
| --- ^^^^
| | |
| | the trait `Happy` is not implemented for `&Baz`
| | help: consider dereferencing here: `&***baz`
| --- ^^^^ the trait `Happy` is not implemented for `&Baz`
| |
| required by a bound introduced by this call
|
note: required by a bound in `foo`
--> $DIR/multiple-0.rs:30:26
|
LL | fn foo<T>(_: T) where T: Happy {}
| ^^^^^ required by this bound in `foo`
help: consider dereferencing here
|
LL | foo(&***baz);
| +++

error: aborting due to previous error

Expand Down