remove unnecessary string searchings

remove unnecessary string searchings for checking if function arguments have `&` and `&mut`
This commit is contained in:
Takayuki Maeda 2022-08-20 15:54:39 +09:00
parent 75b7e52e92
commit 973510749d
4 changed files with 35 additions and 26 deletions

View File

@ -671,7 +671,7 @@ fn suggest_dereferences(
trait_pred: ty::PolyTraitPredicate<'tcx>, trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> bool { ) -> bool {
// It only make sense when suggesting dereferences for arguments // 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; return false;
}; };
let param_env = obligation.param_env; let param_env = obligation.param_env;
@ -702,19 +702,22 @@ fn suggest_dereferences(
Some(steps).filter(|_| self.predicate_may_hold(&obligation)) Some(steps).filter(|_| self.predicate_may_hold(&obligation))
}) { }) {
if steps > 0 { 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
// Don't care about `&mut` because `DerefMut` is used less // often and user will not expect autoderef happens.
// often and user will not expect autoderef happens. if let Some(hir::Node::Expr(hir::Expr {
if src.starts_with('&') && !src.starts_with("&mut ") { kind:
let derefs = "*".repeat(steps); hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, expr),
err.span_suggestion( ..
span, })) = self.tcx.hir().find(*arg_hir_id)
"consider dereferencing here", {
format!("&{}{}", derefs, &src[1..]), let derefs = "*".repeat(steps);
Applicability::MachineApplicable, err.span_suggestion_verbose(
); expr.span.shrink_to_lo(),
return true; "consider dereferencing here",
} derefs,
Applicability::MachineApplicable,
);
return true;
} }
} }
} else if real_trait_pred != trait_pred { } else if real_trait_pred != trait_pred {

View File

@ -2,10 +2,8 @@ error[E0277]: the trait bound `NoToSocketAddrs: ToSocketAddrs` is not satisfied
--> $DIR/issue-39029.rs:16:37 --> $DIR/issue-39029.rs:16:37
| |
LL | let _errors = TcpListener::bind(&bad); LL | let _errors = TcpListener::bind(&bad);
| ----------------- ^^^^ | ----------------- ^^^^ the trait `ToSocketAddrs` is not implemented for `NoToSocketAddrs`
| | | | |
| | the trait `ToSocketAddrs` is not implemented for `NoToSocketAddrs`
| | help: consider dereferencing here: `&*bad`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= note: required because of the requirements on the impl of `ToSocketAddrs` for `&NoToSocketAddrs` = note: required because of the requirements on the impl of `ToSocketAddrs` for `&NoToSocketAddrs`
@ -14,6 +12,10 @@ note: required by a bound in `TcpListener::bind`
| |
LL | pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> { LL | pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
| ^^^^^^^^^^^^^ required by this bound in `TcpListener::bind` | ^^^^^^^^^^^^^ required by this bound in `TcpListener::bind`
help: consider dereferencing here
|
LL | let _errors = TcpListener::bind(&*bad);
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,10 +2,8 @@ error[E0277]: the trait bound `&String: SomeTrait` is not satisfied
--> $DIR/issue-62530.rs:13:26 --> $DIR/issue-62530.rs:13:26
| |
LL | takes_type_parameter(&string); // Error LL | takes_type_parameter(&string); // Error
| -------------------- ^^^^^^^ | -------------------- ^^^^^^^ the trait `SomeTrait` is not implemented for `&String`
| | | | |
| | the trait `SomeTrait` is not implemented for `&String`
| | help: consider dereferencing here: `&*string`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required by a bound in `takes_type_parameter` note: required by a bound in `takes_type_parameter`
@ -13,6 +11,10 @@ note: required by a bound in `takes_type_parameter`
| |
LL | fn takes_type_parameter<T>(_x: T) where T: SomeTrait {} LL | fn takes_type_parameter<T>(_x: T) where T: SomeTrait {}
| ^^^^^^^^^ required by this bound in `takes_type_parameter` | ^^^^^^^^^ required by this bound in `takes_type_parameter`
help: consider dereferencing here
|
LL | takes_type_parameter(&*string); // Error
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,10 +2,8 @@ error[E0277]: the trait bound `&Baz: Happy` is not satisfied
--> $DIR/multiple-0.rs:34:9 --> $DIR/multiple-0.rs:34:9
| |
LL | foo(&baz); LL | foo(&baz);
| --- ^^^^ | --- ^^^^ the trait `Happy` is not implemented for `&Baz`
| | | | |
| | the trait `Happy` is not implemented for `&Baz`
| | help: consider dereferencing here: `&***baz`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required by a bound in `foo` note: required by a bound in `foo`
@ -13,6 +11,10 @@ note: required by a bound in `foo`
| |
LL | fn foo<T>(_: T) where T: Happy {} LL | fn foo<T>(_: T) where T: Happy {}
| ^^^^^ required by this bound in `foo` | ^^^^^ required by this bound in `foo`
help: consider dereferencing here
|
LL | foo(&***baz);
| +++
error: aborting due to previous error error: aborting due to previous error