61 lines
1.3 KiB
Plaintext
61 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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
|
|
|
|
[source,java]
|
|
----
|
|
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
|
|
|
|
[source,java]
|
|
----
|
|
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) {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|