Skip to content

Add UnnecessarySubstring for regions parsed via Integer.parseInt. - #6000

Open
gaul wants to merge 1 commit into
google:masterfrom
gaul:unnecessary-substring
Open

Add UnnecessarySubstring for regions parsed via Integer.parseInt.#6000
gaul wants to merge 1 commit into
google:masterfrom
gaul:unnecessary-substring

Conversation

@gaul

@gaul gaul commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Integer.parseInt(String) and its siblings force callers to materialize the region they parse. Since JDK 9 each has a (CharSequence, int, int, int) overload that reads the region in place, so the copy is pure overhead.

Covers the four parses that have that overload: Integer.parseInt, Integer.parseUnsignedInt, Long.parseLong and Long.parseUnsignedLong. Double.parseDouble, Integer.valueOf and Integer.decode have none. The overload is resolved on the owning symbol rather than inferred from the source level, so --release 8 compilations report nothing.

s.substring(begin) needs s.length() in the replacement, which repeats the target, so that form is only rewritten when the target is an identifier, a literal, or a side-effect-free field or array access.

Run over sources jars for ~100 widely used Maven Central artifacts (54,690 files), this reports 100 occurrences in 18 of them, including commons-lang3, h2, hsqldb, lucene-core and jgit.

`Integer.parseInt(String)` and its siblings force callers to materialize the
region they parse. Since JDK 9 each has a `(CharSequence, int, int, int)`
overload that reads the region in place, so the copy is pure overhead.

Covers the four parses that have that overload: `Integer.parseInt`,
`Integer.parseUnsignedInt`, `Long.parseLong` and `Long.parseUnsignedLong`.
`Double.parseDouble`, `Integer.valueOf` and `Integer.decode` have none. The
overload is resolved on the owning symbol rather than inferred from the source
level, so `--release 8` compilations report nothing.

`s.substring(begin)` needs `s.length()` in the replacement, which repeats the
target, so that form is only rewritten when the target is an identifier, a
literal, or a side-effect-free field or array access.

Run over sources jars for ~100 widely used Maven Central artifacts (54,690
files), this reports 100 occurrences in 18 of them, including commons-lang3,
h2, hsqldb, lucene-core and jgit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant