Skip to content

Commit

Permalink
Introduce FileRules Refaster rule collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Aug 26, 2023
1 parent 9c158f1 commit b4b1f8a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tech.picnic.errorprone.refasterrules;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

/** Refaster rules related to expressions dealing with files. */
@OnlineDocumentation
final class FileRules {
private FileRules() {}

/** Prefer {@link Files#readString(Path, Charset)} over more contrived alternatives. */
static final class FilesReadStringWithCharset {
@BeforeTemplate
String before(Path path, Charset charset) throws IOException {
return new String(Files.readAllBytes(path), charset);
}

@AfterTemplate
String after(Path path, Charset charset) throws IOException {
return Files.readString(path, charset);
}
}

/** Prefer {@link Files#readString(Path)} over more verbose alternatives. */
static final class FilesReadString {
@BeforeTemplate
String before(Path path) throws IOException {
return Files.readString(path, UTF_8);
}

@AfterTemplate
String after(Path path) throws IOException {
return Files.readString(path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class RefasterRulesTest {
ComparatorRules.class,
DoubleStreamRules.class,
EqualityRules.class,
FileRules.class,
ImmutableListRules.class,
ImmutableListMultimapRules.class,
ImmutableMapRules.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tech.picnic.errorprone.refasterrules;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class FileRulesTest implements RefasterRuleCollectionTestCase {
String testFilesReadStringWithCharset() throws IOException {
return new String(Files.readAllBytes(Paths.get("foo")), StandardCharsets.ISO_8859_1);
}

String testFilesReadString() throws IOException {
return Files.readString(Paths.get("foo"), StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tech.picnic.errorprone.refasterrules;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class FileRulesTest implements RefasterRuleCollectionTestCase {
String testFilesReadStringWithCharset() throws IOException {
return Files.readString(Paths.get("foo"), StandardCharsets.ISO_8859_1);
}

String testFilesReadString() throws IOException {
return Files.readString(Paths.get("foo"));
}
}

0 comments on commit b4b1f8a

Please sign in to comment.