Return Value Ignored

ID

java.return_value_ignored

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability

Description

Reports method calls whose return value is discarded when the return value matters. Many immutable-API methods return a new object instead of mutating the receiver; ignoring the return value means the intended operation has no effect.

Rationale

String methods like replace(), trim(), and substring() return new strings — they do not modify the original. Similarly, File.delete() and File.mkdirs() return a boolean indicating success. Ignoring these return values is almost always a bug.

// Bad -- trim() returns a new string; the original is unchanged
String name = "  Alice  ";
name.trim();

Remediation

Assign the return value to a variable or use it directly.

// Good
String name = "  Alice  ";
name = name.trim();

References