Skip to content

New lint to ensure Default::default calls T::new, rather than vice versa. #12662

Open
@solarretrace

Description

@solarretrace

What it does

Detects cases where a parameterless new simply calls Default::default, and indicates that default should call new, rather than the opposite.

Advantage

new functions can potentially be const, while default cannot, and calling them in the opposite order can prohibit this.

Drawbacks

The lint is completely irrelevant if the new function is never intended to be const.

Example

pub struct Config;

impl Default for Config {
    fn default() -> Self { Self }
}

impl Config {
    pub fn new() -> Self {
        Self::default()
    }
}

Could be written as:

pub struct Config;

impl Default for Config {
    fn default() -> Self { Self::new() }
}

impl Config {
    pub fn new() -> Self {
        Self
    }
}

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