[Java] S2639 raises no issue for String.replaceFirst()

The rule S2639 checks whether the first argument to String.replaceAll(regex, replacement) is inappropriate, that is either “.” or “|”; a common mistake is string.replaceAll(".", "");, removing all text.
However, for String.replaceFirst(regex, replacement) no issue is raised. I’d say that string.replaceFirst(".", "x"); intends to replace the first dot character and not the first character and the rule should also check replaceFirst().
There is a corresponding FindBugs rule (RE_POSSIBLE_UNINTENDED_PATTERN) which does check both.

3 Likes

Hi,

This makes a lot of sense, hence ticket created to extend the rule. https://jira.sonarsource.com/browse/SONARJAVA-3032

Thanks for your feedback !

1 Like