]> source.dussan.org Git - jgit.git/commitdiff
[errorprone] Fix pattern see UnusedVariable 17/1194017/2
authorMatthias Sohn <matthias.sohn@sap.com>
Sun, 28 Apr 2024 22:11:46 +0000 (00:11 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 29 Apr 2024 13:05:32 +0000 (15:05 +0200)
See https://errorprone.info/bugpattern/UnusedVariable

Change-Id: I75d7602af31ed7d3264d2beab2d159cfbf29e7cb

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitAndLogCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashDropCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/CGitAttributesTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefUpdateTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergerTest.java
tools/BUILD

index aa65985c55c13b2edebdb02f5070e7534ad91e45..3b0e184a42b3a023940df32364c334beed1fb030 100644 (file)
@@ -13,6 +13,7 @@ package org.eclipse.jgit.api;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.eclipse.jgit.util.FileUtils.RECURSIVE;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeTrue;
@@ -607,14 +608,14 @@ public class AddCommandTest extends RepositoryTestCase {
                try (Git git = new Git(db)) {
                        DirCache dc = git.add().addFilepattern("a.txt").call();
 
-                       dc.getEntry(0).getObjectId();
+                       ObjectId oid = dc.getEntry(0).getObjectId();
 
                        try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
                                writer.print("other content");
                        }
 
                        dc = git.add().addFilepattern("a.txt").call();
-
+                       assertNotEquals(oid, dc.getEntry(0).getObjectId());
                        assertEquals(
                                        "[a.txt, mode:100644, content:other content]",
                                        indexState(CONTENT));
@@ -632,7 +633,7 @@ public class AddCommandTest extends RepositoryTestCase {
                try (Git git = new Git(db)) {
                        DirCache dc = git.add().addFilepattern("a.txt").call();
 
-                       dc.getEntry(0).getObjectId();
+                       ObjectId oid = dc.getEntry(0).getObjectId();
 
                        git.commit().setMessage("commit a.txt").call();
 
@@ -641,7 +642,7 @@ public class AddCommandTest extends RepositoryTestCase {
                        }
 
                        dc = git.add().addFilepattern("a.txt").call();
-
+                       assertNotEquals(oid, dc.getEntry(0).getObjectId());
                        assertEquals(
                                        "[a.txt, mode:100644, content:other content]",
                                        indexState(CONTENT));
@@ -659,12 +660,12 @@ public class AddCommandTest extends RepositoryTestCase {
                try (Git git = new Git(db)) {
                        DirCache dc = git.add().addFilepattern("a.txt").call();
 
-                       dc.getEntry(0).getObjectId();
+                       ObjectId oid = dc.getEntry(0).getObjectId();
                        FileUtils.delete(file);
 
                        // is supposed to do nothing
                        dc = git.add().addFilepattern("a.txt").call();
-
+                       assertEquals(oid, dc.getEntry(0).getObjectId());
                        assertEquals(
                                        "[a.txt, mode:100644, content:content]",
                                        indexState(CONTENT));
@@ -684,12 +685,12 @@ public class AddCommandTest extends RepositoryTestCase {
 
                        git.commit().setMessage("commit a.txt").call();
 
-                       dc.getEntry(0).getObjectId();
+                       ObjectId oid = dc.getEntry(0).getObjectId();
                        FileUtils.delete(file);
 
                        // is supposed to do nothing
                        dc = git.add().addFilepattern("a.txt").call();
-
+                       assertEquals(oid, dc.getEntry(0).getObjectId());
                        assertEquals(
                                        "[a.txt, mode:100644, content:content]",
                                        indexState(CONTENT));
index b7abba420911f6d2632803a4e6a6f8f5512675a0..57e5d4958fad264d550f7c18ded52e1361026846 100644 (file)
@@ -284,11 +284,10 @@ public class CommitAndLogCommandTest extends RepositoryTestCase {
                        // template)
                        chars = commit.getFullMessage().getBytes(UTF_8);
                        int lineStart = 0;
-                       int lineEnd = 0;
                        for (int i = 0; i < 4; i++) {
                                lineStart = RawParseUtils.nextLF(chars, lineStart);
                        }
-                       lineEnd = RawParseUtils.nextLF(chars, lineStart);
+                       int lineEnd = RawParseUtils.nextLF(chars, lineStart);
 
                        String line = RawParseUtils.decode(chars, lineStart, lineEnd);
 
@@ -303,13 +302,12 @@ public class CommitAndLogCommandTest extends RepositoryTestCase {
                        // we should find the untouched template
                        chars = commit.getFullMessage().getBytes(UTF_8);
                        lineStart = 0;
-                       lineEnd = 0;
                        for (int i = 0; i < 4; i++) {
                                lineStart = RawParseUtils.nextLF(chars, lineStart);
                        }
                        lineEnd = RawParseUtils.nextLF(chars, lineStart);
 
-                       line = RawParseUtils.decode(chars, lineStart, lineEnd);
+                       RawParseUtils.decode(chars, lineStart, lineEnd);
 
                        assertTrue(commit.getFullMessage()
                                        .contains("Change-Id: I" + ObjectId.zeroId().getName()));
index f52b715d39ffc39907f8758ba2f25db4a81f7f92..cf952d2b7713839f56af99a961efcb211bd908d8 100644 (file)
@@ -172,9 +172,9 @@ public class PathCheckoutCommandTest extends RepositoryTestCase {
        @Test
        public void testUpdateWorkingDirectoryFromIndex() throws Exception {
                CheckoutCommand co = git.checkout();
-               File written = writeTrashFile(FILE1, "3a");
+               writeTrashFile(FILE1, "3a");
                git.add().addFilepattern(FILE1).call();
-               written = writeTrashFile(FILE1, "");
+               File written = writeTrashFile(FILE1, "");
                assertEquals("", read(written));
                co.addPath(FILE1).call();
                assertEquals("3a", read(written));
@@ -185,9 +185,9 @@ public class PathCheckoutCommandTest extends RepositoryTestCase {
        public void testUpdateWorkingDirectoryFromHeadWithIndexChange()
                        throws Exception {
                CheckoutCommand co = git.checkout();
-               File written = writeTrashFile(FILE1, "3a");
+               writeTrashFile(FILE1, "3a");
                git.add().addFilepattern(FILE1).call();
-               written = writeTrashFile(FILE1, "");
+               File written = writeTrashFile(FILE1, "");
                assertEquals("", read(written));
                co.addPath(FILE1).setStartPoint("HEAD").call();
                assertEquals("3", read(written));
index f9af968a7e82afa1715191b8c6430121c0820372..c81731d7465a5ef9fcb7d059d586a6205b1d569b 100644 (file)
@@ -69,8 +69,7 @@ public class StashDropCommandTest extends RepositoryTestCase {
                RevCommit stashed = git.stashCreate().call();
                assertNotNull(stashed);
                stashRef = git.getRepository().exactRef(Constants.R_STASH);
-               assertEquals(stashed,
-                               git.getRepository().exactRef(Constants.R_STASH).getObjectId());
+               assertEquals(stashed, stashRef.getObjectId());
                try {
                        assertNull(git.stashDrop().setStashRef(100).call());
                        fail("Exception not thrown");
@@ -88,8 +87,7 @@ public class StashDropCommandTest extends RepositoryTestCase {
                RevCommit stashed = git.stashCreate().call();
                assertNotNull(stashed);
                stashRef = git.getRepository().exactRef(Constants.R_STASH);
-               assertEquals(stashed,
-                               git.getRepository().exactRef(Constants.R_STASH).getObjectId());
+               assertEquals(stashed, stashRef.getObjectId());
                assertNull(git.stashDrop().call());
                stashRef = git.getRepository().exactRef(Constants.R_STASH);
                assertNull(stashRef);
index 5638c1f7d9119356c5fb87b15bfe471debd87b53..562a515721a2c1d5906711378ed600c053dcb12d 100644 (file)
@@ -104,10 +104,9 @@ public class CGitAttributesTest extends RepositoryTestCase {
                                UTF_8))) {
                        r.lines().forEach(line -> {
                                // Parse the line and add to result map
-                               int start = 0;
                                int i = line.indexOf(':');
                                String path = line.substring(0, i).trim();
-                               start = i + 1;
+                               int start = i + 1;
                                i = line.indexOf(':', start);
                                String key = line.substring(start, i).trim();
                                String value = line.substring(i + 1).trim();
index 28d5ca726af6566ae4eafcfa4ba700001b100272..cb977bd60174f55b718b43f4c547e0a0a1d3be05 100644 (file)
@@ -513,7 +513,6 @@ public class RefUpdateTest extends SampleDataRepositoryTestCase {
        @Test
        public void testRefsCacheAfterUpdate() throws Exception {
                // Do not use the default repo for this case.
-               List<Ref> allRefs = db.getRefDatabase().getRefs();
                ObjectId oldValue = db.resolve("HEAD");
                ObjectId newValue = db.resolve("HEAD^");
                // first make HEAD refer to loose ref
@@ -529,7 +528,7 @@ public class RefUpdateTest extends SampleDataRepositoryTestCase {
                update = updateRef.update();
                assertEquals(Result.FAST_FORWARD, update);
 
-               allRefs = db.getRefDatabase().getRefs();
+               List<Ref> allRefs = db.getRefDatabase().getRefs();
                Ref master = getRef(allRefs, "refs/heads/master").get();
                Ref head = getRef(allRefs, "HEAD").get();
                assertEquals("refs/heads/master", master.getName());
@@ -550,7 +549,6 @@ public class RefUpdateTest extends SampleDataRepositoryTestCase {
        @Test
        public void testRefsCacheAfterUpdateLooseOnly() throws Exception {
                // Do not use the default repo for this case.
-               List<Ref> allRefs = db.getRefDatabase().getRefs();
                ObjectId oldValue = db.resolve("HEAD");
                writeSymref(Constants.HEAD, "refs/heads/newref");
                RefUpdate updateRef = db.updateRef(Constants.HEAD);
@@ -559,7 +557,7 @@ public class RefUpdateTest extends SampleDataRepositoryTestCase {
                Result update = updateRef.update();
                assertEquals(Result.NEW, update);
 
-               allRefs = db.getRefDatabase().getRefs();
+               List<Ref> allRefs = db.getRefDatabase().getRefs();
                Ref head = getRef(allRefs, "HEAD").get();
                Ref newref = getRef(allRefs, "refs/heads/newref").get();
                assertEquals("refs/heads/newref", newref.getName());
index 5c63c2df6b50dd663e9440f368388aca02aeaad3..31940a16f7150b377a24514fcb6f08ff8f8715ef 100644 (file)
@@ -1567,7 +1567,6 @@ public class ConfigTest {
        @Test
        public void testCommitTemplateEncoding()
                        throws ConfigInvalidException, IOException {
-               Config config = new Config(null);
                File workTree = tmp.newFolder("dummy-worktree");
                Repository repo = FileRepositoryBuilder
                                .create(new File(workTree, ".git"));
@@ -1576,7 +1575,7 @@ public class ConfigTest {
                String templateContent = "content of the template";
                JGitTestUtil.write(tempFile, templateContent);
                String expectedTemplatePath = tempFile.getPath();
-               config = parse("[i18n]\n\tcommitEncoding = utf-8\n"
+               Config config = parse("[i18n]\n\tcommitEncoding = utf-8\n"
                                + "[commit]\n\ttemplate = "
                                + Config.escapeValue(expectedTemplatePath) + "\n");
                assertEquals(templateContent,
@@ -1590,7 +1589,6 @@ public class ConfigTest {
        @Test(expected = ConfigInvalidException.class)
        public void testCommitTemplateWithInvalidEncoding()
                        throws ConfigInvalidException, IOException {
-               Config config = new Config(null);
                File workTree = tmp.newFolder("dummy-worktree");
                File tempFile = tmp.newFile("testCommitTemplate-");
                Repository repo = FileRepositoryBuilder
@@ -1598,7 +1596,7 @@ public class ConfigTest {
                repo.create();
                String templateContent = "content of the template";
                JGitTestUtil.write(tempFile, templateContent);
-               config = parse("[i18n]\n\tcommitEncoding = invalidEcoding\n"
+               Config config = parse("[i18n]\n\tcommitEncoding = invalidEcoding\n"
                                + "[commit]\n\ttemplate = "
                                + Config.escapeValue(tempFile.getPath()) + "\n");
                config.get(CommitConfig.KEY).getCommitTemplateContent(repo);
@@ -1607,7 +1605,6 @@ public class ConfigTest {
        @Test(expected = FileNotFoundException.class)
        public void testCommitTemplateWithInvalidPath()
                        throws ConfigInvalidException, IOException {
-               Config config = new Config(null);
                File workTree = tmp.newFolder("dummy-worktree");
                File tempFile = tmp.newFile("testCommitTemplate-");
                Repository repo = FileRepositoryBuilder
@@ -1617,7 +1614,8 @@ public class ConfigTest {
                JGitTestUtil.write(tempFile, templateContent);
                // commit message encoding
                String expectedTemplatePath = "~/nonExistingTemplate";
-               config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
+               Config config = parse(
+                               "[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
                String templatePath = config.get(CommitConfig.KEY)
                                .getCommitTemplatePath();
                assertEquals(expectedTemplatePath, templatePath);
index 3f99fe2b2604ec9f4d8f76b1f7fecde42be4e0a9..3a036acaca435926636d3f8f88bc89138d036e44 100644 (file)
@@ -1446,6 +1446,8 @@ public class MergerTest extends RepositoryTestCase {
                git.checkout().setName("master").call();
                mergeResult = git.merge().include(commitX).setStrategy(strategy)
                                .call();
+               assertEquals(MergeResult.MergeStatus.MERGED,
+                               mergeResult.getMergeStatus());
 
                // Now, merge commit A and B (i.e. "master" and "second-branch").
                // None of them have the file "a", so there is no conflict, BUT while
@@ -1739,25 +1741,25 @@ public class MergerTest extends RepositoryTestCase {
                git.add().addFilepattern("c").call();
                RevCommit commitI = git.commit().setMessage("Initial commit").call();
 
-               File a = writeTrashFile("a", "content in Ancestor");
+               writeTrashFile("a", "content in Ancestor");
                git.add().addFilepattern("a").call();
                RevCommit commitA1 = git.commit().setMessage("Ancestor 1").call();
 
-               a = writeTrashFile("a", "content in Child 1 (commited on master)");
+               writeTrashFile("a", "content in Child 1 (commited on master)");
                git.add().addFilepattern("a").call();
                // commit C1M
                git.commit().setMessage("Child 1 on master").call();
 
                git.checkout().setCreateBranch(true).setStartPoint(commitI).setName("branch-to-merge").call();
                // "a" becomes executable in A2
-               a = writeTrashFile("a", "content in Ancestor");
+               File a = writeTrashFile("a", "content in Ancestor");
                a.setExecutable(true);
                git.add().addFilepattern("a").call();
                RevCommit commitA2 = git.commit().setMessage("Ancestor 2").call();
 
                // second branch
                git.checkout().setCreateBranch(true).setStartPoint(commitA1).setName("second-branch").call();
-               a = writeTrashFile("a", "content in Child 2 (commited on second-branch)");
+               writeTrashFile("a", "content in Child 2 (commited on second-branch)");
                git.add().addFilepattern("a").call();
                // commit C2S
                git.commit().setMessage("Child 2 on second-branch").call();
index 2c7fd42732e2bc09ae0ba813b1795fb6917cc6f7..90db119b4c6213d2cbe04100b4e96058f0237035 100644 (file)
@@ -407,7 +407,7 @@ java_package_configuration(
         "-Xep:UnusedException:ERROR",
         "-Xep:UnusedMethod:WARN",
         "-Xep:UnusedNestedClass:ERROR",
-        "-Xep:UnusedVariable:WARN",
+        "-Xep:UnusedVariable:ERROR",
         "-Xep:URLEqualsHashCode:ERROR",
         "-Xep:UseBinds:ERROR",
         "-Xep:UseCorrectAssertInTests:WARN",