diff options
| author | bors <bors@rust-lang.org> | 2024-07-27 15:32:18 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-07-27 15:32:18 +0000 |
| commit | 236c8c782beb9306c91cbecd65ff985c356ddb7b (patch) | |
| tree | cc8968222da7c4ca49b975c571c169440bdd306e | |
| parent | d20be39c7f904ba4e648dce3f82e776a5421bb71 (diff) | |
| parent | 10fb062c48e6e14676a532ee5a677d8e34ba9a4a (diff) | |
| download | rust-236c8c782beb9306c91cbecd65ff985c356ddb7b.tar.gz rust-236c8c782beb9306c91cbecd65ff985c356ddb7b.zip | |
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
| -rw-r--r-- | clippy_lints/src/missing_trait_methods.rs | 44 | ||||
| -rw-r--r-- | tests/ui/missing_trait_methods.rs | 8 | ||||
| -rw-r--r-- | tests/ui/missing_trait_methods.stderr | 38 |
3 files changed, 63 insertions, 27 deletions
diff --git a/clippy_lints/src/missing_trait_methods.rs b/clippy_lints/src/missing_trait_methods.rs index 59c8c7f38e4..7ee746365d1 100644 --- a/clippy_lints/src/missing_trait_methods.rs +++ b/clippy_lints/src/missing_trait_methods.rs @@ -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 @@ impl<'tcx> LateLintPass<'tcx> for MissingTraitMethods { }) = 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"); - }, - ); - } - }); } } } diff --git a/tests/ui/missing_trait_methods.rs b/tests/ui/missing_trait_methods.rs index 1b09b717ff7..55b904bde59 100644 --- a/tests/ui/missing_trait_methods.rs +++ b/tests/ui/missing_trait_methods.rs @@ -49,4 +49,12 @@ impl B for Complete { } } +trait MissingMultiple { + fn one() {} + fn two() {} + fn three() {} +} + +impl MissingMultiple for Partial {} + fn main() {} diff --git a/tests/ui/missing_trait_methods.stderr b/tests/ui/missing_trait_methods.stderr index f5d5d4418b2..9c4968b022d 100644 --- a/tests/ui/missing_trait_methods.stderr +++ b/tests/ui/missing_trait_methods.stderr @@ -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 |
