2020-12-21 15:38:52 +01:00
When the return value of a function call contain the operation status code, this value should be tested to make sure the operation completed successfully.
2021-02-02 15:02:10 +01:00
2020-12-21 15:38:52 +01:00
This rule raises an issue when the return values of the following are ignored:
2021-01-27 13:42:22 +01:00
* ``++java.io.File++`` operations that return a status code (except ``++mkdirs++``)
* ``++Iterator.hasNext()++``
* ``++Enumeration.hasMoreElements()++``
* ``++Lock.tryLock()++``
* non-void ``++Condition.await*++`` methods
* ``++CountDownLatch.await(long, TimeUnit)++``
* ``++Semaphore.tryAcquire++``
* ``++BlockingQueue++``: ``++offer++``, ``++remove++``
2020-12-21 15:38:52 +01:00
== Noncompliant Code Example
----
public void doSomething(File file, Lock lock) {
file.delete(); // Noncompliant
// ...
lock.tryLock(); // Noncompliant
}
----
== Compliant Solution
----
public void doSomething(File file, Lock lock) {
if (!lock.tryLock()) {
// lock failed; take appropriate action
}
if (!file.delete()) {
// file delete failed; take appropriate action
}
}
----
== See
* https://wiki.sei.cmu.edu/confluence/x/xzdGBQ[CERT, EXP00-J.] - Do not ignore values returned by methods
* https://wiki.sei.cmu.edu/confluence/x/TTZGBQ[CERT, FIO02-J.] - Detect and handle file-related errors
* http://cwe.mitre.org/data/definitions/754[MITRE, CWE-754] - Improper Check for Unusual Exceptional Conditions