Skip to content

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

Open
@emilk

Description

@emilk

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> {

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions