Repository: rust
Issue: `rustc` should suggest borrowing values, when a borrow satisfies the same trait bound as an owned value
Labels: A-diagnostics,T-compiler

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.

If you cannot produce a minimal reproduction case (something that would work in
isolation), please provide the steps or even link to a repository that causes
the problematic output to occur.
-->
[On the Rust community Discord server,](https://discord.com/channels/273534239310479360/273541522815713281/1025021617174216854) a user reported that functions like `std::fs::create_dir_all`, that take either `PathBuf`s or `&PathBuf`s thanks to their `T: AsRef<Path>` bound, do not make clear to the user that the function does not require an owned value.

It is obviously too late to change the signatures on the freestanding functions in `std::fs` to prevent users from encountering this issue, so we really need to improve the error generated by this issue, which does not mention borrowing the value as a solution.

---

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=83da9a037a3b29b8f28c341fbb725787

```rust
pub fn create_dir_and_return_path() -> std::path::PathBuf {
    let path = std::path::PathBuf::from("haha");
    std::fs::create_dir_all(path).unwrap();
    path
}
```

The current output is:

```
Compiling playground v0.0.1 (/playground)
error[[E0382]](https://doc.rust-lang.org/nightly/error-index.html#E0382): use of moved value: `path`
 --> src/lib.rs:4:5
  |
2 |     let path = std::path::PathBuf::from("haha");
  |         ---- move occurs because `path` has type `PathBuf`, which does not implement the `Copy` trait
3 |     std::fs::create_dir_all(path).unwrap();
  |                             ---- value moved here
4 |     path
  |     ^^^^ value used here after move

For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` due to previous error
```

<!-- The following is not always necessary. -->
Ideally the output should look like:

```
Compiling playground v0.0.1 (/playground)
error[[E0382]](https://doc.rust-lang.org/nightly/error-index.html#E0382): use of moved value: `path`
 --> src/lib.rs:4:5
  |
2 |     let path = std::path::PathBuf::from("haha");
  |         ---- move occurs because `path` has type `PathBuf`, which does not implement the `Copy` trait
3 |     std::fs::create_dir_all(path).unwrap();
  |                             ---- value moved here
4 |     path
  |     ^^^^ value used here after move
  |
  = help: consider borrowing `path` here:
  |
3 -    std::fs::create_dir_all(path).unwrap();
3 +    std::fs::create_dir_all(&path).unwrap();
  |
  = note: ...so that `path` can remain valid after function call


For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` due to previous error
```

<!--
If the problem is not self-explanatory, please provide a rationale for the
change.
-->

<!--
If dramatically different output is caused by small changes, consider also
adding them here.

If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions. The output might also be
different depending on the Edition.
-->