mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 06:28:42 +00:00
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.
This commit is contained in:
parent
7056018335
commit
1ce3cbf9c4
2 changed files with 12 additions and 0 deletions
|
@ -108,6 +108,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
|
|||
// 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)));
|
||||
|
|
|
@ -76,4 +76,11 @@ struct Const;
|
|||
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() {}
|
||||
|
|
Loading…
Add table
Reference in a new issue