Skip to content

Commit 236c8c7

Browse files
committed
Auto merge of #13159 - Alexendoo:missing-trait-methods-stable-order, r=dswij
`missing_trait_methods`: lint methods in definition order Lintcheck for #13157 showed a bunch of changes for `missing_trait_methods` This is because `values_sorted` was sorting the entries by the key's [`DefPathHash`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/def_id/struct.DefPathHash.html), this is stable for a given compiler but can change across versions changelog: none
2 parents d20be39 + 10fb062 commit 236c8c7

File tree

3 files changed

+63
-27
lines changed

3 files changed

+63
-27
lines changed

clippy_lints/src/missing_trait_methods.rs

+18-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_lint_allowed;
33
use clippy_utils::macros::span_is_local;
4-
use rustc_hir::def_id::DefIdMap;
4+
use rustc_hir::def_id::DefIdSet;
55
use rustc_hir::{Impl, Item, ItemKind};
66
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_middle::ty::AssocItem;
87
use rustc_session::declare_lint_pass;
98

109
declare_clippy_lint! {
@@ -68,33 +67,26 @@ impl<'tcx> LateLintPass<'tcx> for MissingTraitMethods {
6867
}) = item.kind
6968
&& let Some(trait_id) = trait_ref.trait_def_id()
7069
{
71-
let mut provided: DefIdMap<&AssocItem> = cx
72-
.tcx
73-
.provided_trait_methods(trait_id)
74-
.map(|assoc| (assoc.def_id, assoc))
70+
let trait_item_ids: DefIdSet = items
71+
.iter()
72+
.filter_map(|impl_item| impl_item.trait_item_def_id)
7573
.collect();
7674

77-
for impl_item in *items {
78-
if let Some(def_id) = impl_item.trait_item_def_id {
79-
provided.remove(&def_id);
80-
}
75+
for assoc in cx
76+
.tcx
77+
.provided_trait_methods(trait_id)
78+
.filter(|assoc| !trait_item_ids.contains(&assoc.def_id))
79+
{
80+
span_lint_and_then(
81+
cx,
82+
MISSING_TRAIT_METHODS,
83+
cx.tcx.def_span(item.owner_id),
84+
format!("missing trait method provided by default: `{}`", assoc.name),
85+
|diag| {
86+
diag.span_help(cx.tcx.def_span(assoc.def_id), "implement the method");
87+
},
88+
);
8189
}
82-
83-
cx.tcx.with_stable_hashing_context(|hcx| {
84-
for assoc in provided.values_sorted(&hcx, true) {
85-
let source_map = cx.tcx.sess.source_map();
86-
span_lint_and_then(
87-
cx,
88-
MISSING_TRAIT_METHODS,
89-
source_map.guess_head_span(item.span),
90-
format!("missing trait method provided by default: `{}`", assoc.name),
91-
|diag| {
92-
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));
93-
diag.span_help(definition_span, "implement the method");
94-
},
95-
);
96-
}
97-
});
9890
}
9991
}
10092
}

tests/ui/missing_trait_methods.rs

+8
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,12 @@ impl B for Complete {
4949
}
5050
}
5151

52+
trait MissingMultiple {
53+
fn one() {}
54+
fn two() {}
55+
fn three() {}
56+
}
57+
58+
impl MissingMultiple for Partial {}
59+
5260
fn main() {}

tests/ui/missing_trait_methods.stderr

+37-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,41 @@ help: implement the method
2424
LL | fn b<'a, T: AsRef<[u8]>>(a: &'a T) -> &'a [u8] {
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

27-
error: aborting due to 2 previous errors
27+
error: missing trait method provided by default: `one`
28+
--> tests/ui/missing_trait_methods.rs:58:1
29+
|
30+
LL | impl MissingMultiple for Partial {}
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
help: implement the method
34+
--> tests/ui/missing_trait_methods.rs:53:5
35+
|
36+
LL | fn one() {}
37+
| ^^^^^^^^
38+
39+
error: missing trait method provided by default: `two`
40+
--> tests/ui/missing_trait_methods.rs:58:1
41+
|
42+
LL | impl MissingMultiple for Partial {}
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
|
45+
help: implement the method
46+
--> tests/ui/missing_trait_methods.rs:54:5
47+
|
48+
LL | fn two() {}
49+
| ^^^^^^^^
50+
51+
error: missing trait method provided by default: `three`
52+
--> tests/ui/missing_trait_methods.rs:58:1
53+
|
54+
LL | impl MissingMultiple for Partial {}
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
|
57+
help: implement the method
58+
--> tests/ui/missing_trait_methods.rs:55:5
59+
|
60+
LL | fn three() {}
61+
| ^^^^^^^^^^
62+
63+
error: aborting due to 5 previous errors
2864

0 commit comments

Comments
 (0)