Recommend *var for comparison when Deref<Target = str>
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
use std::boxed::Box;
use std::ops::Deref;
#[derive(Debug, Eq, PartialEq)]
pub enum CowStr<'a> {
/// An owned, immutable string.
Boxed(Box<str>),
/// A borrowed string.
Borrowed(&'a str),
}
impl<'a> Deref for CowStr<'a> {
type Target = str;
fn deref(&self) -> &str {
match self {
CowStr::Boxed(ref b) => b,
CowStr::Borrowed(b) => b,
}
}
}
fn main() {
let a = CowStr::Borrowed("foo");
if a == "foo" {
println!("OK");
}
}
Current output
error[E0308]: mismatched types
--> src/main.rs:25:13
|
25 | if a == "foo" {
| ^^^^^ expected `CowStr<'_>`, found `&str`
|
help: try wrapping the expression in `CowStr::Borrowed`
|
25 | if a == CowStr::Borrowed("foo") {
| +++++++++++++++++ +
For more information about this error, try `rustc --explain E0308`.
error: could not compile `testrust` (bin "testrust") due to 1 previous error
Desired output
error[E0277]: can't compare `CowStr<'_>` with `str`
--> src/main.rs:25:11
|
25 | if a == "foo" {
| ^^ no implementation for `CowStr<'_> == str`
|
= help: the trait `PartialEq<str>` is not implemented for `CowStr<'_>`
= help: both types can be dereferenced to `str`
help: consider dereferencing both sides of the expression
|
25 - if a == "foo" {
25 + if *a == *"foo" {
|
For more information about this error, try `rustc --explain E0277`.
error: could not compile `testrust` (bin "testrust") due to 1 previous error
Rationale and extra context
Deref both sides is easier and shorter. It should be recommended when both types deref to a type that implements PartialEq.
Other cases
`if &a == "foo"` already recommends deref on both sides, but not `a == "foo"`. However, assert_eq doesn't recommend deref in both cases.
Rust Version
rustc 1.82.0-nightly (41dd149fd 2024-08-11)
binary: rustc
commit-hash: 41dd149fd6a6a06795fc6b9f54cb49af2f61775f
commit-date: 2024-08-11
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 19.1.0
Anything else?
No response
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
Run the supplied Rust example on the noted nightly compiler and compare the current E0308 output with the desired E0277 diagnostic. Trace the compiler's comparison and dereference diagnostic handling, including the existing behavior for &a == "foo", and verify that both-dereference guidance appears when both types dereference to a type implementing PartialEq.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100