Skip to content

[collection_is_never_read] misfires when using retain for iteration #11783

@Ciel-MC

Description

@Ciel-MC

Summary

I am doing some async code and put the message sending and clearing the dead channels together by using retain for iteration, which made clippy think that the collection was never read from, since retain is usually used for modifying only, see code snippet attached.

Lint Name

collection_is_never_read

Reproducer

I tried this code:

 tokio::spawn(async move { let mut users = Vec::<MessageSender>::new(); loop { select! { channel = channel_receiver.recv() => { users.push(channel.expect("The sender is kept as global state and lives as long as the application")); }, message = rx.recv() => { let message = message.expect("The sender is kept as global state and lives as long as the application"); users.retain(|user| user.send(message.clone()).is_ok()); } } } });

I saw this happen:

collection is never read 

I expected to see this happen:
Nothing!

Version

rustc 1.73.0 (cc66ad468 2023-10-03) binary: rustc commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33 commit-date: 2023-10-03 host: x86_64-unknown-linux-gnu release: 1.73.0 LLVM version: 17.0.2 

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveL-nurseryLint: Currently in the nursery group

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions