rust-lang/rust-clippy

Two suggestions for more DRY code

Open

#1,674 opened on Apr 14, 2017

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Rust (1,391 forks)batch import
A-lintL-styleT-middlegood first issue

Repository metrics

Stars
 (10,406 stars)
PR merge metrics
 (Avg merge 19d 22h) (113 merged PRs in 30d)

Description

Given this code:

struct Foo { cap: usize }
impl Foo {
    fn new() -> Foo { Foo { cap: 1 } }
    fn with_capacity(cap: usize) -> Foo {
        Foo { cap: cap }
    }
}
fn main() {}

In my opinion Clippy should suggest to replace four of those usages of "Foo" with "Self" and to remove one ": cap", making the code more DRY, the intended resulting code should be:

struct Foo { cap: usize }
impl Foo {
    fn new() -> Self { Self { cap: 1 } }
    fn with_capacity(cap: usize) -> Self {
        Self { cap }
    }
}
fn main() {}

Contributor guide