arbitrary_source_item_ordering: add configurable trait impl item ordering modes#17343
Conversation
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @samueltardieu (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
f600dca to
6fadf62
Compare
|
No changes for 04dafef |
There was a problem hiding this comment.
This PR changes the behavior of the lint, which is rarely a good idea.
I think this should be controlled by an option. People might not want to depend on the order defined by the trait author, especially when it can change between versions without even changing the API. There is no guarantee that, even in the standard library, traits will internally keep the existing ordering of items.
This option could have three possible values: alphabetical (current behavior), trait_item_ordering (the behavior implemented by this PR), or alphabetical_or_trait_item_ordering to accept both the current behavior and the ordering of the trait items (the names may not be the best ones, they are examples).
|
Reminder, once the PR becomes ready for a review, use |
|
Thanks for the detailed feedback. I've implemented the three-option config system as suggested. Changes1. New configuration optionAdded In /// Represents the ordering requirement for associated items in trait impls.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TraitImplItemOrder {
Alphabetical,
TraitItemOrdering,
AlphabeticalOrTraitItemOrdering,
}In #[lints(arbitrary_source_item_ordering)]
trait_impl_item_order: TraitImplItemOrder = TraitImplItemOrder::Alphabetical,Usage in trait-impl-item-order = "alphabetical_or_trait_item_ordering"2. Lint logicAdded three helper functions in fn check_impl_alphabetical(&self, cx: &LateContext<'_>, trait_impl: &rustc_hir::Impl<'_>) { ... }
fn check_impl_trait_def_order(&self, cx: &LateContext<'_>, trait_impl: &rustc_hir::Impl<'_>) { ... }
fn check_impl_alphabetical_or_trait_def_order(&self, cx: &LateContext<'_>, trait_impl: &rustc_hir::Impl<'_>) { ... }The ItemKind::Impl(trait_impl) if self.enable_ordering_for_impl => match self.trait_impl_item_order {
TraitImplItemOrder::Alphabetical => {
self.check_impl_alphabetical(cx, trait_impl);
},
TraitImplItemOrder::TraitItemOrdering => {
self.check_impl_trait_def_order(cx, trait_impl);
},
TraitImplItemOrder::AlphabeticalOrTraitItemOrdering => {
self.check_impl_alphabetical_or_trait_def_order(cx, trait_impl);
},
},For impl blocks without a trait, the two new modes fall back to the alphabetical check. 3. Tests & documentation
Happy to rename the option or its values if you prefer different naming. @rustbot ready |
…modes Implement configuration-based approach with three options: - alphabetical (default) - trait_item_ordering - alphabetical_or_trait_item_ordering Fixes false positives for non-alphabetically-ordered trait definitions.
fce1254 to
b9a0911
Compare
arbitrary_source_item_ordering false positive on trait implsarbitrary_source_item_ordering false positive on trait impls
arbitrary_source_item_ordering false positive on trait implsarbitrary_source_item_ordering false positive on trait impls
|
@rustbot review Squash and rebased commits. |
|
This lint has been nominated for inclusion. |
There was a problem hiding this comment.
Could you please add more tests with the trait_item_ordering and alphabetical_or_trait_item_ordering options in case of a trait with default function implementations? I want to make sure that the user can skip some of the functions in the middle and not be warned about that, as it keeps the order consistent.
|
And please, update the PR title and description to acknowledge that this is no longer a fix but a new feature, as pointed out in the Zulip thread (as pointed out by @kpreid). |
arbitrary_source_item_ordering false positive on trait implsaf25acc to
b33283f
Compare
|
Thank you for reviewing. I've added test cases to make sure that Implemented test casesIn both mode test folders ( // Default trait impl ordering.
trait WithDefault {
fn a() {}
fn b() {}
fn c() {}
}
struct SkipMiddle;
impl WithDefault for SkipMiddle {
fn a() {}
fn c() {}
}Also added coverage for trait ConstTrait2 {
const A: bool = true;
const B: bool = false;
const C: bool = true;
fn method() {}
}
struct SkipImpl;
// Tests skipped `const` in trait.
impl ConstTrait2 for SkipImpl {
const A: bool = true;
const C: bool = true;
fn method() {}
}Both passing ( @rustbot ready |
…ait_item_ordering` and `alphabetical_or_trait_item_ordering
f8bd4c9 to
04dafef
Compare
|
@rustbot review Squash and rebased commits, renamed the title and PR description, ready for review. |
Add configuration options for trait impl item ordering
Implements three-mode configuration for
arbitrary_source_item_ordering:alphabetical(default, preserves current behavior)trait_item_ordering(follow trait definition order)alphabetical_or_trait_item_ordering(accept either ordering)This allows users to choose the ordering style that best fits their codebase.
Traits like
IntoIteratorwith non-alphabetical definition orders can now behandled gracefully without false positives.
The default behavior is unchanged, maintaining backward compatibility.
changelog: [
arbitrary_source_item_ordering]: add configurable trait impl item ordering modesCloses #17241