Skip to content

Consider side effects when rewriting iterator behaviors - #14490

Merged
Jarcho merged 3 commits into
rust-lang:masterfrom
profetia:issue14444
Apr 22, 2025
Merged

Consider side effects when rewriting iterator behaviors#14490
Jarcho merged 3 commits into
rust-lang:masterfrom
profetia:issue14444

Conversation

@profetia

Copy link
Copy Markdown
Member

Closes #9191
Closes #14444
Closes #8055

Adds a new helper to partly check for side effects by recursively checking if the iterator type contains closures with mutable captures.

changelog: [double_ended_iterator_last] fix FP when iter has side effects
changelog: [needless_collect] fix lint not consider side effects

@rustbot

rustbot commented Mar 28, 2025

Copy link
Copy Markdown
Collaborator

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 28, 2025

@Jarcho Jarcho 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.

I don't see why this needs to be limited to checking only closure captures. Any iterator that contains a mutable reference has the same issue.

I think any iterator type containing one of the following would need to not lint:

  • A mutable reference/pointer
  • A reference/pointer to a non-Freeze type
  • A PhantomData type containing any of the previous.

@profetia
profetia force-pushed the issue14444 branch 2 times, most recently from 92e35bf to 4a54775 Compare April 1, 2025 12:17
@profetia

profetia commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

Updated. Now these will also be covered.

@profetia
profetia requested a review from Jarcho April 1, 2025 12:20
@profetia

profetia commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

This CI failure does not seem to be my problem. Any ideas?

@samueltardieu

samueltardieu commented Apr 1, 2025

Copy link
Copy Markdown
Member

This CI failure does not seem to be my problem. Any ideas?

I've restarted it to see if it's intermittent (we'll have to fix it anyway) or not. For reference, here is the failing version.

Edit: this does not look like it is intermittent.

@profetia

profetia commented Apr 2, 2025

Copy link
Copy Markdown
Member Author

Thanks to #14514, the CI is working now.

@profetia

Copy link
Copy Markdown
Member Author

r? clippy

@rustbot rustbot assigned Manishearth and unassigned Jarcho Apr 17, 2025
@Manishearth

Copy link
Copy Markdown
Member

r? @samueltardieu

@rustbot

rustbot commented Apr 17, 2025

Copy link
Copy Markdown
Collaborator

samueltardieu is on vacation.

Please choose another assignee.

@Jarcho Jarcho 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.

Looks like I didn't actually post a review last time I looked at this. Sorry about the wait.

Comment thread clippy_utils/src/ty/mod.rs Outdated
@Manishearth

Copy link
Copy Markdown
Member

r? Jarcho

sending it back, then 😄

Comment thread clippy_utils/src/ty/mod.rs Outdated
Comment thread clippy_utils/src/ty/mod.rs Outdated
Comment thread clippy_utils/src/ty/mod.rs Outdated
@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 Apr 19, 2025
@Jarcho

Jarcho commented Apr 19, 2025

Copy link
Copy Markdown
Contributor

For the util name something like has_non_owning_mutable_access would be a more precise as to what it's checking for.

Comment thread clippy_lints/src/methods/double_ended_iterator_last.rs Outdated
Comment thread clippy_utils/src/ty/mod.rs Outdated
@profetia

Copy link
Copy Markdown
Member Author

I pushed a refactored version. The uitest passed but the compiler panicked in dogfood, can you maybe give me some help?

Comment thread clippy_utils/src/ty/mod.rs Outdated
@Jarcho

Jarcho commented Apr 19, 2025

Copy link
Copy Markdown
Contributor

You'll need to keep track of what types have been seen as you recurse. Right now walking a type which recurses via PhantomData will overflow the stack. e.g.:

struct Foo<T> {
    inner: T,
    marker: core::marker::PhantomData<Self>,
}
impl<T: Iterator> Iterator for Foo<T> {
    type Item = T::Item;
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

fn main() {
    Foo {
        inner: [].iter(),
        marker: core::marker::PhantomData,
    }.collect::<Vec<&i32>>().len();
}

@profetia
profetia force-pushed the issue14444 branch 3 times, most recently from a58ffda to 5ab23fa Compare April 19, 2025 18:16

@Jarcho Jarcho 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.

Just one last extension to handle any known projections and this should be good.

Comment thread clippy_utils/src/ty/mod.rs Outdated
Comment thread clippy_utils/src/ty/mod.rs Outdated
Comment thread clippy_utils/src/ty/mod.rs Outdated
@profetia
profetia force-pushed the issue14444 branch 2 times, most recently from 6fd1995 to a6c8868 Compare April 20, 2025 06:24
Comment thread clippy_utils/src/ty/mod.rs Outdated

@Jarcho Jarcho 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.

Thank you.

@Jarcho
Jarcho added this pull request to the merge queue Apr 22, 2025
Merged via the queue into rust-lang:master with commit c3fb102 Apr 22, 2025
@profetia
profetia deleted the issue14444 branch September 12, 2025 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

5 participants