modify str_to_string to be machine-applicable

This commit is contained in:
Jacher 2024-05-30 17:02:23 +00:00
parent e7efe4381a
commit 5d0fcfbf56
3 changed files with 18 additions and 8 deletions

View File

@ -399,13 +399,17 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
&& let ty::Ref(_, ty, ..) = ty.kind()
&& ty.is_str()
{
span_lint_and_help(
let mut applicability = Applicability::MachineApplicable;
let snippet = snippet_with_applicability(cx, self_arg.span, "..", &mut applicability);
span_lint_and_sugg(
cx,
STR_TO_STRING,
expr.span,
"`to_string()` called on a `&str`",
None,
"consider using `.to_owned()`",
"try",
format!("{snippet}.to_owned()"),
applicability,
);
}
}

View File

@ -0,0 +1,9 @@
#![warn(clippy::str_to_string)]
fn main() {
let hello = "hello world".to_owned();
//~^ ERROR: `to_string()` called on a `&str`
let msg = &hello[..];
msg.to_owned();
//~^ ERROR: `to_string()` called on a `&str`
}

View File

@ -2,9 +2,8 @@ error: `to_string()` called on a `&str`
--> tests/ui/str_to_string.rs:4:17
|
LL | let hello = "hello world".to_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"hello world".to_owned()`
|
= help: consider using `.to_owned()`
= note: `-D clippy::str-to-string` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::str_to_string)]`
@ -12,9 +11,7 @@ error: `to_string()` called on a `&str`
--> tests/ui/str_to_string.rs:7:5
|
LL | msg.to_string();
| ^^^^^^^^^^^^^^^
|
= help: consider using `.to_owned()`
| ^^^^^^^^^^^^^^^ help: try: `msg.to_owned()`
error: aborting due to 2 previous errors