Redundant String.substring(0)
ID |
java.string_substring_zero |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Best Practice |
Language |
Java |
Tags |
code-style |
Description
Reports calls to String.substring(0) — the one-argument overload with a start index of zero.
Rationale
String.substring(int beginIndex) returns the tail of the string starting at the given index. With beginIndex == 0 the returned value is the original string itself — the JDK short-circuits the call and simply returns this. The expression is therefore a no-op that adds a method frame and a reader-distracting step without changing behaviour. Deleting it makes the intent (or lack thereof) clear.
// Bad — no-op substring
String copy = s.substring(0);
// Good — use the string directly
String copy = s;
Remediation
Remove the .substring(0) call. If the original goal was to make an explicit copy for some downstream API, confirm that the copy is actually needed — String is immutable, so a copy is almost never required.