From a433a9067386802147354413af5194f1e39ef9c9 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Wed, 7 Aug 2024 15:41:45 +0200 Subject: [PATCH] Introduce `FilesCreateTempFileToFile` Refaster rule (#1162) --- .../errorprone/refasterrules/FileRules.java | 23 +++++++++++++++++++ .../refasterrules/FileRulesTestInput.java | 7 ++++++ .../refasterrules/FileRulesTestOutput.java | 7 ++++++ 3 files changed, 37 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java index bc230e0ae5..3a45fe1e4b 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/FileRules.java @@ -2,12 +2,15 @@ import static java.nio.charset.StandardCharsets.UTF_8; +import com.google.errorprone.refaster.Refaster; import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.attribute.FileAttribute; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; /** Refaster rules related to expressions dealing with files. */ @@ -40,4 +43,24 @@ String after(Path path) throws IOException { return Files.readString(path); } } + + /** + * Prefer {@link Files#createTempFile(String, String, FileAttribute[])} over alternatives that + * create files with more liberal permissions. + */ + static final class FilesCreateTempFileToFile { + @BeforeTemplate + @SuppressWarnings("java:S5443" /* This violation will be rewritten. */) + File before(String prefix, String suffix) throws IOException { + return Refaster.anyOf( + File.createTempFile(prefix, suffix), File.createTempFile(prefix, suffix, null)); + } + + @AfterTemplate + @SuppressWarnings( + "java:S5443" /* On POSIX systems the file will only have user read-write permissions. */) + File after(String prefix, String suffix) throws IOException { + return Files.createTempFile(prefix, suffix).toFile(); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java index c33f3e679f..ea50c16e62 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestInput.java @@ -1,5 +1,7 @@ package tech.picnic.errorprone.refasterrules; +import com.google.common.collect.ImmutableSet; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -14,4 +16,9 @@ String testFilesReadStringWithCharset() throws IOException { String testFilesReadString() throws IOException { return Files.readString(Paths.get("foo"), StandardCharsets.UTF_8); } + + ImmutableSet testFilesCreateTempFileToFile() throws IOException { + return ImmutableSet.of( + File.createTempFile("foo", "bar"), File.createTempFile("baz", "qux", null)); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java index 5b516e31f9..3494efbcff 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/FileRulesTestOutput.java @@ -1,5 +1,7 @@ package tech.picnic.errorprone.refasterrules; +import com.google.common.collect.ImmutableSet; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -14,4 +16,9 @@ String testFilesReadStringWithCharset() throws IOException { String testFilesReadString() throws IOException { return Files.readString(Paths.get("foo")); } + + ImmutableSet testFilesCreateTempFileToFile() throws IOException { + return ImmutableSet.of( + Files.createTempFile("foo", "bar").toFile(), Files.createTempFile("baz", "qux").toFile()); + } }