]> git.lizzy.rs Git - rust.git/commitdiff
Ignore new-without-default lint when `new` method has generic types
authorscott-linder <scott.b.linder@wmich.edu>
Wed, 14 Jun 2017 16:50:19 +0000 (12:50 -0400)
committerscott-linder <scott.b.linder@wmich.edu>
Wed, 14 Jun 2017 16:58:22 +0000 (12:58 -0400)
There may be no sensible `Default` impl if the result of `new` depends
on a type parameter.

clippy_lints/src/new_without_default.rs
clippy_tests/examples/new_without_default.rs

index 34f467184f3b9ed4cd865bdba2eee9db28bc16a0..e5644b606f56b462905c167646c9a72b5062233a 100644 (file)
@@ -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)));
index d4999f84e5531a7316a0741335816021c04544ab..a10db135c5e45c223851c20bbd6e5190176dbd43 100644 (file)
@@ -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<T>() -> Self { IgnoreGenericNew } // the derived Default does not make sense here as the result depends on T
+}
+
 fn main() {}