The result of a method of an immutable value is not used

tx0/java:ignored-resultdefaultwarningbug

What it finds

name.trim(); as a statement does nothing: strings, BigDecimal and BigInteger cannot be changed, and their methods return a new value instead, which is dropped here. The call was meant to be name = name.trim();.

Only receivers whose type is known to be one of these classes are reported, and only methods that compute a new value from it. The value of a rule of a switch expression, case A -> s.trim();, is used. A test may make such a call only to see it throw, so calls are left alone inside a try block that catches something, in a lambda passed to a method such as assertThrows, and in a method whose annotation expects an exception, as @Test(expected = ...).

From the rule's source, ignored_result.rs.

Example from the rule's tests
import java.math.BigDecimal;import java.math.BigInteger; class Prices {  String normalize(String name, BigDecimal price, BigInteger count, StringBuilder out) {    name.trim();tx0 reports: the result of trim is not used    name.replace('a', 'b');tx0 reports: the result of replace is not used    price.setScale(2);tx0 reports: the result of setScale is not used    count.add(BigInteger.ONE);tx0 reports: the result of add is not used    String.format("%s", name);tx0 reports: the result of format is not used    "literal".toUpperCase();tx0 reports: the result of toUpperCase is not used    out.append(name);    name = name.trim();    try {      price.divide(BigDecimal.ZERO);    } catch (ArithmeticException expected) {    }    var copy = name;    copy.strip();tx0 reports: the result of strip is not used    return switch (name.length()) {      case 0 -> name.trim();      default -> name;    };  }   @Test(expected = NullPointerException.class)  void throwsOnNull(String s) {    s.concat(null);  }   void throwsInLambda(String s) {    assertThrows(NullPointerException.class, () -> {      s.concat(null);    });  }   static void assertThrows(Class<?> type, Runnable body) {}   @interface Test {    Class<?> expected();  }}
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:ignored-result" = "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:ignored-result] reason="..."

tx0 explain tx0/java:ignored-result shows the same in the terminal. Found a false positive? Report it.