From b8b54eb0d87a2a24940940b8c93c853a62691a14 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 29 Dec 2016 11:19:32 -0800 Subject: [PATCH] Fix suggestion span on new_without_default (fixes #1407) --- clippy_lints/src/new_without_default.rs | 12 ++++++------ tests/compile-fail/new_without_default.rs | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/new_without_default.rs b/clippy_lints/src/new_without_default.rs index 9c7b132dd3d..de71586863a 100644 --- a/clippy_lints/src/new_without_default.rs +++ b/clippy_lints/src/new_without_default.rs @@ -117,14 +117,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault { let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT), !implements_trait(cx, self_ty, default_trait_id, Vec::new()) ], { - if can_derive_default(self_ty, cx, default_trait_id) { + if let Some(sp) = can_derive_default(self_ty, cx, default_trait_id) { span_lint_and_then(cx, NEW_WITHOUT_DEFAULT_DERIVE, span, &format!("you should consider deriving a \ `Default` implementation for `{}`", self_ty), |db| { - db.suggest_item_with_attr(cx, span, "try this", "#[derive(Default)]"); + db.suggest_item_with_attr(cx, sp, "try this", "#[derive(Default)]"); }); } else { span_lint_and_then(cx, @@ -151,17 +151,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault { } } -fn can_derive_default<'t, 'c>(ty: ty::Ty<'t>, cx: &LateContext<'c, 't>, default_trait_id: DefId) -> bool { +fn can_derive_default<'t, 'c>(ty: ty::Ty<'t>, cx: &LateContext<'c, 't>, default_trait_id: DefId) -> Option { match ty.sty { ty::TyAdt(adt_def, substs) if adt_def.is_struct() => { for field in adt_def.all_fields() { let f_ty = field.ty(cx.tcx, substs); if !implements_trait(cx, f_ty, default_trait_id, Vec::new()) { - return false; + return None; } } - true + cx.tcx.map.span_if_local(adt_def.did) }, - _ => false, + _ => None, } } diff --git a/tests/compile-fail/new_without_default.rs b/tests/compile-fail/new_without_default.rs index cad675275db..65f6805e121 100644 --- a/tests/compile-fail/new_without_default.rs +++ b/tests/compile-fail/new_without_default.rs @@ -5,23 +5,23 @@ #![deny(new_without_default, new_without_default_derive)] pub struct Foo; +//~^HELP try this +//~^^SUGGESTION #[derive(Default)] +//~^^SUGGESTION pub struct Foo impl Foo { pub fn new() -> Foo { Foo } //~^ERROR: you should consider deriving a `Default` implementation for `Foo` - //~|HELP try this - //~^^^SUGGESTION #[derive(Default)] - //~^^^SUGGESTION pub fn new } pub struct Bar; +//~^HELP try this +//~^^SUGGESTION #[derive(Default)] +//~^^SUGGESTION pub struct Bar impl Bar { pub fn new() -> Self { Bar } //~^ERROR: you should consider deriving a `Default` implementation for `Bar` - //~|HELP try this - //~^^^SUGGESTION #[derive(Default)] - //~^^^SUGGESTION pub fn new } pub struct Ok;