Skip to content

Suggest impl FromStr instead of TryFrom<&str> #14522

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

Open
emilk opened this issue Apr 2, 2025 · 0 comments
Open

Suggest impl FromStr instead of TryFrom<&str> #14522

emilk opened this issue Apr 2, 2025 · 0 comments
Labels
A-lint Area: New lints

Comments

@emilk
Copy link

emilk commented Apr 2, 2025

What it does

Looks for impl TryFrom<&str> and suggest impl FromStr instead

Advantage

When parsing a string, FromStr is the idiomatic trait to implement, but that may not be immediately obvious to a Rust novice.

From the docs of TryFrom:

(TryFrom) is useful when you are doing a type conversion that may trivially succeed but may also need special handling

Parsing is something else completely, and for that trait FromStr is to be used.

See also @kangalio's comment on the inverse issue.

Drawbacks

If there is a lifetime on the type, then FromStr won't work, and so we should not make the suggestion in that cade.

Example

impl TryFrom<&str> for MyType {
    type Error = MyError;
    fn try_from(value: &strr) -> Result<Self, Self::Error> {

Could be written as:

impl FromStr MyType {
    type Err = MyError;
    fn from_str(value: &strr) -> Result<Self, Self::Err> {
@emilk emilk added the A-lint Area: New lints label Apr 2, 2025
emilk added a commit to rerun-io/rerun that referenced this issue Apr 2, 2025
This makes it more ergonomic to connect to redap.

Also: use more idiomatic `FromStr` instead of `TryFrom<&str>`. I've
opened a clippy lint about this:
rust-lang/rust-clippy#14522

Review each commit seperately.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant