Add needless_conversion_for_trait
lint
#15451
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:rust-clippy/clippy_dev/src/new_lint.rs
Line 436 in 14dfc03
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.clippy_utils::ty
: Move some functions fromunnecessary_to_owned
andneedless_conversion_for_generic_args
(cc: @Jarcho) into theclippy_utils::ty
module.replace_types
: Emphasize thatreplace_types
cannot change a function's output type (see below).needless_conversion_for_trait
lint; getuitest
to pass: The PR's main commit.Relationship to
unnecessary_to_owned
Some of
unnecessary_to_owned
's functionality is subsumed by this lint. However,replace_types
, whichneedless_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 someunnecessary_to_owned
tests contain a function whose output type is changed by the lint's suggestion:rust-clippy/tests/ui/unnecessary_to_owned.rs
Line 460 in 14dfc03
rust-clippy/tests/ui/unnecessary_to_owned.rs
Line 604 in 14dfc03
Also,
unnecessary_to_owned
will not eliminate a call toToString::to_string
unless the receiver implementsDeref<Target = str>
orAsRef<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 flaggedVec::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 forDiagExt
:rust-clippy/clippy_utils/src/sugg.rs
Lines 702 to 715 in 14dfc03
needless_conversion_for_trait
reports thatmsg.to_string()
could be replaced withmsg
. However, making this change causes the borrow checker to thinkmsg
's reference flows into&mut self
. I haveallow
ed 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
lintSummary Notes
Managed by
@rustbot
—see help for details