Skip to content

Commit bcbbb4d

Browse files
committed
new_without_default, partialeq_ne_impl: Use span_lint_node
Fixes #2892, fixes #3199
1 parent e2608fc commit bcbbb4d

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

clippy_lints/src/new_without_default.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::rustc_errors::Applicability;
1717
use crate::syntax::source_map::Span;
1818
use crate::utils::paths;
1919
use crate::utils::sugg::DiagnosticBuilderExt;
20-
use crate::utils::{get_trait_def_id, implements_trait, return_ty, same_tys, span_lint_and_then};
20+
use crate::utils::{get_trait_def_id, implements_trait, return_ty, same_tys, span_lint_node_and_then};
2121
use if_chain::if_chain;
2222

2323
/// **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 {
165165
}
166166

167167
if let Some(sp) = can_derive_default(self_ty, cx, default_trait_id) {
168-
span_lint_and_then(
168+
span_lint_node_and_then(
169169
cx,
170170
NEW_WITHOUT_DEFAULT_DERIVE,
171+
id,
171172
impl_item.span,
172173
&format!(
173174
"you should consider deriving a `Default` implementation for `{}`",
@@ -183,9 +184,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
183184
);
184185
});
185186
} else {
186-
span_lint_and_then(
187+
span_lint_node_and_then(
187188
cx,
188189
NEW_WITHOUT_DEFAULT,
190+
id,
189191
impl_item.span,
190192
&format!(
191193
"you should consider adding a `Default` implementation for `{}`",

clippy_lints/src/partialeq_ne_impl.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use crate::rustc::hir::*;
1111
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1212
use crate::rustc::{declare_tool_lint, lint_array};
13-
use crate::utils::{is_automatically_derived, span_lint};
13+
use crate::utils::{is_automatically_derived, span_lint_node};
1414
use if_chain::if_chain;
1515

1616
/// **What it does:** Checks for manual re-implementations of `PartialEq::ne`.
@@ -56,10 +56,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
5656
then {
5757
for impl_item in impl_items {
5858
if impl_item.ident.name == "ne" {
59-
span_lint(cx,
60-
PARTIALEQ_NE_IMPL,
61-
impl_item.span,
62-
"re-implementing `PartialEq::ne` is unnecessary")
59+
span_lint_node(
60+
cx,
61+
PARTIALEQ_NE_IMPL,
62+
impl_item.id.node_id,
63+
impl_item.span,
64+
"re-implementing `PartialEq::ne` is unnecessary",
65+
);
6366
}
6467
}
6568
}

tests/ui/new_without_default.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,18 @@ impl<'a, T: 'a> OptionRefWrapper<'a, T> {
139139
}
140140
}
141141

142+
pub struct Allow(Foo);
143+
144+
impl Allow {
145+
#[allow(clippy::new_without_default)]
146+
pub fn new() -> Self { unimplemented!() }
147+
}
148+
149+
pub struct AllowDerive;
150+
151+
impl AllowDerive {
152+
#[allow(clippy::new_without_default_derive)]
153+
pub fn new() -> Self { unimplemented!() }
154+
}
155+
142156
fn main() {}

tests/ui/partialeq_ne_impl.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ impl PartialEq for Foo {
2020
}
2121
}
2222

23+
struct Bar;
24+
25+
impl PartialEq for Bar {
26+
fn eq(&self, _: &Bar) -> bool { true }
27+
#[allow(clippy::partialeq_ne_impl)]
28+
fn ne(&self, _: &Bar) -> bool { false }
29+
}
30+
2331
fn main() {}

0 commit comments

Comments
 (0)