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

Recommend IntoIterator over Iterator for argument types #598

Open
smoelius opened this issue Feb 3, 2023 · 1 comment
Open

Recommend IntoIterator over Iterator for argument types #598

smoelius opened this issue Feb 3, 2023 · 1 comment
Labels
good first issue Good for newcomers lint New lint or lint enhancement

Comments

@smoelius
Copy link
Collaborator

smoelius commented Feb 3, 2023

In most cases,* if a function requires an argument type to implement Iterator, the function could instead require the argument type to implement IntoIterator.

IntoIterator is implemented for types that implement Iterator. So the function would accept all the arguments it did before.

But, in addition, the function would accept things like slices, vectors, etc.

* The one exception I know of is when the Iterator bound is needed for another trait. Here is an example: https://github.com/trailofbits/necessist/blob/c17890cf085abbe1c376b75118c354c08f9a6d6f/core/src/core.rs#L406-L412

But for a first attempt, this lint could not fire whenever the type parameter appears in another trait bound.

@smoelius smoelius added the good first issue Good for newcomers label Feb 3, 2023
@smoelius smoelius added the lint New lint or lint enhancement label Dec 11, 2023
@smoelius
Copy link
Collaborator Author

Here are roughly the steps required to implement this lint.

This lint should probably be a general-purpose lint, and so would go in examples/general.

Suppose the lint is to be called arg_iter:

  • cd to examples/general.
  • Run the following command, which will create directory called arg_iter within the current directory:
    cargo dylint new arg_iter
  • Adjust the lint to use dylint_linting's constituent feature, as described in examples/general/README.md.
  • Add a check_fn method to the lint's LateLintPass implementation.
  • Using Clippy's unnecessary_to_owned lint as an example, write some code to collect the function's trait predicates.
  • If the function has an input whose type is a variable required only to implement the Iterator trait, emit a diagnostic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers lint New lint or lint enhancement
Projects
None yet
Development

No branches or pull requests

1 participant