From 1ce3cbf9c43040b63e01f67131b2d30c0ab204d3 Mon Sep 17 00:00:00 2001 From: scott-linder Date: Wed, 14 Jun 2017 12:50:19 -0400 Subject: [PATCH] Ignore new-without-default lint when `new` method has generic types There may be no sensible `Default` impl if the result of `new` depends on a type parameter. --- clippy_lints/src/new_without_default.rs | 5 +++++ clippy_tests/examples/new_without_default.rs | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/clippy_lints/src/new_without_default.rs b/clippy_lints/src/new_without_default.rs index 34f467184f3..e5644b606f5 100644 --- a/clippy_lints/src/new_without_default.rs +++ b/clippy_lints/src/new_without_default.rs @@ -108,6 +108,11 @@ fn check_fn( // can't be implemented by default return; } + if !sig.generics.ty_params.is_empty() { + // when the result of `new()` depends on a type parameter we should not require an + // impl of `Default` + return; + } if decl.inputs.is_empty() && name == "new" && cx.access_levels.is_reachable(id) { let self_ty = cx.tcx .type_of(cx.tcx.hir.local_def_id(cx.tcx.hir.get_parent(id))); diff --git a/clippy_tests/examples/new_without_default.rs b/clippy_tests/examples/new_without_default.rs index d4999f84e55..a10db135c5e 100644 --- a/clippy_tests/examples/new_without_default.rs +++ b/clippy_tests/examples/new_without_default.rs @@ -76,4 +76,11 @@ fn new() -> Private { unimplemented!() } // We don't lint private items impl Const { pub const fn new() -> Const { Const } // const fns can't be implemented via Default } + +pub struct IgnoreGenericNew; + +impl IgnoreGenericNew { + pub fn new() -> Self { IgnoreGenericNew } // the derived Default does not make sense here as the result depends on T +} + fn main() {}