Skip to content

New lint: Returning unit type when expecting Ord #5080

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

Closed
Uriopass opened this issue Jan 22, 2020 · 6 comments · Fixed by #5737
Closed

New lint: Returning unit type when expecting Ord #5080

Uriopass opened this issue Jan 22, 2020 · 6 comments · Fixed by #5737
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. good first issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group

Comments

@Uriopass
Copy link

Uriopass commented Jan 22, 2020

I had a very weird bug today, I was trying to sort my list using a somewhat complicated key expression with sort_by_key, but no sorting was happening it was like getting a random order.

Turns out I had an extra semicolon at the end of my expression, making my key return () instead which implements Ord (always Equal).

Example:

vec.sort_by_key(|x| {
    some_function(some_expression(x));
                                  // ^---- extra semicolon here
});
@flip1995 flip1995 added L-correctness Lint: Belongs in the correctness lint group good first issue These issues are a good way to get started with Clippy A-lint Area: New lints labels Jan 23, 2020
@basil-cow
Copy link
Contributor

basil-cow commented Jan 23, 2020

Should extend this to all places that expect function Fn(...) -> K, K: Ord and that function is not a |...| ()?

@Uriopass
Copy link
Author

Yes, that's pretty much what my titles says I think.

@basil-cow
Copy link
Contributor

It's probably not a good first issue, you have to look at the bounds of the generic function and check if the last statement of the closure is an explicit (), I would say it's too confusing

@flip1995 flip1995 added the E-medium Call for participation: Medium difficulty level problem and requires some initial experience. label Jan 25, 2020
@flip1995
Copy link
Member

Yeah, to make it generally apply to Fn(...) -> K, K: Ord types might be a little bit harder. But only linting sort_by_key is just calling cx.tcx.expr_ty(last_expr_in_closure), and checking if it is ()-type.

rabisg0 added a commit to rabisg0/rust-clippy that referenced this issue Mar 21, 2020
This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes rust-lang#5080
rabisg0 added a commit to rabisg0/rust-clippy that referenced this issue Mar 21, 2020
This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes rust-lang#5080
rabisg0 added a commit to rabisg0/rust-clippy that referenced this issue Mar 21, 2020
This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes rust-lang#5080
rabisg0 added a commit to rabisg0/rust-clippy that referenced this issue Mar 21, 2020
This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes rust-lang#5080
rabisg0 added a commit to rabisg0/rust-clippy that referenced this issue Mar 30, 2020
This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes rust-lang#5080
rabisg0 added a commit to rabisg0/rust-clippy that referenced this issue Apr 1, 2020
This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes rust-lang#5080
@Aaron1011
Copy link
Member

Aaron1011 commented Apr 4, 2020

I think this could be generalized to any function that implicitly returns (). If we have code like this:

fn my_fn() -> SomeType { 
    ...
}

fn foo<T>() -> T where T: SomeBound() {
    ...
    my_fn();
}

If both SomeType and () are valid return types for foo (that is, they both satisfy all of the bounds), we could suggest making the intended value more explicit (return my_expr(); or return ()).

@rabisg0
Copy link
Contributor

rabisg0 commented Apr 6, 2020

From what I've seen return () doesn't remain in the hir so linting based on that might be difficult.

bors added a commit that referenced this issue Jul 13, 2020
Reprise: new lint: Unintentional return of unit from closures expecting Ord

This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes #5080

Reprise of #5348 where I addressed all the comments there
bors added a commit that referenced this issue Jul 14, 2020
Reprise: new lint: Unintentional return of unit from closures expecting Ord

This lint catches cases where the last statement of a closure expecting
an instance of Ord has a trailing semi-colon. It compiles since the
closure ends up return () which also implements Ord but causes
unexpected results in cases such as sort_by_key.

Fixes #5080

Reprise of #5348 where I addressed all the comments there

changelog: add lint [`unit_return_expecting_ord`]
@bors bors closed this as completed in 1267909 Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. good first issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group
Projects
None yet
5 participants