Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clippy_utils::{
SpanlessEq, get_expr_use_or_unification_node, get_parent_expr, is_lint_allowed, method_calls, peel_blocks, sym,
};
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, Node};
use rustc_lint::{LateContext, LateLintPass, LintContext};
Expand Down Expand Up @@ -410,6 +411,23 @@ impl<'tcx> LateLintPass<'tcx> for StrToString {
diag.span_suggestion(expr.span, "try", format!("{snippet}.to_owned()"), applicability);
},
);
} else if let ExprKind::Path(_) = expr.kind
&& let Some(parent) = get_parent_expr(cx, expr)
&& let ExprKind::Call(_, args) | ExprKind::MethodCall(_, _, args, _) = &parent.kind
&& args.iter().any(|a| a.hir_id == expr.hir_id)
&& let Res::Def(DefKind::AssocFn, def_id) = expr.res(cx)
&& cx.tcx.is_diagnostic_item(sym::to_string_method, def_id)
{
// Detected `ToString::to_string` passed as an argument (generic: any call or method call)
span_lint_and_sugg(
cx,
STR_TO_STRING,
expr.span,
"`ToString::to_string` used as `&str` to `String` converter",
"try",
"ToOwned::to_owned".to_string(),
Applicability::MachineApplicable,
);
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/ui/str_to_string.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,32 @@ fn issue16271(key: &[u8]) {
let _value = t!(str::from_utf8(key)).to_owned();
//~^ str_to_string
}

struct GenericWrapper<T>(T);

impl<T> GenericWrapper<T> {
fn mapper<U, F: FnOnce(T) -> U>(self, f: F) -> U {
f(self.0)
}
}

fn issue16511(x: Option<&str>) {
let _ = x.map(ToOwned::to_owned);
//~^ str_to_string

let _ = x.map(ToOwned::to_owned);
//~^ str_to_string

let _ = ["a", "b"].iter().map(ToOwned::to_owned);
//~^ str_to_string

fn mapper<F: Fn(&str) -> String>(f: F) -> String {
f("hello")
}
let _ = mapper(ToOwned::to_owned);
//~^ str_to_string

let w = GenericWrapper("hello");
let _ = w.mapper(ToOwned::to_owned);
//~^ str_to_string
}
29 changes: 29 additions & 0 deletions tests/ui/str_to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,32 @@ fn issue16271(key: &[u8]) {
let _value = t!(str::from_utf8(key)).to_string();
//~^ str_to_string
}

struct GenericWrapper<T>(T);

impl<T> GenericWrapper<T> {
fn mapper<U, F: FnOnce(T) -> U>(self, f: F) -> U {
f(self.0)
}
}

fn issue16511(x: Option<&str>) {
let _ = x.map(ToString::to_string);
//~^ str_to_string

let _ = x.map(str::to_string);
//~^ str_to_string

let _ = ["a", "b"].iter().map(ToString::to_string);
//~^ str_to_string

fn mapper<F: Fn(&str) -> String>(f: F) -> String {
f("hello")
}
let _ = mapper(ToString::to_string);
//~^ str_to_string

let w = GenericWrapper("hello");
let _ = w.mapper(ToString::to_string);
//~^ str_to_string
}
32 changes: 31 additions & 1 deletion tests/ui/str_to_string.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,35 @@ error: `to_string()` called on a `&str`
LL | let _value = t!(str::from_utf8(key)).to_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t!(str::from_utf8(key)).to_owned()`

error: aborting due to 3 previous errors
error: `ToString::to_string` used as `&str` to `String` converter
--> tests/ui/str_to_string.rs:35:19
|
LL | let _ = x.map(ToString::to_string);
| ^^^^^^^^^^^^^^^^^^^ help: try: `ToOwned::to_owned`

error: `ToString::to_string` used as `&str` to `String` converter
--> tests/ui/str_to_string.rs:38:19
|
LL | let _ = x.map(str::to_string);
| ^^^^^^^^^^^^^^ help: try: `ToOwned::to_owned`

error: `ToString::to_string` used as `&str` to `String` converter
--> tests/ui/str_to_string.rs:41:35
|
LL | let _ = ["a", "b"].iter().map(ToString::to_string);
| ^^^^^^^^^^^^^^^^^^^ help: try: `ToOwned::to_owned`

error: `ToString::to_string` used as `&str` to `String` converter
--> tests/ui/str_to_string.rs:47:20
|
LL | let _ = mapper(ToString::to_string);
| ^^^^^^^^^^^^^^^^^^^ help: try: `ToOwned::to_owned`

error: `ToString::to_string` used as `&str` to `String` converter
--> tests/ui/str_to_string.rs:51:22
|
LL | let _ = w.mapper(ToString::to_string);
| ^^^^^^^^^^^^^^^^^^^ help: try: `ToOwned::to_owned`

error: aborting due to 8 previous errors