Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example attempts to delete a file but fails to check whether the operation has succeeded.:

Code Block
bgColor#FFcccc
public void deleteFile(){

  File someFile = new File("someFileName.txt");
  // doDo something with someFile
  someFile.delete();

}

...

This compliant solution checks the boolean Boolean value returned by the delete() method and handles any resulting errors.:

Code Block
bgColor#ccccff
public void deleteFile(){

  File someFile = new File("someFileName.txt");
  // doDo something with someFile
  if (!someFile.delete()) {
    // handleHandle failure to delete the file
  }

}

...

It is especially important to process the return values of immutable object methods. While Although many methods of mutable objects operate by changing some internal state of the object, methods of immutable objects cannot change the object and often return a mutated new object, leaving the original object unchanged.

...

This compliant solution correctly updates the String reference original with the return value from the String.replace() method.:

Code Block
bgColor#ccccff
public class Replace {
  public static void main(String[] args) {
    String original = "insecure";
    original = original.replace('i', '9');
    System.out.println(original);
  }
}

...

Ignoring method return values can lead to unexpected program behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXP00-J

medium

Medium

probable

Probable

medium

Medium

P8

L2

Automated Detection

Tool
Version
Checker
Description
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

JAVA.NULL.RET.UNCHECKED
JAVA.FUNCS.IRV

Call Might Return Null (Java)
Ignored Return Value (Java)

Coverity7.5CHECKED_RETURNImplemented
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.EXP00.NASSIG
CERT.EXP00.AECB
Ensure method and constructor return values are used
Avoid "try", "catch" and "finally" blocks with empty bodies
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V6010, V6101
SonarQube
Include Page
SonarQube_V
SonarQube_V

S2201

S899

Return values from functions without side effects should not be ignored

Return values should not be ignored when they contain the operation status code

SpotBugs

Include Page
SpotBugs_V
SpotBugs_V

RV_RETURN_VALUE_IGNORED
RV_RETURN_VALUE_IGNORED_INFERRED
RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT
RV_RETURN_VALUE_IGNORED_BAD_PRACTICE
RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED
RR_NOT_CHECKED
SR_NOT_CHECKED

Implemented

Related Guidelines

Bibliography

[API 2006]

method delete()

 


method replace()

[Green 2008]

String.replace

[Pugh 2009]

Misusing putIfAbsent

 


...

Image Added Image Added Image Added02. Expressions (EXP)      02. Expressions (EXP)      EXP01-J. Never dereference null pointers