Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New lint [manual_partial_ord_and_ord_impl] #10788

Merged
merged 4 commits into from
Jul 8, 2023

Conversation

Centri3
Copy link
Member

@Centri3 Centri3 commented May 15, 2023

Lints when both PartialOrd and Ord are manually implemented yet the body of PartialOrd::partial_cmp isn't Some(self.cmp(<other>)).

This is in correctness currently but I'm ok with elsewhere.

Closes #10744


changelog: new lint [needless_partial_ord_impl]
#10788

@rustbot
Copy link
Collaborator

rustbot commented May 15, 2023

r? @llogiq

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 15, 2023
@bors
Copy link
Collaborator

bors commented May 23, 2023

☔ The latest upstream changes (presumably #10810) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch from f3b63ba to cbd63a5 Compare May 24, 2023 05:27
@xFrednet
Copy link
Member

xFrednet commented Jun 3, 2023

@blyxyas Would you mind reviewing this PR as well? It looks like a nice new lint!

r? @xFrednet

@rustbot rustbot assigned xFrednet and unassigned llogiq Jun 3, 2023
@blyxyas
Copy link
Member

blyxyas commented Jun 3, 2023

Yep, will also review it!

@blyxyas
Copy link
Member

blyxyas commented Jun 3, 2023

I feel like manual_partial_ord_and_ord_impl is pretty long and doesn't really convey anything. Maybe something like partial_ord_same_as_ord?

"change this to",
format!("{{ Some(self.cmp({})) }}", param_ident.name),
// TODO: This is always correct afaik.
Applicability::MaybeIncorrect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably use Applicability::Unspecified until we have some tool (likely a new implementation to lintcheck) to measure breaking changes when suggestions change in a bigger scope (instead of the 26 current crates, a bigger dataset).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the PartialOrd implementation has side effects? 🤔
In that case, PartialOrd cannot be replaced with Some(self.cmp(other))

(this is a very edgy edge-case)

impl Ord for A {
    fn cmp(&self, other: &Self) -> Ordering {
        todo!();
    }
}

impl PartialOrd for A {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        fn_with_side_effects();
	todo!();
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would break it, yes. Though I'm not sure if this is too big of an issue since implementations of PartialOrd and Ord must agree; I'm not sure if this extends to printing to stdout or mutating a static and whatnot but generally, this would be a pretty terrible idea anyway.

There's also derive_ord_xor_partial_ord, which is similar and disallows the implementation even if it has side effects afaict.

@blyxyas
Copy link
Member

blyxyas commented Jun 3, 2023

Sorry for taking so long to review, it looks like a very useful lint, even tho not a lot of people manually implement Ord and PartialOrd ^^

@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch 2 times, most recently from e4ac6ba to c10fc0f Compare June 5, 2023 09:48
@Centri3
Copy link
Member Author

Centri3 commented Jun 9, 2023

lintcheck reveals 4 instances of needless_partial_ord_impl and 1 instance of needless_clone_impl in the wild: 2 in the log crate, 2 in regex, and the one for needless_clone_impl from ryu. They seem correct, but the suggestion most certainly isn't. If somebody can take a closer look at these that'd be much appreciated :D

@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch from 1deaa44 to 7f43e0f Compare June 9, 2023 09:09
@blyxyas
Copy link
Member

blyxyas commented Jun 10, 2023

Could you split these two lints into two different PRs so they're easier to review and easier for generating the changelog? (and a simpler Git structure in general)

@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch 2 times, most recently from 0a7ec70 to cfcf04b Compare June 11, 2023 07:53
@xFrednet
Copy link
Member

Could you split these two lints into two different PRs so they're easier to review and easier for generating the changelog? (and a simpler Git structure in general)

From the newly created PRs, it looks like the lints were quite different. So asking this is reasonable. For further reference, you can just add multiple changelog entries to a PR and I'll combine them:

changelog: new lint XYZ
changelog: new lint ZZZ

works just fine. Also, a small warning, that at one point you might encounter contributors, who are not as proficient with git as you are. And in this regard, just to add even more to this totally side tracked comment: I can recommend playing a bit with aliases for git if you use the console. I have a few for fast rebasing of just fixing up the last commit. :)

@bors
Copy link
Collaborator

bors commented Jun 12, 2023

☔ The latest upstream changes (presumably #10921) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch 2 times, most recently from 2f48612 to f3875c7 Compare June 12, 2023 10:40
@bors
Copy link
Collaborator

bors commented Jun 12, 2023

☔ The latest upstream changes (presumably #10827) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch from 40227bb to 1af0124 Compare June 12, 2023 16:35
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small changes and I think it's pretty ready to be merged 👍 (The other or self comment has to be addressed tho, I actually think it's a bug)

clippy_lints/src/needless_impls.rs Outdated Show resolved Hide resolved
clippy_lints/src/needless_impls.rs Outdated Show resolved Hide resolved
tests/ui/needless_partial_ord_impl.stderr Outdated Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Jun 15, 2023

☔ The latest upstream changes (presumably #10934) made this pull request unmergeable. Please resolve the merge conflicts.

@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch from c5c1a77 to c515a30 Compare June 18, 2023 17:30
@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch from c515a30 to f2cccc2 Compare June 30, 2023 20:17
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is complete. Thanks! ❤️
cc @xFrednet

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, I have a few minor comments, and then it should be ready for bors consumption :D

clippy_lints/src/incorrect_impls.rs Outdated Show resolved Hide resolved
Comment on lines +34 to +39
impl PartialOrd for B {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just checked and self.cmp(other).into() also wraps the result into Some. If it's simple to implement, can you also check for this kind of body and accept it as a correct implementation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like handling all cases like that is a bit challenging, you could also do Some(Self::cmp(self, other)) I'm pretty sure which should be correct but isn't caught

tests/ui/incorrect_partial_ord_impl_on_ord_type.rs Outdated Show resolved Hide resolved
clippy_lints/src/incorrect_impls.rs Outdated Show resolved Hide resolved
@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch 2 times, most recently from 2a3354f to 8f302dc Compare July 6, 2023 03:09
@Centri3
Copy link
Member Author

Centri3 commented Jul 6, 2023

regex test failed, see #11111

@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch from 8f302dc to 862f82b Compare July 7, 2023 01:28
@bors
Copy link
Collaborator

bors commented Jul 7, 2023

☔ The latest upstream changes (presumably #11122) made this pull request unmergeable. Please resolve the merge conflicts.

@xFrednet
Copy link
Member

xFrednet commented Jul 7, 2023

This version looks good to me. Could you rebase on master and also squash the changes into 1-4 commits? Then everything should be ready. Here you can also r=blyxyas,xFrednet afterwards :)

Thank you for the update and review you two!


Bors, you know what to do :D

@bors
Copy link
Collaborator

bors commented Jul 7, 2023

✌️ @Centri3, you can now approve this pull request!

If @xFrednet told you to "r=me" after making some further change, please make that change, then do @bors r=@xFrednet

cargo dev fmt

cargo test passes

cargo test passes

refactor a lil

Update bool_comparison.stderr

heavily refactor + bump `clippy::version`

refactor

refactor

check bounds to increase accuracy, and add todos
@Centri3 Centri3 force-pushed the duplicate_manual_partial_ord_impl branch from 862f82b to a5dfb68 Compare July 8, 2023 18:12
@Centri3
Copy link
Member Author

Centri3 commented Jul 8, 2023

@bors r=xFrednet,blyxyas

@bors
Copy link
Collaborator

bors commented Jul 8, 2023

📌 Commit a5dfb68 has been approved by xFrednet,blyxyas

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jul 8, 2023

⌛ Testing commit a5dfb68 with merge e5db198...

@bors
Copy link
Collaborator

bors commented Jul 8, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet,blyxyas
Pushing e5db198 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint that encourages people to implement PartialOrd in terms of Ord when applicable
6 participants