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

Improvements to RUF15 #7618

Closed
hoxbro opened this issue Sep 23, 2023 · 2 comments · Fixed by #7848
Closed

Improvements to RUF15 #7618

hoxbro opened this issue Sep 23, 2023 · 2 comments · Fixed by #7848
Assignees
Labels
rule Implementing or modifying a lint rule

Comments

@hoxbro
Copy link
Contributor

hoxbro commented Sep 23, 2023

I have two (small) suggestions for improving unnecessary-iterable-allocation-for-first-element (RUF015)

  1. Don't add iter to built-in functions that are already iterable
    An example of this could be list(zip([0]))[0], which right now is converted to next(iter(zip([10]))), and my suggestion would make it not add iter so it would be next(zip([10]))

  2. Handle an unpacked list
    This could be something like this [*x][0], which should be handled like list(x)[0] and converts to next(iter(x)). I don't think this is used much in the wild, so it could be overkill to add it.

I have a first already started implementing this here. Though, as this is the first time I have written any Rust, it could likely be improved upon 😅

@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Sep 23, 2023
@charliermarsh
Copy link
Member

I'd like to support the former but would probably skip the latter -- or, at least, that could be a separate rule that converts [*x] to list(x), rather than part of RUF015.

@hoxbro
Copy link
Contributor Author

hoxbro commented Sep 23, 2023

With the latter my point was not to do a conversion of [*x] to list(x), but convert [*x][0] to next(iter(x)).

Can you assign me this?

charliermarsh pushed a commit that referenced this issue Oct 8, 2023
## Summary

Resolves #7618. 

The list of builtin iterator is not exhaustive.

## Test Plan

`cargo test`

``` python
a = [1, 2]

examples = [
    enumerate(a),
    filter(lambda x: x, a),
    map(int, a),
    reversed(a),
    zip(a),
    iter(a),
]

for example in examples:
    print(next(example))
```
konstin pushed a commit that referenced this issue Oct 11, 2023
## Summary

Resolves #7618. 

The list of builtin iterator is not exhaustive.

## Test Plan

`cargo test`

``` python
a = [1, 2]

examples = [
    enumerate(a),
    filter(lambda x: x, a),
    map(int, a),
    reversed(a),
    zip(a),
    iter(a),
]

for example in examples:
    print(next(example))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants