Skip to content

Commit e0ab332

Browse files
authored
Merge pull request rust-lang#1409 from Manishearth/fx-new-default
Fix suggestion span on new_without_default (fixes rust-lang#1407)
2 parents 5d78485 + b8b54eb commit e0ab332

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

clippy_lints/src/new_without_default.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
117117
let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT),
118118
!implements_trait(cx, self_ty, default_trait_id, Vec::new())
119119
], {
120-
if can_derive_default(self_ty, cx, default_trait_id) {
120+
if let Some(sp) = can_derive_default(self_ty, cx, default_trait_id) {
121121
span_lint_and_then(cx,
122122
NEW_WITHOUT_DEFAULT_DERIVE, span,
123123
&format!("you should consider deriving a \
124124
`Default` implementation for `{}`",
125125
self_ty),
126126
|db| {
127-
db.suggest_item_with_attr(cx, span, "try this", "#[derive(Default)]");
127+
db.suggest_item_with_attr(cx, sp, "try this", "#[derive(Default)]");
128128
});
129129
} else {
130130
span_lint_and_then(cx,
@@ -151,17 +151,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
151151
}
152152
}
153153

154-
fn can_derive_default<'t, 'c>(ty: ty::Ty<'t>, cx: &LateContext<'c, 't>, default_trait_id: DefId) -> bool {
154+
fn can_derive_default<'t, 'c>(ty: ty::Ty<'t>, cx: &LateContext<'c, 't>, default_trait_id: DefId) -> Option<Span> {
155155
match ty.sty {
156156
ty::TyAdt(adt_def, substs) if adt_def.is_struct() => {
157157
for field in adt_def.all_fields() {
158158
let f_ty = field.ty(cx.tcx, substs);
159159
if !implements_trait(cx, f_ty, default_trait_id, Vec::new()) {
160-
return false;
160+
return None;
161161
}
162162
}
163-
true
163+
cx.tcx.map.span_if_local(adt_def.did)
164164
},
165-
_ => false,
165+
_ => None,
166166
}
167167
}

tests/compile-fail/new_without_default.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
#![deny(new_without_default, new_without_default_derive)]
66

77
pub struct Foo;
8+
//~^HELP try this
9+
//~^^SUGGESTION #[derive(Default)]
10+
//~^^SUGGESTION pub struct Foo
811

912
impl Foo {
1013
pub fn new() -> Foo { Foo }
1114
//~^ERROR: you should consider deriving a `Default` implementation for `Foo`
12-
//~|HELP try this
13-
//~^^^SUGGESTION #[derive(Default)]
14-
//~^^^SUGGESTION pub fn new
1515
}
1616

1717
pub struct Bar;
18+
//~^HELP try this
19+
//~^^SUGGESTION #[derive(Default)]
20+
//~^^SUGGESTION pub struct Bar
1821

1922
impl Bar {
2023
pub fn new() -> Self { Bar }
2124
//~^ERROR: you should consider deriving a `Default` implementation for `Bar`
22-
//~|HELP try this
23-
//~^^^SUGGESTION #[derive(Default)]
24-
//~^^^SUGGESTION pub fn new
2525
}
2626

2727
pub struct Ok;

0 commit comments

Comments
 (0)