Skip to content

Add arg_iter lint to recommend IntoIterator over Iterator for argumen… #1581

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

timsburk
Copy link

@timsburk timsburk commented Apr 9, 2025

This PR implements the lint mentioned in issue #598 that suggests using IntoIterator instead of Iterator for function parameter types when possible.

Implementation details

  • Added a new lint in examples/general/arg_iter that detects when a function parameter type requires the Iterator trait
  • The lint checks if the type parameter is used in other trait bounds (and avoids suggesting changes in those cases)
  • Added documentation explaining why using IntoIterator provides more flexibility
  • Implemented UI tests to verify the lint catches appropriate cases

Testing

  • Added test cases covering correct detection of Iterator bounds that could be changed
  • Added tests for cases where the lint should not trigger (when Iterator is needed for other bounds)
  • Verified the error messages provide helpful suggestions for fixing the issue

This allows functions to accept a wider range of types (like slices, arrays, vectors) without requiring callers to explicitly call .iter() or .into_iter().

@timsburk timsburk requested a review from smoelius as a code owner April 9, 2025 19:16
@CLAassistant
Copy link

CLAassistant commented Apr 9, 2025

CLA assistant check
All committers have signed the CLA.

@timsburk
Copy link
Author

timsburk commented Apr 9, 2025

Hello @smoelius, I have made a PR for issue mentioned in #598. Can you please review and give feedbacks?

@smoelius
Copy link
Collaborator

@timsburk Sorry for the delay. I have a little bit of a reviewing backlog. But I will try to get to this soon.

@smoelius
Copy link
Collaborator

@timsburk Sorry I haven't had a chance to review this. I am getting ready to travel, and I will get back early next week. I will make it a priority to review this then.

Copy link
Collaborator

@smoelius smoelius left a comment

Choose a reason for hiding this comment

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

@timsburk Thanks very much for your patience.

Let's please move this lint from general to supplementary.

As noted below, I think the current tests are not being run. I will need to review the lint's logic more carefully, but I would like to see the tests run first.

@@ -31,6 +32,10 @@ wrong_serialize_struct_arg = { path = "wrong_serialize_struct_arg", features = [

dylint_linting = { path = "../../utils/linting" }

[features]
# Feature to avoid duplicate lint registration for abs_home_path
avoid_duplicate_abs_home_path = []
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this for?

@@ -37,4 +37,60 @@ For each lint subdirectory:
rlib = ["dylint_linting/constituent"]
```


Copy link
Collaborator

Choose a reason for hiding this comment

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

This file will need to be updated, but it should be possible to do that automatically. (I'll follow up with instructions later during the review.)

I don't think these manual changes should be necessary.

Comment on lines +161 to +167
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert!(true);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like a mistake(?).

Also, I don't think the current tests are being run.

Please add something like the following to this file:

#[test]
fn ui() {
    dylint_testing::ui_test(env!("CARGO_PKG_NAME"), "ui");
}

Comment on lines +155 to +159
#[no_mangle]
pub fn register_lints(_sess: &rustc_session::Session, lint_store: &mut rustc_lint::LintStore) {
lint_store.register_lints(&[ARG_ITER]);
lint_store.register_late_pass(|_| Box::new(ArgIter));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#[no_mangle]
pub fn register_lints(_sess: &rustc_session::Session, lint_store: &mut rustc_lint::LintStore) {
lint_store.register_lints(&[ARG_ITER]);
lint_store.register_late_pass(|_| Box::new(ArgIter));
}

If you rewrite the lint to use declare_late_lint!, this register_lints function should be unnecessary.

Comment on lines +155 to +159
#[no_mangle]
pub fn register_lints(_sess: &rustc_session::Session, lint_store: &mut rustc_lint::LintStore) {
lint_store.register_lints(&[ARG_ITER]);
lint_store.register_late_pass(|_| Box::new(ArgIter));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#[no_mangle]
pub fn register_lints(_sess: &rustc_session::Session, lint_store: &mut rustc_lint::LintStore) {
lint_store.register_lints(&[ARG_ITER]);
lint_store.register_late_pass(|_| Box::new(ArgIter));
}

If you rewrite the lint to use declare_late_lint!, this register_lints function should be unnecessary.

@@ -0,0 +1,31 @@
// This should trigger our lint
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this file can be removed.

@@ -0,0 +1,6 @@
[package]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this file can be removed.

@@ -0,0 +1,31 @@
// This should trigger our lint
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this file can be removed.

use rustc_span::symbol::Symbol;
use rustc_span::def_id::LocalDefId;

declare_lint! {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use dylint_linting::declare_late_lint!.

You can find examples in the supplementary directory, e.g., here:

dylint_linting::declare_late_lint! {

Using dylint_linting::declare_late_lint! will make some of the other declarations in this file unnecessary, e.g., the declaration of the register_lints function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants