Skip to content

Add a lint to check for functions/methods that could be From #14018

Open
@FalkWoldmann

Description

@FalkWoldmann

What it does

Checks for functions and methods that could be a From impl (we could even do this for TryFrom)

Advantage

  • Makes the code more idiomatic

Drawbacks

No response

Example

struct StructA {
    some_field: String
}

struct StructB {
    some_field: String
}

fn map_to_struct_b(a: StructA) -> StructB {
    StructB {
        some_field: a.some_field
    }
}

impl StructA {
    fn to_struct_b(self) -> StructB {
        StructB {
            some_field: self.some_field
        }
    }
}

Could be written as:

impl From<StructA> for StructB {
    fn from(a: StructA) -> Self {
        StructB {
            some_field: a.some_field
        }
    }
}

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