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

Do not suggest borrow that is already there in fully-qualified call #132469

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -1209,6 +1209,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
c @ ObligationCauseCode::WhereClauseInExpr(_, _, hir_id, _)
if self.tcx.hir().span(*hir_id).lo() == span.lo() =>
{
if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(*hir_id)
&& let hir::ExprKind::Call(base, _) = expr.kind
&& let hir::ExprKind::Path(hir::QPath::TypeRelative(ty, _)) = base.kind
&& ty.span == span
Comment on lines +1212 to +1215
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not following the logic: where in this condition are you checking that the expression is already borrowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add the check for the existing borrow one level up, but I also wanted to avoid suggesting &str::from("") when we have str::from("") as that won't help either.

Copy link
Member

@Nadrieril Nadrieril Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so the comment below could be saying something like "don't suggest borrowing when this wouldn't fix the problem (because the type is specified by a path instead of inferred)"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I understand my confusion: I had understood "Do not suggest borrowing when we already do so" as "Do not suggest borrowing when the expression is already borrowed"

Copy link
Member

@Nadrieril Nadrieril Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming back to this: I still don't understand what this branch is catching. Can you rephrase the comment to be more explicit about what the branch means plz?

{
// Do not suggest borrowing when we already do so. This would happen with
// `let _ = &str::from("");` where the expression corresponds to the `str`.
return false;
}
c
}
c if matches!(
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let _ = &str::from("value");
//~^ ERROR the trait bound `str: From<_>` is not satisfied
//~| ERROR the size for values of type `str` cannot be known at compilation time
}
26 changes: 26 additions & 0 deletions tests/ui/suggestions/dont-suggest-borrowing-existing-borrow.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0277]: the trait bound `str: From<_>` is not satisfied
--> $DIR/dont-suggest-borrowing-existing-borrow.rs:2:14
|
LL | let _ = &str::from("value");
| ^^^ the trait `From<_>` is not implemented for `str`
|
= help: the following other types implement trait `From<T>`:
`String` implements `From<&String>`
`String` implements `From<&mut str>`
`String` implements `From<&str>`
`String` implements `From<Box<str>>`
`String` implements `From<Cow<'_, str>>`
`String` implements `From<char>`

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/dont-suggest-borrowing-existing-borrow.rs:2:14
|
LL | let _ = &str::from("value");
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: the return type of a function must have a statically known size

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
Loading