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

Diagnostics: suggest using from_str when calling parse on a type that implements FromStr #77843

Open
yoshuawuyts opened this issue Oct 12, 2020 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@yoshuawuyts
Copy link
Member

Problem

I find myself confusing the parse and from_str methods quite often. Take for example:

error[E0599]: no function or associated item named `parse` found for struct `connection_string::JdbcString` in the current scope
   --> src\connector\mssql.rs:363:55
    |
363 |         let mut jdbc = connection_string::JdbcString::parse(input)?;
    |                                                       ^^^^^ function or associated item not found in `connection_string::JdbcString`

parse doesn't exist on JdbcString; from_str does. So the way to fix it would be to write:

// either call `from_str`
let mut jdbc = connection_string::JdbcString::from_str(input)?;

// or flip it and call `parse` on the string
let mut jdbc: connection_string::JdbcString = input.parse()?;

Solution

It'd be great if the diagnostic could detect if parse was called on a type that implements FromStr and suggest to use from_str instead:

error[E0599]: no function or associated item named `parse` found for struct `connection_string::JdbcString` in the current scope
   --> src\connector\mssql.rs:363:55
    |
363 |         let mut jdbc = connection_string::JdbcString::parse(input)?;
    |                                                       ^^^^^ function or associated item not found in `connection_string::JdbcString`
    |                                                       |
    |                                                       help: did you mean to call `connection_string::JdbcString::from_str`?
@jyn514 jyn514 added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Oct 12, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Oct 12, 2020
@estebank
Copy link
Contributor

estebank commented May 1, 2024

I believe that this can be resolved after #120730 by annotating the connection_string::JdbcString::from_str method with #[doc(alias = "parse")] (but that won't work for methods or fields #124273).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` A-trait-system Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants