rust-lang / rust-lang/rust-clippy

lint: Unnecessary intermediary conversion

Open
#12,698 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

This lints against using from twice using an itermediary type when there is an From implementation in one step

Advantage
  • Reduce the verbosity of the code
  • Make the intent clearer
Drawbacks

It might be possible that the 2 implementations do not have the same behaviour, but based on the documentation of From, this does not really fit the contract of the trait.

Example

With the context:

struct A;
struct B;
struct C;

impl From<B> for A {
    fn from(_: B) -> Self {
        Self
    }
}

impl From<C> for A {
    fn from(_: C) -> Self {
        Self
    }
}

impl From<C> for B {
    fn from(_: C) -> Self {
        Self
    }
}
let c: C = …;
let b: B = c.into();
let a: A = b.into();

Could be written as:

let c: C = …;
let a: A = c.into();

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 with the Rust examples in this issue and inspect existing Clippy lints for chained From or Into conversions to identify the relevant lint structure and test conventions. Done means the new lint reliably detects the shown two-step conversion when a direct From implementation exists, while avoiding cases outside that contract, with tests covering the behavior.

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.