Skip to content

Include async functions in the len_without_is_empty #10359

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

Merged
merged 1 commit into from
Mar 9, 2023

Conversation

mladedav
Copy link
Contributor

@mladedav mladedav commented Feb 16, 2023

fixes #7232

Changes done to the functionality:

Allowing different error types for the functions was disallowed. So the following was linted before but is not after this change

impl Foo {
    pub len(&self) -> Result<usize, Error1> { todo!(); }
    pub is_empty(&self) -> Result<bool, Error2> { todo!(); }
}

changelog: Enhancement: [len_without_is_empty]: Now also detects async functions
#10359

@rustbot
Copy link
Collaborator

rustbot commented Feb 16, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Manishearth (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 16, 2023
@mladedav
Copy link
Contributor Author

r? @llogiq

@rustbot rustbot assigned llogiq and unassigned Manishearth Feb 16, 2023
@mladedav mladedav force-pushed the dm/private/is-empty branch from 6f96437 to b83b963 Compare February 16, 2023 15:02
@mladedav mladedav marked this pull request as draft February 16, 2023 15:16
@mladedav mladedav force-pushed the dm/private/is-empty branch from b83b963 to 9d69b0b Compare February 16, 2023 17:11
@llogiq
Copy link
Contributor

llogiq commented Feb 16, 2023

fmt and dogfood are still failing; those should be easy to fix.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Feb 16, 2023
@mladedav mladedav force-pushed the dm/private/is-empty branch from 9d69b0b to 31b3bd1 Compare February 16, 2023 20:58
@mladedav mladedav marked this pull request as ready for review February 16, 2023 21:30
@mladedav
Copy link
Contributor Author

mladedav commented Feb 16, 2023

The tests are fixed now, hope everything looks fine

@rustbot llogiq

@mladedav
Copy link
Contributor Author

@rustbot label: +S-waiting-on-review, -S-waiting-on-author

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Feb 16, 2023
fn parse_len_output<'tcx>(cx: &LateContext<'_>, sig: FnSig<'tcx>) -> Option<LenOutput<'tcx>> {

fn extract_future_output<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<&'tcx PathSegment<'tcx>> {
if let ty::Alias(_, alias_ty) = ty.kind() {
Copy link
Contributor

Choose a reason for hiding this comment

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

You chould use the if_chain! macro here for shorter and clearer code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nowadays we can even use let chains.

return Some(LenOutput::Option(def_id));
}

return Some(LenOutput::Option(def_id));
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the use of the if is_first_generic_integral(segment) above if it returns the same thing anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, this is a bug. is_first_generic_integral checks that len() returns Option<T> where T is an integral type. This should ignore things like pub fn len() -> Option<String> because that's what happens for the sync version.

I've added a test for this.

let item = hir_map.get_if_local(def_id);
let item = item.unwrap();

if let Node::Item(item) = item {
Copy link
Contributor

Choose a reason for hiding this comment

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

or could use a unique if let … here since you are really matching deeper and deeper with one possible pattern.

Copy link
Contributor Author

@mladedav mladedav Feb 19, 2023

Choose a reason for hiding this comment

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

I rewrote this with an if let without nesting. However, it can't be a unique if let since at times I want to check some bounds before indexing into an array.

I have collapsed the chains as much as possible though. I personally don't like that much (e.g. I'd rather split

        let Some(Node::Item(Item { kind: ItemKind::OpaqueTy(opaque), .. })) = cx.tcx.hir().get_if_local(alias_ty.def_id) &&
        ...

into

        let Some(Node::Item(item)) = cx.tcx.hir().get_if_local(alias_ty.def_id) &&
        let Item { kind: ItemKind::OpaqueTy(opaque), .. } = item &&
        ...

but if you prefer it like this I have no issue with that.

edit, I had to split the example I provided to shorten the line under 120 characters anyway, but I would probably still cut some of the remaining ones.

@mladedav
Copy link
Contributor Author

@llogiq

Hi, do you think you will have some time for this PR? I'd like to finish this soI don't forget about it.

@bors
Copy link
Contributor

bors commented Mar 5, 2023

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

(Self::Option(_), Res::Def(_, def_id)) if cx.tcx.is_diagnostic_item(sym::Option, def_id) => true,
(Self::Result(_), Res::Def(_, def_id)) if cx.tcx.is_diagnostic_item(sym::Result, def_id) => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
(Self::Option(_), Res::Def(_, def_id)) if cx.tcx.is_diagnostic_item(sym::Option, def_id) => true,
(Self::Result(_), Res::Def(_, def_id)) if cx.tcx.is_diagnostic_item(sym::Result, def_id) => true,
(Self::Option(_), Res::Def(_, def_id)) => cx.tcx.is_diagnostic_item(sym::Option, def_id),
(Self::Result(_), Res::Def(_, def_id)) => cx.tcx.is_diagnostic_item(sym::Result, def_id),

I think we don't need guards here.

Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

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

r=me after rebase

@mladedav mladedav force-pushed the dm/private/is-empty branch from c68b745 to 1335978 Compare March 7, 2023 20:41
@mladedav mladedav force-pushed the dm/private/is-empty branch from 1335978 to 5369052 Compare March 7, 2023 21:04
@mladedav
Copy link
Contributor Author

mladedav commented Mar 7, 2023

r? @llogiq

@rustbot
Copy link
Collaborator

rustbot commented Mar 7, 2023

Could not assign reviewer from: llogiq.
User(s) llogiq are either the PR author or are already assigned, and there are no other candidates.
Use r? to specify someone else to assign.

@llogiq
Copy link
Contributor

llogiq commented Mar 9, 2023

Thank you!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 9, 2023

📌 Commit 5369052 has been approved by llogiq

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Mar 9, 2023

⌛ Testing commit 5369052 with merge 9074da0...

@bors
Copy link
Contributor

bors commented Mar 9, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing 9074da0 to master...

@bors bors merged commit 9074da0 into rust-lang:master Mar 9, 2023
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.

len_without_is_empty triggers for async len methods, but does not accept async is_empty methods
6 participants