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
This commit is contained in:
bors 2024-07-27 15:32:18 +00:00
commit 236c8c782b
3 changed files with 63 additions and 27 deletions

View File

@ -1,10 +1,9 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_lint_allowed;
use clippy_utils::macros::span_is_local;
use rustc_hir::def_id::DefIdMap;
use rustc_hir::def_id::DefIdSet;
use rustc_hir::{Impl, Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::AssocItem;
use rustc_session::declare_lint_pass;
declare_clippy_lint! {
@ -68,33 +67,26 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
}) = item.kind
&& let Some(trait_id) = trait_ref.trait_def_id()
{
let mut provided: DefIdMap<&AssocItem> = cx
.tcx
.provided_trait_methods(trait_id)
.map(|assoc| (assoc.def_id, assoc))
let trait_item_ids: DefIdSet = items
.iter()
.filter_map(|impl_item| impl_item.trait_item_def_id)
.collect();
for impl_item in *items {
if let Some(def_id) = impl_item.trait_item_def_id {
provided.remove(&def_id);
}
for assoc in cx
.tcx
.provided_trait_methods(trait_id)
.filter(|assoc| !trait_item_ids.contains(&assoc.def_id))
{
span_lint_and_then(
cx,
MISSING_TRAIT_METHODS,
cx.tcx.def_span(item.owner_id),
format!("missing trait method provided by default: `{}`", assoc.name),
|diag| {
diag.span_help(cx.tcx.def_span(assoc.def_id), "implement the method");
},
);
}
cx.tcx.with_stable_hashing_context(|hcx| {
for assoc in provided.values_sorted(&hcx, true) {
let source_map = cx.tcx.sess.source_map();
span_lint_and_then(
cx,
MISSING_TRAIT_METHODS,
source_map.guess_head_span(item.span),
format!("missing trait method provided by default: `{}`", assoc.name),
|diag| {
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));
diag.span_help(definition_span, "implement the method");
},
);
}
});
}
}
}

View File

@ -49,4 +49,12 @@ fn b<T: AsRef<[u8]>>(a: &T) -> &[u8] {
}
}
trait MissingMultiple {
fn one() {}
fn two() {}
fn three() {}
}
impl MissingMultiple for Partial {}
fn main() {}

View File

@ -24,5 +24,41 @@ help: implement the method
LL | fn b<'a, T: AsRef<[u8]>>(a: &'a T) -> &'a [u8] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
error: missing trait method provided by default: `one`
--> tests/ui/missing_trait_methods.rs:58:1
|
LL | impl MissingMultiple for Partial {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: implement the method
--> tests/ui/missing_trait_methods.rs:53:5
|
LL | fn one() {}
| ^^^^^^^^
error: missing trait method provided by default: `two`
--> tests/ui/missing_trait_methods.rs:58:1
|
LL | impl MissingMultiple for Partial {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: implement the method
--> tests/ui/missing_trait_methods.rs:54:5
|
LL | fn two() {}
| ^^^^^^^^
error: missing trait method provided by default: `three`
--> tests/ui/missing_trait_methods.rs:58:1
|
LL | impl MissingMultiple for Partial {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: implement the method
--> tests/ui/missing_trait_methods.rs:55:5
|
LL | fn three() {}
| ^^^^^^^^^^
error: aborting due to 5 previous errors