Skip to content

extend unnecessary_to_owned to cover vec.contains(&str.to_string()) - #17920

Open
llogiq wants to merge 1 commit into
rust-lang:mainfrom
llogiq:contains-redundant-clone
Open

llogiq wants to merge 1 commit into
rust-lang:mainfrom
llogiq:contains-redundant-clone

Conversation

@llogiq

@llogiq llogiq commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

I happened upon this pattern in a codebase I was working on, and was surprised it wasn't covered already. Using any Vec::<String>::contains or VecDeque::<String>::contains will require the argument to be of type &String, so people sometimes use &_.to_string() for the value, despite the fact that _.iter().any(|x| x == _) does the same linear scan and requires no additional allocation. A small thing, but we can still lint it.


changelog: extend redundant_clone to cover vec.contains(&str.to_string())

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Oct 25, 2026
@rustbot

rustbot commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 25, 2026
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Lintcheck changes for dd9dd13

Lint Added Removed Changed
clippy::unnecessary_to_owned 3 0 0

This comment will be updated if you push new changes

@llogiq

llogiq commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Cool, found one in cargo of all things.

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

Community review: LGTM, I have some comments, feel free to ignore. none of them seem very important

View changes since this review

Comment thread tests/ui/redundant_clone.rs Resolved
Comment thread tests/ui/redundant_clone.rs Resolved
Comment thread tests/ui/redundant_clone.stderr Resolved
@llogiq

llogiq commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @CommanderStorm for the review. I incorporated all suggestions and also extended the lint to also cover arrays and slices instead of just Vecs and VecDeques.

@CommanderStorm

CommanderStorm commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

I incorporated all suggestions

I think you forgot to push, right? 😅

@llogiq
llogiq force-pushed the contains-redundant-clone branch from 4c81456 to 80edf9a Compare October 27, 2026 18:07
@rustbot

rustbot commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Gri-ffin Gri-ffin left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the addition but the lint is in nursery so it would mostly be allow by default for most cases :/, isn't unnecessary_to_owned a better fit for this addition?

View changes since this review

@llogiq
llogiq force-pushed the contains-redundant-clone branch from 80edf9a to 6543d13 Compare October 31, 2026 12:08
@llogiq llogiq changed the title extend redundant_clone to cover vec.contains(&str.to_string()) extend unnecessary_to_owned to cover vec.contains(&str.to_string()) Oct 31, 2026

@Gri-ffin Gri-ffin left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rustbot rustbot removed the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Oct 31, 2026
@rustbot

rustbot commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

r? @dswij

rustbot has assigned @dswij for the project review.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 9 candidates
  • 9 candidates expanded to 9 candidates
  • Random selection from Jarcho, Manishearth, blyxyas, dswij

@llogiq
llogiq force-pushed the contains-redundant-clone branch from 6543d13 to 5d3451c Compare October 31, 2026 14:42
@llogiq

llogiq commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

I think there's still a problem if the collection type is inferred from the .contains(_) argument. I'll add a test and make sure it works.

@llogiq
llogiq force-pushed the contains-redundant-clone branch from 5d3451c to dd9dd13 Compare October 31, 2026 18:56
@llogiq

llogiq commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Ok, this should now be fixed. @dswij r?

@rust-lang rust-lang deleted a comment from rustbot Oct 31, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants