`self.to_string().eq(other.to_string())` where `other: &Self` and `Self: Display` suggests removing `.to_string()` instead of taking reference
Open
Nobody has claimed this yet.
A-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
#[derive(Debug)]
struct Error;
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "Error")
}
}
impl std::error::Error for Error {}
// This error instead of suggesting adding an & in front of `other.to_string()`, suggests removing `.to_string()`:
// error[E0308]: mismatched types
// --> src/lib.rs:14:29
// |
// 14 | self.to_string().eq(other.to_string())
// | -- ^^^^^^^^^^^^^^^^^ expected `&_`, found `String`
// | |
// | arguments to this method are incorrect
// |
// = note: expected reference `&_`
// found struct `String`
// note: method defined here
// --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:255:8
// |
// 255 | fn eq(&self, other: &Rhs) -> bool;
// | ^^
// help: try removing the method call
// |
// 14 - self.to_string().eq(other.to_string())
// 14 + self.to_string().eq(other)
// |
//
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
self.to_string().eq(other.to_string())
}
}
// Changing to the error suggestion though:
// error[E0277]: can't compare `String` with `Error`
// --> src/lib.rs:42:29
// |
// 42 | self.to_string().eq(other)
// | -- ^^^^^ no implementation for `String == Error`
// | |
// | required by a bound introduced by this call
// |
// = help: the trait `PartialEq<Error>` is not implemented for `String`
// = help: the following other types implement trait `PartialEq<Rhs>`:
// <String as PartialEq<&'a str>>
// <String as PartialEq<Cow<'a, str>>>
// <String as PartialEq<str>>
// <String as PartialEq>
//
//impl PartialEq for Error {
// fn eq(&self, other: &Self) -> bool {
// self.to_string().eq(other)
// }
//}
Current output
error[E0308]: mismatched types
--> src/lib.rs:14:29
|
14 | self.to_string().eq(other.to_string())
| -- ^^^^^^^^^^^^^^^^^ expected `&_`, found `String`
| |
| arguments to this method are incorrect
|
= note: expected reference `&_`
found struct `String`
note: method defined here
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:255:8
|
255 | fn eq(&self, other: &Rhs) -> bool;
| ^^
help: try removing the method call
|
14 - self.to_string().eq(other.to_string())
14 + self.to_string().eq(other)
|
Desired output
error[E0308]: mismatched types
--> src/lib.rs:14:29
|
14 | self.to_string().eq(other.to_string())
| -- ^^^^^^^^^^^^^^^^^ expected `&_`, found `String`
| |
| arguments to this method are incorrect
|
= note: expected reference `&_`
found struct `String`
note: method defined here
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:255:8
|
255 | fn eq(&self, other: &Rhs) -> bool;
| ^^
help: try taking a reference
|
14 - self.to_string().eq(other.to_string())
14 + self.to_string().eq(&other.to_string())
|
Rationale and extra context
No response
Other cases
No response
Rust Version
From play.rust-lang.org:
Build using the Nightly version: 1.82.0-nightly (2024-08-05 e57f3090aec33cdbf660)
Anything else?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example from the issue in the Rust playground using the linked nightly version, then trace the compiler diagnostic logic that emits the “try removing the method call” suggestion for this mismatch. Done means the diagnostic suggests taking a reference to other.to_string() for the reported case without regressing the existing suggestion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100