Skip to content

FP: default_constructed_unit_structs #10755

Closed
@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Lint Name

default_constructed_unit_structs

Reproducer

I tried this code:

struct Sqlite {}

trait HasArguments<'q> {
    type Arguments;
}

impl<'q> HasArguments<'q> for Sqlite {
    type Arguments = std::marker::PhantomData<&'q ()>;
}

fn foo() {
    let _ = <Sqlite as HasArguments>::Arguments::default();
}

fn main() {}

I saw this happen:

warning: use of `default` to create a unit struct
  --> elided-lifetime-in-path-in-type-relative-expression.rs:12:48
   |
12 |     let _ = <Sqlite as HasArguments>::Arguments::default();
   |                                                ^^^^^^^^^^^ help: remove this call to `default`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_constructed_unit_structs
   = note: requested on the command line with `-W clippy::default-constructed-unit-structs`

warning: 2 warnings emitted

The suggestion does not compile

error[E0575]: expected method or associated constant, found associated type `HasArguments::Arguments`
  --> elided-lifetime-in-path-in-type-relative-expression.rs:12:13
   |
12 |     let _ = <Sqlite as HasArguments>::Arguments;
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: can't use a type alias as a constructor

error: aborting due to previous error

Version

rustc 1.71.0-nightly (6d140d59b 2023-05-06)
binary: rustc
commit-hash: 6d140d59bb957903b02ddb993450509d8d92b448
commit-date: 2023-05-06
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions