diff --git a/src/new_without_default.rs b/src/new_without_default.rs index d341afb4d92..461d1f5bebd 100644 --- a/src/new_without_default.rs +++ b/src/new_without_default.rs @@ -51,6 +51,7 @@ impl LateLintPass for NewWithoutDefault { let self_ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(cx.tcx.map.get_parent(id))).ty; if_let_chain!{[ + self_ty.walk_shallow().next().is_none(), // implements_trait does not work with generics let Some(ret_ty) = return_ty(cx.tcx.node_id_to_type(id)), same_tys(cx, self_ty, ret_ty), let Some(default_trait_id) = get_trait_def_id(cx, &DEFAULT_TRAIT_PATH), diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2934f1c4fba..16174e434d4 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -270,6 +270,7 @@ pub fn implements_trait<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: ty::Ty<'tcx>, -> bool { cx.tcx.populate_implementations_for_trait_if_necessary(trait_id); + let ty = cx.tcx.erase_regions(&ty); let infcx = infer::new_infer_ctxt(cx.tcx, &cx.tcx.tables, None, ProjectionMode::Any); let obligation = traits::predicate_for_trait_def(cx.tcx, traits::ObligationCause::dummy(), diff --git a/tests/compile-fail/new_without_default.rs b/tests/compile-fail/new_without_default.rs index cc033043bc5..30015f6c9e8 100755 --- a/tests/compile-fail/new_without_default.rs +++ b/tests/compile-fail/new_without_default.rs @@ -32,13 +32,36 @@ impl Params { fn new(_: u32) -> Self { Params } } -struct Generics<'a, T> { - foo: &'a bool, +struct GenericsOk { bar: T, } -impl<'c, V> Generics<'c, V> { - fn new<'b>() -> Generics<'b, V> { unimplemented!() } //~ERROR: you should consider adding a `Default` implementation for +impl Default for GenericsOk { + fn default() -> Self { unimplemented!(); } +} + +impl<'c, V> GenericsOk { + fn new() -> GenericsOk { unimplemented!() } +} + +struct LtOk<'a> { + foo: &'a bool, +} + +impl<'b> Default for LtOk<'b> { + fn default() -> Self { unimplemented!(); } +} + +impl<'c> LtOk<'c> { + fn new() -> LtOk<'c> { unimplemented!() } +} + +struct LtKo<'a> { + foo: &'a bool, +} + +impl<'c> LtKo<'c> { + fn new() -> LtKo<'c> { unimplemented!() } //~ERROR: you should consider adding a `Default` implementation for } fn main() {}