new_without_default, partialeq_ne_impl: Use span_lint_node

Fixes #2892, fixes #3199
This commit is contained in:
Shotaro Yamada 2018-12-11 15:06:41 +09:00 committed by Shotaro Yamada
parent e2608fc272
commit bcbbb4d09b
4 changed files with 35 additions and 8 deletions

View file

@ -17,7 +17,7 @@ use crate::rustc_errors::Applicability;
use crate::syntax::source_map::Span;
use crate::utils::paths;
use crate::utils::sugg::DiagnosticBuilderExt;
use crate::utils::{get_trait_def_id, implements_trait, return_ty, same_tys, span_lint_and_then};
use crate::utils::{get_trait_def_id, implements_trait, return_ty, same_tys, span_lint_node_and_then};
use if_chain::if_chain;
/// **What it does:** Checks for types with a `fn new() -> Self` method and no
@ -165,9 +165,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
}
if let Some(sp) = can_derive_default(self_ty, cx, default_trait_id) {
span_lint_and_then(
span_lint_node_and_then(
cx,
NEW_WITHOUT_DEFAULT_DERIVE,
id,
impl_item.span,
&format!(
"you should consider deriving a `Default` implementation for `{}`",
@ -183,9 +184,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
);
});
} else {
span_lint_and_then(
span_lint_node_and_then(
cx,
NEW_WITHOUT_DEFAULT,
id,
impl_item.span,
&format!(
"you should consider adding a `Default` implementation for `{}`",

View file

@ -10,7 +10,7 @@
use crate::rustc::hir::*;
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::utils::{is_automatically_derived, span_lint};
use crate::utils::{is_automatically_derived, span_lint_node};
use if_chain::if_chain;
/// **What it does:** Checks for manual re-implementations of `PartialEq::ne`.
@ -56,10 +56,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
then {
for impl_item in impl_items {
if impl_item.ident.name == "ne" {
span_lint(cx,
PARTIALEQ_NE_IMPL,
impl_item.span,
"re-implementing `PartialEq::ne` is unnecessary")
span_lint_node(
cx,
PARTIALEQ_NE_IMPL,
impl_item.id.node_id,
impl_item.span,
"re-implementing `PartialEq::ne` is unnecessary",
);
}
}
}

View file

@ -139,4 +139,18 @@ impl<'a, T: 'a> OptionRefWrapper<'a, T> {
}
}
pub struct Allow(Foo);
impl Allow {
#[allow(clippy::new_without_default)]
pub fn new() -> Self { unimplemented!() }
}
pub struct AllowDerive;
impl AllowDerive {
#[allow(clippy::new_without_default_derive)]
pub fn new() -> Self { unimplemented!() }
}
fn main() {}

View file

@ -20,4 +20,12 @@ impl PartialEq for Foo {
}
}
struct Bar;
impl PartialEq for Bar {
fn eq(&self, _: &Bar) -> bool { true }
#[allow(clippy::partialeq_ne_impl)]
fn ne(&self, _: &Bar) -> bool { false }
}
fn main() {}