rspec/rules/S4087/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

47 lines
1.0 KiB
Plaintext

== Why is this an issue?
Java 7's try-with-resources structure automatically handles closing the resources that the ``++try++`` itself opens. Thus, adding an explicit ``++close()++`` call is redundant and potentially confusing.
=== Noncompliant code example
[source,java]
----
try (PrintWriter writer = new PrintWriter(process.getOutputStream())) {
String contents = file.contents();
writer.write(new Gson().toJson(new MyObject(contents)));
writer.flush();
writer.close(); // Noncompliant
}
----
=== Compliant solution
[source,java]
----
try (PrintWriter writer = new PrintWriter(process.getOutputStream())) {
String contents = file.contents();
writer.write(new Gson().toJson(new MyObject(contents)));
writer.flush();
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this "close" call; closing the resource is handled automatically by the try-with-resources.
=== Highlighting
``++close()++``
endif::env-github,rspecator-view[]