34 lines
1.1 KiB
Plaintext
34 lines
1.1 KiB
Plaintext
![]() |
== Why is this an issue?
|
||
|
|
||
|
Calling the ``++to_string++`` method on a value that is already of type ``++String++`` in Rust is redundant and unnecessary. This redundant call does not change the value and only adds unnecessary complexity to the code. It can also be confusing to readers and may lead to misunderstandings about the code's intent.
|
||
|
|
||
|
If the intent was to clone the string, it is better to use the ``++clone++`` method instead.
|
||
|
|
||
|
Avoiding such redundant calls helps keep the code clean, concise, and more readable.
|
||
|
|
||
|
=== Noncompliant code example
|
||
|
|
||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||
|
----
|
||
|
let message = String::from("hello world");
|
||
|
println!("{}", message.to_string()); // Noncompliant: 'message' is already a String
|
||
|
----
|
||
|
|
||
|
=== Compliant solution
|
||
|
|
||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||
|
----
|
||
|
let message = String::from("hello world");
|
||
|
println!("{}", message);
|
||
|
|
||
|
// or
|
||
|
|
||
|
let cloned_message = message.clone(); // Use 'clone' if you need a duplicate
|
||
|
println!("{}", cloned_message);
|
||
|
----
|
||
|
|
||
|
== Resources
|
||
|
=== Documentation
|
||
|
|
||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#string_to_string
|