When a method is called that returns data read from some data source, that data should be stored rather than thrown away. Any other course of
action is surely a bug.
This rule raises an issue when the return value of any of the following is ignored or merely null-checked: BufferedReader.readLine()
,
Reader.read()
, and these methods in any child classes.
Noncompliant Code Example
public void doSomethingWithFile(String fileName) {
BufferedReader buffReader = null;
try {
buffReader = new BufferedReader(new FileReader(fileName));
while (buffReader.readLine() != null) { // Noncompliant
// ...
}
} catch (IOException e) {
// ...
}
}
Compliant Solution
public void doSomethingWithFile(String fileName) {
BufferedReader buffReader = null;
try {
buffReader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = buffReader.readLine()) != null) {
// ...
}
} catch (IOException e) {
// ...
}
}