== Why is this an issue?

Operations performed on a string with predictable outcomes should be avoided. For example:

* checking if a string contains itself
* comparing a string with itself
* matching a string against itself
* creating a substring from 0 to the end of the string
* creating a substring from the end of the string
* replacing a string with itself
* replacing a substring with the exact substring

== How to fix it

Avoid performing the operation that has a predictable outcome.

=== Code examples

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
String speech = "SonarQube is the best static code analysis tool."

String s1 = speech.substring(0); // Noncompliant - yields the whole string
String s2 = speech.substring(speech.length()); // Noncompliant - yields "";
String s3 = speech.substring(5, speech.length()); // Noncompliant - use the 1-arg version instead

if (speech.contains(speech)) { // Noncompliant - always true
    // ...
}
----

==== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
String speech = "SonarQube is the best static code analysis tool."

String s1 = speech;
String s2 = "";
String s3 = speech.substring(5);

// ...
----

== Resources

* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/String.html[Oracle Java SE - String]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Remove this substring call; it has predictable results.


'''
== Comments And Links
(visible only on this page)

=== is related to: S3039

=== on 10 Oct 2014, 14:29:35 Freddy Mallet wrote:
"Clumsy" tag @Ann ? :)

=== on 21 Oct 2014, 15:41:13 Nicolas Peru wrote:
 Shouldn't this be readability tagged ?

=== on 22 Oct 2014, 11:41:27 Ann Campbell wrote:
\[~nicolas.peru] for the most part we don't have tags that correspond to SQALE characteristics. 

=== on 3 Jul 2019, 15:14:28 Tibor Blenessy wrote:
``++String.contains++`` idea inspiration is from https://twitter.com/gmuecke/status/1140487680758833152[https://twitter.com/gmuecke/status/1140487680758833152  ]

Ideally, we would handle such cases with symbolic execution, but we can catch such trivial cases by simple pattern matching until we get there.

endif::env-github,rspecator-view[]
