Skip to content

Commit 1ce3cbf

Browse files
author
scott-linder
committed
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.
1 parent 7056018 commit 1ce3cbf

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

clippy_lints/src/new_without_default.rs

+5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
108108
// can't be implemented by default
109109
return;
110110
}
111+
if !sig.generics.ty_params.is_empty() {
112+
// when the result of `new()` depends on a type parameter we should not require an
113+
// impl of `Default`
114+
return;
115+
}
111116
if decl.inputs.is_empty() && name == "new" && cx.access_levels.is_reachable(id) {
112117
let self_ty = cx.tcx
113118
.type_of(cx.tcx.hir.local_def_id(cx.tcx.hir.get_parent(id)));

clippy_tests/examples/new_without_default.rs

+7
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,11 @@ struct Const;
7676
impl Const {
7777
pub const fn new() -> Const { Const } // const fns can't be implemented via Default
7878
}
79+
80+
pub struct IgnoreGenericNew;
81+
82+
impl IgnoreGenericNew {
83+
pub fn new<T>() -> Self { IgnoreGenericNew } // the derived Default does not make sense here as the result depends on T
84+
}
85+
7986
fn main() {}

0 commit comments

Comments
 (0)