Fix placement of new_without_default suggestion

This commit is contained in:
Oliver Schneider 2017-11-29 17:10:53 +01:00
parent 317e97bae7
commit d5b73c184b
No known key found for this signature in database
GPG Key ID: A69F8D225B3AD7D9
2 changed files with 11 additions and 17 deletions

View File

@ -98,23 +98,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
}
if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {
let name = impl_item.name;
let span = impl_item.span;
let id = impl_item.id;
let decl = &sig.decl;
if sig.constness == hir::Constness::Const {
// can't be implemented by default
return;
}
if !impl_item.generics
.ty_params
.is_empty()
{
if !impl_item.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) {
if sig.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)));
if_chain! {
@ -126,7 +121,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
span_lint_and_then(
cx,
NEW_WITHOUT_DEFAULT_DERIVE,
span,
impl_item.span,
&format!("you should consider deriving a `Default` implementation for `{}`", self_ty),
|db| {
db.suggest_item_with_attr(cx, sp, "try this", "#[derive(Default)]");
@ -135,12 +130,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
span_lint_and_then(
cx,
NEW_WITHOUT_DEFAULT,
span,
impl_item.span,
&format!("you should consider adding a `Default` implementation for `{}`", self_ty),
|db| {
db.suggest_prepend_item(
cx,
span,
item.span,
"try this",
&create_new_without_default_suggest_msg(self_ty),
);

View File

@ -29,11 +29,10 @@ error: you should consider adding a `Default` implementation for `LtKo<'c>`
= note: `-D new-without-default` implied by `-D warnings`
help: try this
|
64 | impl Default for LtKo<'c> {
65 | fn default() -> Self {
66 | Self::new()
67 | }
68 | }
69 |
...
63 | impl Default for LtKo<'c> {
64 | fn default() -> Self {
65 | Self::new()
66 | }
67 | }
|