Skip to content

Commit

Permalink
Introduce temporary file creation Refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Apr 30, 2024
1 parent 32778ed commit 879ba3a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

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. */
Expand Down Expand Up @@ -40,4 +42,34 @@ String after(Path path) throws IOException {
return Files.readString(path);
}
}

/** Prefer {@link File#createTempFile(String, String)} over more verbose alternatives. */
static final class FileCreateTempFile {
@BeforeTemplate
File before(String prefix, String suffix) throws IOException {
return File.createTempFile(prefix, suffix, null);
}

@AfterTemplate
File after(String prefix, String suffix) throws IOException {
return File.createTempFile(prefix, suffix);
}
}

/**
* Prefer {@link Files#createTempFile(String, String, FileAttribute[])} over {@link
* File#createTempFile(String, String)}, as the former creates files with more restrictive
* permissions.
*/
static final class FilesCreateTempFileToFile {
@BeforeTemplate
File before(String prefix, String suffix) throws IOException {
return File.createTempFile(prefix, suffix);
}

@AfterTemplate
File after(String prefix, String suffix) throws IOException {
return Files.createTempFile(prefix, suffix).toFile();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -14,4 +15,12 @@ String testFilesReadStringWithCharset() throws IOException {
String testFilesReadString() throws IOException {
return Files.readString(Paths.get("foo"), StandardCharsets.UTF_8);
}

File testFileCreateTempFile() throws IOException {
return File.createTempFile("foo", "bar", null);
}

File testFilesCreateTempFileToFile() throws IOException {
return File.createTempFile("foo", "bar");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -14,4 +15,12 @@ String testFilesReadStringWithCharset() throws IOException {
String testFilesReadString() throws IOException {
return Files.readString(Paths.get("foo"));
}

File testFileCreateTempFile() throws IOException {
return File.createTempFile("foo", "bar");
}

File testFilesCreateTempFileToFile() throws IOException {
return Files.createTempFile("foo", "bar").toFile();
}
}

0 comments on commit 879ba3a

Please sign in to comment.