-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
A-lintArea: New lintsArea: New lints
Description
What it does
When calling a function with an impl Into<String> parameter with a &str derived from a String that is not used later, an unnecessary copy of the value will be made inside of the function. This could be avoided by passing the String directly.
The same applies (at least) to the following types:
Vec<T>and&[T]Box<T>andT
Advantage
With this lint unnecessary copies of heap-based value can be avoided.
Drawbacks
Code could be intentionally structured to only pass values by reference for consistency purposes. For example in macros.
Example
fn main() {
takes_into_string(&*format!("some long {}", "string"));
}
fn takes_into_string(s: impl Into<String>) {
println!("{}", s.into());
}Could be written as:
fn main() {
takes_into_string(format!("some long {}", "string"));
}
fn takes_into_string(s: impl Into<String>) {
println!("{}", s.into());
}Comparison with existing lints
Unlike clippy::needless-borrows-for-generic-args this would also find more sophisticated patterns (specifically &* instead of &). Alternatively this lint could be extended to support that.
Additional Context
No response
Metadata
Metadata
Assignees
Labels
A-lintArea: New lintsArea: New lints