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

Please lint on a.eq(b.into()) if a.eq(b) would work #135201

Open
joshtriplett opened this issue Jan 7, 2025 · 3 comments
Open

Please lint on a.eq(b.into()) if a.eq(b) would work #135201

joshtriplett opened this issue Jan 7, 2025 · 3 comments
Labels
A-inference Area: Type inference A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

In the course of investigating crater breakage, I discovered many instances of a pattern that reduces to this:

fn main() {
    let s = String::from("hello");
    assert!(s.eq("hello".into()));
}

This compiles, presumably because "hello".into() has only one possible impl that would work. It breaks if another From impl is brought into scope. For instance, cargo add bstr and add use bstr as _; to the top of that example, and it breaks.

Could we lint against this?

cc @estebank; this is in the same spirit as #129249 .

@joshtriplett joshtriplett added A-inference Area: Type inference A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Jan 7, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 7, 2025
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 7, 2025
@matthiaskrgr
Copy link
Member

@joshtriplett
Copy link
Member Author

@matthiaskrgr It does, but that's a clippy lint. I'm suggesting a rustc one, so that crates in the ecosystem are much more likely to encounter it and fix it.

That said, #129249 will probably cover this. @estebank, would your lint implementation catch this?

@estebank
Copy link
Contributor

estebank commented Jan 8, 2025

@joshtriplett I believe it could, I need to try and get it working again to confirm.

Edit: can confirm that it would be picked up by that lint:

error: this conversion is useless `&str` to `&str`
 --> bar.rs:3:18
  |
3 |     assert!(s.eq("hello".into()));
  |                  ^^^^^^^^^^^^^^
  |
  = note: `#[deny(self_type_conversion)]` on by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants