rspec/rules/S4042/java/rule.adoc

45 lines
1.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
When ``++java.io.File#delete++`` fails, this ``++boolean++`` method simply returns ``++false++`` with no indication of the cause. On the other hand, when ``++java.nio.file.Files#delete++`` fails, this ``++void++`` method returns one of a series of exception types to better indicate the cause of the failure. And since more information is generally better in a debugging situation, ``++java.nio.file.Files#delete++`` is the preferred option.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public void cleanUp(Path path) {
File file = new File(path);
if (!file.delete()) { // Noncompliant
//...
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public void cleanUp(Path path) throws NoSuchFileException, DirectoryNotEmptyException, IOException {
Files.delete(path);
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use "java.nio.file.Files#delete" here for better messages on error conditions.
=== Highlighting
Use of ``++File#delete++``
endif::env-github,rspecator-view[]