A character passed to a string builder's constructor sets its capacity, not its content

tx0/java:char-as-capacitydefaultwarningbug

What it finds

new StringBuilder('[') does not start the builder with [: there is no constructor that takes a char, so the character is widened to an int and taken as the initial capacity. The builder starts empty. new StringBuilder("[") starts it with the text.

From the rule's source, char_as_capacity.rs.

Example from the rule's tests
class Json {  String list(java.util.List<String> items) {    StringBuilder sb = new StringBuilder('[');tx0 reports: '[' is taken as the initial capacity    StringBuilder ok = new StringBuilder("[");    StringBuilder sized = new StringBuilder(16);    StringBuffer old = new StringBuffer('x');tx0 reports: 'x' is taken as the initial capacity    return sb.toString() + ok + sized + old;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:char-as-capacity" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:char-as-capacity] reason="..."

tx0 explain tx0/java:char-as-capacity shows the same in the terminal. Found a false positive? Report it.