Skip to content

Include async functions in the len_without_is_empty#10359

Merged
bors merged 1 commit into
rust-lang:masterfrom
mladedav:dm/private/is-empty
Mar 9, 2023
Merged

Include async functions in the len_without_is_empty#10359
bors merged 1 commit into
rust-lang:masterfrom
mladedav:dm/private/is-empty

Conversation

@mladedav

@mladedav mladedav commented Feb 16, 2023

Copy link
Copy Markdown
Contributor

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

rustbot commented Feb 16, 2023

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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

llogiq commented Feb 16, 2023

Copy link
Copy Markdown
Contributor

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

mladedav commented Feb 16, 2023

Copy link
Copy Markdown
Contributor Author

The tests are fixed now, hope everything looks fine

@rustbot llogiq

@mladedav

Copy link
Copy Markdown
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
Comment thread clippy_lints/src/len_zero.rs Outdated
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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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.

Comment thread clippy_lints/src/len_zero.rs Outdated
let item = hir_map.get_if_local(def_id);
let item = item.unwrap();

if let Node::Item(item) = item {

Copy link
Copy Markdown
Member

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.

@mladedav mladedav Feb 19, 2023

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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

bors commented Mar 5, 2023

Copy link
Copy Markdown
Contributor

☔ 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
Copy Markdown
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.

@llogiq llogiq left a comment

Copy link
Copy Markdown
Contributor

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
Co-authored-by: Akshay <nerdy@peppe.rs>
@mladedav mladedav force-pushed the dm/private/is-empty branch from 1335978 to 5369052 Compare March 7, 2023 21:04
@mladedav

mladedav commented Mar 7, 2023

Copy link
Copy Markdown
Contributor Author

r? @llogiq

@rustbot

rustbot commented Mar 7, 2023

Copy link
Copy Markdown
Collaborator

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

llogiq commented Mar 9, 2023

Copy link
Copy Markdown
Contributor

Thank you!

@bors r+

@bors

bors commented Mar 9, 2023

Copy link
Copy Markdown
Contributor

📌 Commit 5369052 has been approved by llogiq

It is now in the queue for this repository.

@bors

bors commented Mar 9, 2023

Copy link
Copy Markdown
Contributor

⌛ Testing commit 5369052 with merge 9074da0...

@bors

bors commented Mar 9, 2023

Copy link
Copy Markdown
Contributor

☀️ 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