rspec/rules/S2677/java/rule.adoc

49 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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.
2021-04-28 16:49:39 +02:00
== 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) {
// ...
}
}
----
2021-04-28 16:49:39 +02:00
== 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) {
// ...
}
}
----
ifdef::env-github,rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]