rust-lang / rust-lang/rust-clippy

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

Open
#14,018 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the function and method examples in the issue, then review existing Clippy lint conventions and tests for how new lints are structured. Define the qualifying conversion cases and exclusions before implementation; done means the lint reliably identifies the intended cases and has coverage for the examples and relevant edge cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.