rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Add `needless_conversion_for_trait` lint

Open smoelius opened this issue 5 months ago • 29 comments

Adds a lint to flag trait-behavior preserving calls where the receiver/argument could be used directly. For example, the lint flags the call to as_path in the following code, because &lint_file_path could be used instead: https://github.com/rust-lang/rust-clippy/blob/14dfc0359717c1576f6b7060614f9bbd331fb6a7/clippy_dev/src/new_lint.rs#L436

Organization

The PR is currently organized as five commits:

  • DisallowedPath -> ConfPath; REPLACEMENTS_ALLOWED -> REPLACEABLE: Shorten some configuration-related names for types used by this lint.
  • Move some functions into clippy_utils::ty: Move some functions from unnecessary_to_owned and needless_conversion_for_generic_args (cc: @Jarcho) into the clippy_utils::ty module.
  • Expand comment in replace_types: Emphasize that replace_types cannot change a function's output type (see below).
  • Add needless_conversion_for_trait lint; get uitest to pass: The PR's main commit.
  • Fix adjacent code: Fix Clippy code flagged by the lint.

Relationship to unnecessary_to_owned

Some of unnecessary_to_owned's functionality is subsumed by this lint. However, replace_types, which needless_conversion_for_trait uses, does not allow a function's output type to change. unnecessary_to_owned does not have this restriction. So, to ensure the two lints do not flag the same code, unnecessary_to_owned now requires that a function's output type contain the type to be replaced. Note that some unnecessary_to_owned tests contain a function whose output type is changed by the lint's suggestion:

  • https://github.com/rust-lang/rust-clippy/blob/14dfc0359717c1576f6b7060614f9bbd331fb6a7/tests/ui/unnecessary_to_owned.rs#L460
  • https://github.com/rust-lang/rust-clippy/blob/14dfc0359717c1576f6b7060614f9bbd331fb6a7/tests/ui/unnecessary_to_owned.rs#L604

Also, unnecessary_to_owned will not eliminate a call to ToString::to_string unless the receiver implements Deref<Target = str> or AsRef<str>. needless_conversion_for_trait incorporates this restriction as well.

check_inherent_functions

The lint includes a check_crate_post check that runs when Clippy is compiled in debug mode. The check looks for functions the lint should either warn about, or should explicitly ignore. For example, while preparing this PR, the check flagged Vec::into_chunks, which was added to the list of explicitly ignored functions.

False positive in clippy_utils

There are three false positives in clippy_utils that I do not know how best to resolve. They concern the implementations of the three functions for DiagExt: https://github.com/rust-lang/rust-clippy/blob/14dfc0359717c1576f6b7060614f9bbd331fb6a7/clippy_utils/src/sugg.rs#L702-L715

needless_conversion_for_trait reports that msg.to_string() could be replaced with msg. However, making this change causes the borrow checker to think msg's reference flows into &mut self. I have allowed the lint for the trait implementation, but I am open to suggestions for eliminating this class of false positives.

changelog: Add needless_conversion_for_trait lint

Summary Notes

Managed by @rustbot—see help for details

smoelius avatar Aug 10 '25 12:08 smoelius

r? @y21

rustbot has assigned @y21. 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 avatar Aug 10 '25 12:08 rustbot

Lintcheck changes for 33a368e26a4052fba4304dcc614c7e9c89541090

Lint Added Removed Changed
clippy::needless_conversion_for_trait 42 0 0
clippy::unnecessary_to_owned 0 2 0

This comment will be updated if you push new changes

github-actions[bot] avatar Aug 10 '25 12:08 github-actions[bot]

:umbrella: The latest upstream changes (possibly 73b5a59c224c189735aa628f85c874849e52cabf) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Aug 18 '25 15:08 rustbot

:umbrella: The latest upstream changes (possibly 368b2355797b13511776f0a35ec4864468b87ece) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Sep 08 '25 08:09 rustbot

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.

rustbot avatar Sep 08 '25 22:09 rustbot

:umbrella: The latest upstream changes (possibly 9f02cbc7a8170cac7b71e8df8735b1752c7020ec) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Sep 12 '25 17:09 rustbot

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.

rustbot avatar Sep 13 '25 11:09 rustbot

:umbrella: The latest upstream changes (possibly 0a7b3282af046f66769156b8a578e20d05384e98) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 04 '25 13:10 rustbot

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.

rustbot avatar Oct 04 '25 19:10 rustbot

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.

rustbot avatar Oct 04 '25 23:10 rustbot

:umbrella: The latest upstream changes (possibly #15823) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 06 '25 15:10 rustbot

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.

rustbot avatar Oct 07 '25 13:10 rustbot

:umbrella: The latest upstream changes (possibly 0a2eeceefcd99185643f7659ce25b6376749c271) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 07 '25 16:10 rustbot

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.

rustbot avatar Oct 08 '25 12:10 rustbot

:umbrella: The latest upstream changes (possibly ebbbbebf2dbb4df97193463d1de61a9737315ef0) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 11 '25 07:10 rustbot

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.

rustbot avatar Oct 11 '25 12:10 rustbot

:umbrella: The latest upstream changes (possibly a8d1258ba7e02ef72289ddf83e15df9929207ed3) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 13 '25 05:10 rustbot

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.

rustbot avatar Oct 13 '25 11:10 rustbot

:umbrella: The latest upstream changes (possibly d9fb15c4b1ebe9e7dc419e07f53af681d7860cbe) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 16 '25 14:10 rustbot

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.

rustbot avatar Oct 17 '25 19:10 rustbot

:umbrella: The latest upstream changes (possibly b0d93f123ab111dcb44c8b325ecd60d9230b8778) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 27 '25 13:10 rustbot

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.

rustbot avatar Oct 28 '25 23:10 rustbot

:umbrella: The latest upstream changes (possibly c936595d17413c1f08e162e117e504fb4ed126e4) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Oct 31 '25 18:10 rustbot

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.

rustbot avatar Nov 02 '25 23:11 rustbot

:umbrella: The latest upstream changes (possibly c48592eb49d984aee2f813336e2363798e004119) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Nov 14 '25 14:11 rustbot

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.

rustbot avatar Nov 14 '25 20:11 rustbot

:umbrella: The latest upstream changes (possibly a10cafebcdd63d95823ec8bff67a22a71a15a32d) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Dec 02 '25 19:12 rustbot

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.

rustbot avatar Dec 03 '25 13:12 rustbot

Hi, @y21. I realize I opened this PR during the feature freeze, but I think that has been over for a while now. Did you have any comments on this PR?

smoelius avatar Dec 03 '25 13:12 smoelius

:umbrella: The latest upstream changes (possibly d36794f5ec93c3dd14befc798e3e7c252fd116ec) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Dec 15 '25 12:12 rustbot