67 lines
1.8 KiB
Plaintext
67 lines
1.8 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The `Reader.read()` and the `BufferedReader.readLine()` are used for reading data from a data source.
|
|
The return value of these methods is the data read from the data source, or `null` when the end of the data source is reached.
|
|
If the return value is ignored, the data read from the source is thrown away and may indicate a bug.
|
|
|
|
This rule raises an issue when the return values of `Reader.read()` and `BufferedReader.readLine()`
|
|
and their subclasses are ignored or merely null-checked.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
public void doSomethingWithFile(String fileName) {
|
|
try(BufferedReader buffReader = new BufferedReader(new FileReader(fileName))) {
|
|
while (buffReader.readLine() != null) { // Noncompliant
|
|
// ...
|
|
}
|
|
} catch (IOException e) {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
public void doSomethingWithFile(String fileName) {
|
|
try(BufferedReader buffReader = new BufferedReader(new FileReader(fileName))) {
|
|
String line = null;
|
|
while ((line = buffReader.readLine()) != null) {
|
|
// ...
|
|
}
|
|
} catch (IOException e) {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/io/Reader.html#read()[Oracle SDK 20 - Reader.read()]
|
|
|
|
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/io/BufferedReader.html#readLine()[Oracle SDK 20 - BufferedReader.readLine()]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use or store the value returned from "xxx" instead of throwing it away.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== is related to: S2674
|
|
|
|
=== relates to: S899
|
|
|
|
endif::env-github,rspecator-view[]
|