Merge pull request #1409 from Manishearth/fx-new-default

Fix suggestion span on new_without_default (fixes #1407)
This commit is contained in:
llogiq 2016-12-29 20:34:33 +01:00 committed by GitHub
commit e0ab332303
2 changed files with 12 additions and 12 deletions

View File

@ -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), let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT),
!implements_trait(cx, self_ty, default_trait_id, Vec::new()) !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, span_lint_and_then(cx,
NEW_WITHOUT_DEFAULT_DERIVE, span, NEW_WITHOUT_DEFAULT_DERIVE, span,
&format!("you should consider deriving a \ &format!("you should consider deriving a \
`Default` implementation for `{}`", `Default` implementation for `{}`",
self_ty), self_ty),
|db| { |db| {
db.suggest_item_with_attr(cx, span, "try this", "#[derive(Default)]"); db.suggest_item_with_attr(cx, sp, "try this", "#[derive(Default)]");
}); });
} else { } else {
span_lint_and_then(cx, 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<Span> {
match ty.sty { match ty.sty {
ty::TyAdt(adt_def, substs) if adt_def.is_struct() => { ty::TyAdt(adt_def, substs) if adt_def.is_struct() => {
for field in adt_def.all_fields() { for field in adt_def.all_fields() {
let f_ty = field.ty(cx.tcx, substs); let f_ty = field.ty(cx.tcx, substs);
if !implements_trait(cx, f_ty, default_trait_id, Vec::new()) { 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,
} }
} }

View File

@ -5,23 +5,23 @@
#![deny(new_without_default, new_without_default_derive)] #![deny(new_without_default, new_without_default_derive)]
pub struct Foo; pub struct Foo;
//~^HELP try this
//~^^SUGGESTION #[derive(Default)]
//~^^SUGGESTION pub struct Foo
impl Foo { impl Foo {
pub fn new() -> Foo { Foo } pub fn new() -> Foo { Foo }
//~^ERROR: you should consider deriving a `Default` implementation for `Foo` //~^ERROR: you should consider deriving a `Default` implementation for `Foo`
//~|HELP try this
//~^^^SUGGESTION #[derive(Default)]
//~^^^SUGGESTION pub fn new
} }
pub struct Bar; pub struct Bar;
//~^HELP try this
//~^^SUGGESTION #[derive(Default)]
//~^^SUGGESTION pub struct Bar
impl Bar { impl Bar {
pub fn new() -> Self { Bar } pub fn new() -> Self { Bar }
//~^ERROR: you should consider deriving a `Default` implementation for `Bar` //~^ERROR: you should consider deriving a `Default` implementation for `Bar`
//~|HELP try this
//~^^^SUGGESTION #[derive(Default)]
//~^^^SUGGESTION pub fn new
} }
pub struct Ok; pub struct Ok;