Skip to content

Commit 468c86e

Browse files
committed
Add unnecessary_to_owned lint
1 parent aa3648a commit 468c86e

11 files changed

+1087
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,6 +3210,7 @@ Released 2018-09-13
32103210
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
32113211
[`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports
32123212
[`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by
3213+
[`unnecessary_to_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
32133214
[`unnecessary_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
32143215
[`unnecessary_wraps`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_wraps
32153216
[`unneeded_field_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#unneeded_field_pattern

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
181181
LintId::of(methods::UNNECESSARY_FILTER_MAP),
182182
LintId::of(methods::UNNECESSARY_FOLD),
183183
LintId::of(methods::UNNECESSARY_LAZY_EVALUATIONS),
184+
LintId::of(methods::UNNECESSARY_TO_OWNED),
184185
LintId::of(methods::UNWRAP_OR_ELSE_DEFAULT),
185186
LintId::of(methods::USELESS_ASREF),
186187
LintId::of(methods::WRONG_SELF_CONVENTION),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ store.register_lints(&[
315315
methods::UNNECESSARY_FILTER_MAP,
316316
methods::UNNECESSARY_FOLD,
317317
methods::UNNECESSARY_LAZY_EVALUATIONS,
318+
methods::UNNECESSARY_TO_OWNED,
318319
methods::UNWRAP_OR_ELSE_DEFAULT,
319320
methods::UNWRAP_USED,
320321
methods::USELESS_ASREF,

clippy_lints/src/lib.register_perf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ store.register_group(true, "clippy::perf", Some("clippy_perf"), vec![
1717
LintId::of(methods::MANUAL_STR_REPEAT),
1818
LintId::of(methods::OR_FUN_CALL),
1919
LintId::of(methods::SINGLE_CHAR_PATTERN),
20+
LintId::of(methods::UNNECESSARY_TO_OWNED),
2021
LintId::of(misc::CMP_OWNED),
2122
LintId::of(mutex_atomic::MUTEX_ATOMIC),
2223
LintId::of(redundant_clone::REDUNDANT_CLONE),

clippy_lints/src/methods/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mod uninit_assumed_init;
5757
mod unnecessary_filter_map;
5858
mod unnecessary_fold;
5959
mod unnecessary_lazy_eval;
60+
mod unnecessary_to_owned;
6061
mod unwrap_or_else_default;
6162
mod unwrap_used;
6263
mod useless_asref;
@@ -1885,6 +1886,32 @@ declare_clippy_lint! {
18851886
"usages of `str::splitn` that can be replaced with `str::split`"
18861887
}
18871888

1889+
declare_clippy_lint! {
1890+
/// ### What it does
1891+
/// Checks for unnecessary calls to [`ToOwned::to_owned`](https://doc.rust-lang.org/std/borrow/trait.ToOwned.html#tymethod.to_owned)
1892+
/// and other `to_owned`-like functions.
1893+
///
1894+
/// ### Why is this bad?
1895+
/// The unnecessary calls result in unnecessary allocations.
1896+
///
1897+
/// ### Example
1898+
/// ```rust
1899+
/// let path = std::path::Path::new("x");
1900+
/// foo(&path.to_string_lossy().to_string());
1901+
/// fn foo(s: &str) {}
1902+
/// ```
1903+
/// Use instead:
1904+
/// ```rust
1905+
/// let path = std::path::Path::new("x");
1906+
/// foo(&path.to_string_lossy());
1907+
/// fn foo(s: &str) {}
1908+
/// ```
1909+
#[clippy::version = "1.58.0"]
1910+
pub UNNECESSARY_TO_OWNED,
1911+
perf,
1912+
"unnecessary calls to `to_owned`-like functions"
1913+
}
1914+
18881915
pub struct Methods {
18891916
avoid_breaking_exported_api: bool,
18901917
msrv: Option<RustcVersion>,
@@ -1964,7 +1991,8 @@ impl_lint_pass!(Methods => [
19641991
MANUAL_STR_REPEAT,
19651992
EXTEND_WITH_DRAIN,
19661993
MANUAL_SPLIT_ONCE,
1967-
NEEDLESS_SPLITN
1994+
NEEDLESS_SPLITN,
1995+
UNNECESSARY_TO_OWNED
19681996
]);
19691997

19701998
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2007,6 +2035,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
20072035
single_char_add_str::check(cx, expr, args);
20082036
into_iter_on_ref::check(cx, expr, *method_span, method_call.ident.name, args);
20092037
single_char_pattern::check(cx, expr, method_call.ident.name, args);
2038+
unnecessary_to_owned::check(cx, expr, method_call.ident.name, args);
20102039
},
20112040
hir::ExprKind::Binary(op, lhs, rhs) if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne => {
20122041
let mut info = BinaryExprInfo {

0 commit comments

Comments
 (0)