Skip to content

fix(map_clone): avoid suggestions after type-changing coercions - #17671

Merged
dswij merged 1 commit into
rust-lang:masterfrom
saberoueslati:fix/map-clone-explicit-clone
Sep 9, 2026
Merged

dswij merged 1 commit into
rust-lang:masterfrom
saberoueslati:fix/map-clone-explicit-clone

Conversation

@saberoueslati

@saberoueslati saberoueslati commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

map_clone incorrectly suggests .cloned() for calls such as:

input.iter().map(|s: &&String| String::clone(s))

Here, String::clone coerces &&String to &String and returns String, while .cloned() returns &String.

Only lint when argument adjustments preserve its type. This fixes the false positive while retaining valid Option and Result diagnostics. Tests cover String::clone and <String as Clone>::clone.

Fixes #17550

changelog: Fix [map_clone] false positive for clone calls using deref coercion

@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Sep 1, 2026
@rustbot

rustbot commented Sep 1, 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.

@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: Change LGTM

Could you test if this change also fixes this FP and add it to the description if it does?

View changes since this review

@saberoueslati

Copy link
Copy Markdown
Contributor Author

@CommanderStorm
I checked #10278. This change doesn't affect that case: its reproducer uses method-call syntax (s.clone()), while this PR only changes the function-call path (String::clone(s)).
I also don't think it is a false positive. The suggested .cloned() replacement compiles and still produces a Vec<String>. The compilation error in the issue comes from applying .clone() instead of the suggested .cloned(), as Alexendoo pointed out in the first comment. The equivalent case is already covered as an intended lint in tests/ui/map_clone.rs:14, and the test passes.

@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 Sep 3, 2026
@rustbot

rustbot commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

r? @llogiq

rustbot has assigned @llogiq 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 6 candidates

@dswij
dswij added this pull request to the merge queue Sep 9, 2026
Merged via the queue into rust-lang:master with commit 01078f8 Sep 9, 2026
11 of 13 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 9, 2026
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.

FP: map_clone suggests replacing an explicit Foo::clone(x) with cloned()

6 participants