rust-lang / rust-lang/rust-clippy

Implementing default with default - recursion

Open
#8,609 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

Hey, this is probably a pretty silly feature request, but this tripped me up way more than it should have.

Normally, if you try to implement Default and use ..Default::default(), it'll give you the following warning:
recursive call site [unconditional_recursion].

However, if you add any function call, this will no longer happen.

pub struct Test {
    pub a: String,
    pub b: i32,
}

impl Default for Test {
    fn default() -> Self {
        let test = "test".to_string();
        Self {
            a: test,
            ..Default::default() // No compiler warning!
        }
    }
}

In retrospect, this is exceptionally obvious. But my intuitive thought when I was writing that line was that the remaining fields would be implemented with Default, not that it would refer to the struct itself. Rust Analyzer even suggests using it... not that that's any excuse either, but I lost a lot of time to this particular footgun 😞

Related issue: https://github.com/rust-lang/rust-clippy/issues/103

Version

No response

Additional Labels

No response

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 by reviewing the Rust example and the related rust-clippy issue #103 to understand the requested recursion warning. The payload names no repository files or tests; done should include detecting this Default implementation pattern and emitting an appropriate warning without being bypassed by an unrelated function call.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.