Skip to content

Commit d0a7179

Browse files
authored
Rollup merge of rust-lang#100796 - TaKO8Ki:remove-unnecessary-string-searching, r=compiler-errors
Refactor: remove unnecessary string searchings This patch removes unnecessary string searchings for checking if function arguments have `&` and `&mut`.
2 parents 2a862b8 + 9735107 commit d0a7179

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
671671
trait_pred: ty::PolyTraitPredicate<'tcx>,
672672
) -> bool {
673673
// It only make sense when suggesting dereferences for arguments
674-
let ObligationCauseCode::FunctionArgumentObligation { .. } = obligation.cause.code() else {
674+
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else {
675675
return false;
676676
};
677677
let param_env = obligation.param_env;
@@ -702,19 +702,22 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
702702
Some(steps).filter(|_| self.predicate_may_hold(&obligation))
703703
}) {
704704
if steps > 0 {
705-
if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) {
706-
// Don't care about `&mut` because `DerefMut` is used less
707-
// often and user will not expect autoderef happens.
708-
if src.starts_with('&') && !src.starts_with("&mut ") {
709-
let derefs = "*".repeat(steps);
710-
err.span_suggestion(
711-
span,
712-
"consider dereferencing here",
713-
format!("&{}{}", derefs, &src[1..]),
714-
Applicability::MachineApplicable,
715-
);
716-
return true;
717-
}
705+
// Don't care about `&mut` because `DerefMut` is used less
706+
// often and user will not expect autoderef happens.
707+
if let Some(hir::Node::Expr(hir::Expr {
708+
kind:
709+
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, expr),
710+
..
711+
})) = self.tcx.hir().find(*arg_hir_id)
712+
{
713+
let derefs = "*".repeat(steps);
714+
err.span_suggestion_verbose(
715+
expr.span.shrink_to_lo(),
716+
"consider dereferencing here",
717+
derefs,
718+
Applicability::MachineApplicable,
719+
);
720+
return true;
718721
}
719722
}
720723
} else if real_trait_pred != trait_pred {

src/test/ui/traits/suggest-deferences/issue-39029.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ error[E0277]: the trait bound `NoToSocketAddrs: ToSocketAddrs` is not satisfied
22
--> $DIR/issue-39029.rs:16:37
33
|
44
LL | let _errors = TcpListener::bind(&bad);
5-
| ----------------- ^^^^
6-
| | |
7-
| | the trait `ToSocketAddrs` is not implemented for `NoToSocketAddrs`
8-
| | help: consider dereferencing here: `&*bad`
5+
| ----------------- ^^^^ the trait `ToSocketAddrs` is not implemented for `NoToSocketAddrs`
6+
| |
97
| required by a bound introduced by this call
108
|
119
= note: required for `&NoToSocketAddrs` to implement `ToSocketAddrs`
@@ -14,6 +12,10 @@ note: required by a bound in `TcpListener::bind`
1412
|
1513
LL | pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
1614
| ^^^^^^^^^^^^^ required by this bound in `TcpListener::bind`
15+
help: consider dereferencing here
16+
|
17+
LL | let _errors = TcpListener::bind(&*bad);
18+
| +
1719

1820
error: aborting due to previous error
1921

src/test/ui/traits/suggest-deferences/issue-62530.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0277]: the trait bound `&String: SomeTrait` is not satisfied
22
--> $DIR/issue-62530.rs:13:26
33
|
44
LL | takes_type_parameter(&string); // Error
5-
| -------------------- ^^^^^^^
6-
| | |
7-
| | the trait `SomeTrait` is not implemented for `&String`
8-
| | help: consider dereferencing here: `&*string`
5+
| -------------------- ^^^^^^^ the trait `SomeTrait` is not implemented for `&String`
6+
| |
97
| required by a bound introduced by this call
108
|
119
note: required by a bound in `takes_type_parameter`
1210
--> $DIR/issue-62530.rs:4:44
1311
|
1412
LL | fn takes_type_parameter<T>(_x: T) where T: SomeTrait {}
1513
| ^^^^^^^^^ required by this bound in `takes_type_parameter`
14+
help: consider dereferencing here
15+
|
16+
LL | takes_type_parameter(&*string); // Error
17+
| +
1618

1719
error: aborting due to previous error
1820

src/test/ui/traits/suggest-deferences/multiple-0.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ error[E0277]: the trait bound `&Baz: Happy` is not satisfied
22
--> $DIR/multiple-0.rs:34:9
33
|
44
LL | foo(&baz);
5-
| --- ^^^^
6-
| | |
7-
| | the trait `Happy` is not implemented for `&Baz`
8-
| | help: consider dereferencing here: `&***baz`
5+
| --- ^^^^ the trait `Happy` is not implemented for `&Baz`
6+
| |
97
| required by a bound introduced by this call
108
|
119
note: required by a bound in `foo`
1210
--> $DIR/multiple-0.rs:30:26
1311
|
1412
LL | fn foo<T>(_: T) where T: Happy {}
1513
| ^^^^^ required by this bound in `foo`
14+
help: consider dereferencing here
15+
|
16+
LL | foo(&***baz);
17+
| +++
1618

1719
error: aborting due to previous error
1820

0 commit comments

Comments
 (0)