Use regexp to use JEP394 where IntelliJ inspection fails.

PCRE regexp: `(\w+) instanceof ([A-Z]\w+)((\)|;)?.*\n.*)\(\2\) \1`

Replaced with: `$1 instanceof $2 roflmao$3roflmao`

Then processed with a Ruby script to rewrite `roflmao` with the name of the class, lowercasing the first letter.

Then I used the IntelliJ "Remove unnecessary parentheses" inspection.

#jdk21 #jep391

PiperOrigin-RevId: 628255645
Change-Id: I74031b97efa72636fdccb1c2473bc3a5f92ad7e9
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/AbstractLabelCycleReporter.java b/src/main/java/com/google/devtools/build/lib/skyframe/AbstractLabelCycleReporter.java
index b89bd01..746093d 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/AbstractLabelCycleReporter.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/AbstractLabelCycleReporter.java
@@ -50,8 +50,8 @@
 
   /** Returns the String representation of the {@code SkyKey}. */
   protected String prettyPrint(Object rawKey) {
-    if (rawKey instanceof ActionLookupKey) {
-      return ((ActionLookupKey) rawKey).getLabel().toString();
+    if (rawKey instanceof ActionLookupKey actionLookupKey) {
+      return actionLookupKey.getLabel().toString();
     }
     return getLabel((SkyKey) rawKey).toString();
   }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ActionArtifactCycleReporter.java b/src/main/java/com/google/devtools/build/lib/skyframe/ActionArtifactCycleReporter.java
index f96186b..642f740 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ActionArtifactCycleReporter.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ActionArtifactCycleReporter.java
@@ -54,8 +54,8 @@
    * #shouldSkipOnPathToCycle}
    */
   private static String prettyPrint(SkyFunctionName skyFunctionName, Object arg) {
-    if (arg instanceof Artifact) {
-      return prettyPrintArtifact(((Artifact) arg));
+    if (arg instanceof Artifact artifact) {
+      return prettyPrintArtifact(artifact);
     } else if (arg instanceof ActionLookupData) {
       return "action from: " + arg;
     } else if (arg instanceof TopLevelActionLookupKeyWrapper key) {
@@ -87,13 +87,13 @@
   @Override
   protected Label getLabel(SkyKey key) {
     Object arg = key.argument();
-    if (arg instanceof Artifact) {
-      return ((Artifact) arg).getOwner();
-    } else if (arg instanceof ActionLookupData) {
-      return ((ActionLookupData) arg).getLabel();
-    } else if (arg instanceof TopLevelActionLookupKeyWrapper) {
-      return ((TopLevelActionLookupKeyWrapper) arg).actionLookupKey().getLabel();
-    } else if (arg instanceof TestCompletionKey
+    if (arg instanceof Artifact artifact) {
+      return artifact.getOwner();
+    } else if (arg instanceof ActionLookupData actionLookupData) {
+      return actionLookupData.getLabel();
+    } else if (arg instanceof TopLevelActionLookupKeyWrapper topLevelActionLookupKeyWrapper) {
+      return topLevelActionLookupKeyWrapper.actionLookupKey().getLabel();
+    } else if (arg instanceof TestCompletionKey testCompletionKey
         && key.functionName().equals(SkyFunctions.TEST_COMPLETION)) {
       return ((TestCompletionKey) arg).configuredTargetKey().getLabel();
     }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionFunction.java
index f599087..023a3e2 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionFunction.java
@@ -1061,8 +1061,8 @@
             input, ((ActionExecutionValue) retrievedMetadata).getExistingFileArtifactValue(input));
       } else if (retrievedMetadata instanceof MissingArtifactValue) {
         inputData.putWithNoDepOwner(input, FileArtifactValue.MISSING_FILE_MARKER);
-      } else if (retrievedMetadata instanceof FileArtifactValue) {
-        inputData.putWithNoDepOwner(input, (FileArtifactValue) retrievedMetadata);
+      } else if (retrievedMetadata instanceof FileArtifactValue fileArtifactValue) {
+        inputData.putWithNoDepOwner(input, fileArtifactValue);
       } else {
         throw new IllegalStateException(
             "unknown metadata for " + input.getExecPathString() + ": " + retrievedMetadata);
@@ -1642,8 +1642,8 @@
     }
 
     private void handleActionExecutionExceptionFromSkykey(SkyKey key, ActionExecutionException e) {
-      if (key instanceof Artifact) {
-        handleActionExecutionExceptionPerArtifact((Artifact) key, e);
+      if (key instanceof Artifact artifact) {
+        handleActionExecutionExceptionPerArtifact(artifact, e);
         return;
       }
       Set<Artifact> associatedInputs = skyKeyToDerivedArtifactSetForExceptions.get().get(key);
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionState.java b/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionState.java
index fa7141c..bff051b 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionState.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionState.java
@@ -390,8 +390,8 @@
 
     @Override
     public ActionExecutionValue get() throws ActionExecutionException, InterruptedException {
-      if (e instanceof InterruptedException) {
-        throw (InterruptedException) e;
+      if (e instanceof InterruptedException interruptedException) {
+        throw interruptedException;
       }
       throw (ActionExecutionException) e;
     }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionValue.java
index ecdea6f..87c9901 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ActionExecutionValue.java
@@ -141,8 +141,8 @@
         actionOutputMetadataStore.getAllArtifactData(),
         actionOutputMetadataStore.getAllTreeArtifactData(),
         outputSymlinks,
-        action instanceof IncludeScannable
-            ? ((IncludeScannable) action).getDiscoveredModules()
+        action instanceof IncludeScannable includeScannable
+            ? includeScannable.getDiscoveredModules()
             : NestedSetBuilder.emptySet(Order.STABLE_ORDER));
   }
 
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ActionOutputMetadataStore.java b/src/main/java/com/google/devtools/build/lib/skyframe/ActionOutputMetadataStore.java
index a6ebff5..9af32fd 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ActionOutputMetadataStore.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ActionOutputMetadataStore.java
@@ -686,8 +686,8 @@
       return FileArtifactValue.createForDirectoryWithMtime(stat.getLastModifiedTime());
     }
 
-    if (stat instanceof FileStatusWithMetadata) {
-      return ((FileStatusWithMetadata) stat).getMetadata();
+    if (stat instanceof FileStatusWithMetadata fileStatusWithMetadata) {
+      return fileStatusWithMetadata.getMetadata();
     }
 
     FileStateValue fileStateValue =
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/BuildTopLevelAspectsDetailsFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/BuildTopLevelAspectsDetailsFunction.java
index 40f725c..59ada98 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/BuildTopLevelAspectsDetailsFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/BuildTopLevelAspectsDetailsFunction.java
@@ -123,8 +123,8 @@
     AspectsList.Builder builder = new AspectsList.Builder();
 
     for (AspectClass aspectClass : topLevelAspectsClasses) {
-      if (aspectClass instanceof StarlarkAspectClass) {
-        StarlarkAspect starlarkAspect = loadStarlarkAspect(env, (StarlarkAspectClass) aspectClass);
+      if (aspectClass instanceof StarlarkAspectClass starlarkAspectClass) {
+        StarlarkAspect starlarkAspect = loadStarlarkAspect(env, starlarkAspectClass);
         if (starlarkAspect == null) {
           return null;
         }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/BzlLoadFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/BzlLoadFunction.java
index 7c17bf8..ba6d18f 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/BzlLoadFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/BzlLoadFunction.java
@@ -1136,8 +1136,8 @@
     ImmutableList.Builder<Pair<String, Location>> loads = ImmutableList.builder();
     for (StarlarkFile file : files) {
       for (Statement stmt : file.getStatements()) {
-        if (stmt instanceof LoadStatement) {
-          StringLiteral module = ((LoadStatement) stmt).getImport();
+        if (stmt instanceof LoadStatement loadStatement) {
+          StringLiteral module = loadStatement.getImport();
           loads.add(Pair.of(module.getValue(), module.getStartLocation()));
         }
       }
@@ -1536,8 +1536,8 @@
           String.format(
               "Encountered error while reading extension file '%s': %s", file, cause.getMessage());
     DetailedExitCode detailedExitCode =
-        cause instanceof DetailedException
-            ? ((DetailedException) cause).getDetailedExitCode()
+        cause instanceof DetailedException detailedException
+            ? detailedException.getDetailedExitCode()
             : BzlLoadFailedException.createDetailedExitCode(
                 errorMessage, Code.CONTAINING_PACKAGE_NOT_FOUND);
       return new BzlLoadFailedException(
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/BzlmodRepoRuleFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/BzlmodRepoRuleFunction.java
index 850bf37..9298ac9 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/BzlmodRepoRuleFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/BzlmodRepoRuleFunction.java
@@ -307,8 +307,8 @@
       RepoSpec repoSpec, ImmutableMap<String, Module> loadedModules)
       throws BzlmodRepoRuleFunctionException {
     Object object = loadedModules.get(repoSpec.bzlFile()).getGlobal(repoSpec.ruleClassName());
-    if (object instanceof RuleFunction) {
-      return ((RuleFunction) object).getRuleClass();
+    if (object instanceof RuleFunction ruleFunction) {
+      return ruleFunction.getRuleClass();
     } else {
       InvalidRuleException e =
           new InvalidRuleException("Invalid repository rule: " + repoSpec.getRuleClass());
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ClientEnvironmentValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/ClientEnvironmentValue.java
index 4357e23..99b9870 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ClientEnvironmentValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ClientEnvironmentValue.java
@@ -37,8 +37,8 @@
 
   @Override
   public boolean equals(Object o) {
-    return (o instanceof ClientEnvironmentValue)
-        && Objects.equals(((ClientEnvironmentValue) o).value, value);
+    return o instanceof ClientEnvironmentValue clientEnvironmentValue
+        && Objects.equals(clientEnvironmentValue.value, value);
   }
 
   @Override
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/DefaultSyscallCache.java b/src/main/java/com/google/devtools/build/lib/skyframe/DefaultSyscallCache.java
index 1674344..cc1c024 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/DefaultSyscallCache.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/DefaultSyscallCache.java
@@ -161,8 +161,8 @@
   @SuppressWarnings("unchecked")
   public Collection<Dirent> readdir(Path path) throws IOException {
     Object result = readdirCache.get(path);
-    if (result instanceof IOException) {
-      throw (IOException) result;
+    if (result instanceof IOException ioException) {
+      throw ioException;
     }
     return (Collection<Dirent>) result; // unchecked cast
   }
@@ -173,14 +173,14 @@
     // Try to load a Symlinks.NOFOLLOW result first. Symlinks are rare and this enables sharing the
     // cache for all non-symlink paths.
     Object result = statCache.get(Pair.of(path, Symlinks.NOFOLLOW));
-    if (result instanceof IOException) {
-      throw (IOException) result;
+    if (result instanceof IOException ioException) {
+      throw ioException;
     }
     FileStatus status = (FileStatus) result;
     if (status != NO_STATUS && symlinks == Symlinks.FOLLOW && status.isSymbolicLink()) {
       result = statCache.get(Pair.of(path, Symlinks.FOLLOW));
-      if (result instanceof IOException) {
-        throw (IOException) result;
+      if (result instanceof IOException ioException) {
+        throw ioException;
       }
       status = (FileStatus) result;
     }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/DetailedException.java b/src/main/java/com/google/devtools/build/lib/skyframe/DetailedException.java
index a7ac7ed..0f2c269 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/DetailedException.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/DetailedException.java
@@ -22,8 +22,8 @@
 
   @Nullable
   static DetailedExitCode getDetailedExitCode(Exception exception) {
-    return exception instanceof DetailedException
-        ? ((DetailedException) exception).getDetailedExitCode()
+    return exception instanceof DetailedException detailedException
+        ? detailedException.getDetailedExitCode()
         : null;
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/GlobFunctionWithMultipleRecursiveFunctions.java b/src/main/java/com/google/devtools/build/lib/skyframe/GlobFunctionWithMultipleRecursiveFunctions.java
index fae5c2d..72c07d9 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/GlobFunctionWithMultipleRecursiveFunctions.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/GlobFunctionWithMultipleRecursiveFunctions.java
@@ -375,8 +375,8 @@
 
   @SuppressWarnings("unchecked") // cast to NestedSet<PathFragment>
   private static void addToMatches(Object toAdd, NestedSetBuilder<PathFragment> matches) {
-    if (toAdd instanceof PathFragment) {
-      matches.add((PathFragment) toAdd);
+    if (toAdd instanceof PathFragment pathFragment) {
+      matches.add(pathFragment);
     } else if (toAdd instanceof NestedSet) {
       matches.addTransitive((NestedSet<PathFragment>) toAdd);
     }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/IncrementalArtifactConflictFinder.java b/src/main/java/com/google/devtools/build/lib/skyframe/IncrementalArtifactConflictFinder.java
index 7ad14e1..6afc6c72d 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/IncrementalArtifactConflictFinder.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/IncrementalArtifactConflictFinder.java
@@ -492,8 +492,8 @@
   private static Artifact getOwningArtifactFromTrie(Object trieNode) {
     Preconditions.checkArgument(
         trieNode instanceof Artifact || trieNode instanceof ConcurrentHashMap);
-    if (trieNode instanceof Artifact) {
-      return (Artifact) trieNode;
+    if (trieNode instanceof Artifact artifact) {
+      return artifact;
     }
     Object nodeIter = trieNode;
     while (!(nodeIter instanceof Artifact)) {
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java
index b6a201d..3d88b35 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java
@@ -1436,8 +1436,8 @@
         @Override
         BuildFileContainsErrorsException create(
             PackageIdentifier packId, String msg, DetailedExitCode detailedExitCode, Exception e) {
-          return e instanceof IOException
-              ? new BuildFileContainsErrorsException(packId, msg, (IOException) e, detailedExitCode)
+          return e instanceof IOException ioException
+              ? new BuildFileContainsErrorsException(packId, msg, ioException, detailedExitCode)
               : new BuildFileContainsErrorsException(packId, msg, detailedExitCode);
         }
       },
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternsValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternsValue.java
index d684d9b..c67e21f 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternsValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternsValue.java
@@ -133,8 +133,8 @@
 
   @Override
   public boolean equals(Object other) {
-    return other instanceof PrepareDepsOfPatternsValue
-        && targetPatternKeys.equals(((PrepareDepsOfPatternsValue) other).getTargetPatternKeys());
+    return other instanceof PrepareDepsOfPatternsValue prepareDepsOfPatternsValue
+        && targetPatternKeys.equals(prepareDepsOfPatternsValue.getTargetPatternKeys());
   }
 
   @Override
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
index 42e0cd9..0151bc0 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
@@ -335,8 +335,8 @@
 
         if (value instanceof FileArtifactValue || value instanceof TreeArtifactValue) {
           fsVal = (HasDigest) value;
-        } else if (value instanceof ActionExecutionValue) {
-          fsVal = ((ActionExecutionValue) value).getExistingFileArtifactValue(artifact);
+        } else if (value instanceof ActionExecutionValue actionExecutionValue) {
+          fsVal = actionExecutionValue.getExistingFileArtifactValue(artifact);
         } else {
           return NON_EXISTENT_FILE_INFO;
         }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/RecursivePkgSkyKey.java b/src/main/java/com/google/devtools/build/lib/skyframe/RecursivePkgSkyKey.java
index aefdb9d..8cbd4e8 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/RecursivePkgSkyKey.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/RecursivePkgSkyKey.java
@@ -37,8 +37,8 @@
   @Override
   public boolean equals(Object o) {
     return super.equals(o)
-        && ((o instanceof RecursivePkgSkyKey))
-        && ((RecursivePkgSkyKey) o).functionName().equals(functionName());
+        && o instanceof RecursivePkgSkyKey recursivePkgSkyKey
+        && recursivePkgSkyKey.functionName().equals(functionName());
   }
 
   /** Don't bother to memoize hashCode because {@link RecursivePkgKey#hashCode} is cheap enough. */
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeActionExecutor.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeActionExecutor.java
index 2c0020e..5efe5ae 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeActionExecutor.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeActionExecutor.java
@@ -920,8 +920,8 @@
 
   private InputMetadataProvider createFileCache(
       InputMetadataProvider graphFileCache, @Nullable FileSystem actionFileSystem) {
-    if (actionFileSystem instanceof InputMetadataProvider) {
-      return (InputMetadataProvider) actionFileSystem;
+    if (actionFileSystem instanceof InputMetadataProvider inputMetadataProvider) {
+      return inputMetadataProvider;
     }
     return new DelegatingPairInputMetadataProvider(graphFileCache, perBuildFileCache);
   }
@@ -1122,8 +1122,8 @@
       // Action failures may be caused by lost inputs. Lost input failures have higher priority
       // because rewinding may be able to restore what was lost and allow the action to complete
       // without error.
-      if (e instanceof LostInputsActionExecutionException) {
-        lostInputsException = (LostInputsActionExecutionException) e;
+      if (e instanceof LostInputsActionExecutionException lostInputsActionExecutionException) {
+        lostInputsException = lostInputsActionExecutionException;
       } else {
         try {
           checkActionFileSystemForLostInputs(
@@ -1869,8 +1869,8 @@
     if (actionResult != null) {
       return actionResult.spawnResults();
     }
-    if (exception instanceof SpawnActionExecutionException) {
-      return ImmutableList.of(((SpawnActionExecutionException) exception).getSpawnResult());
+    if (exception instanceof SpawnActionExecutionException spawnActionExecutionException) {
+      return ImmutableList.of(spawnActionExecutionException.getSpawnResult());
     }
     return ImmutableList.of();
   }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeErrorProcessor.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeErrorProcessor.java
index e90e586..8497791 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeErrorProcessor.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeErrorProcessor.java
@@ -551,8 +551,8 @@
       executionDetailedExitCode =
           getExecutionDetailedExitCodeFromCause(result, exception, bugReporter);
       analysisRootCauses =
-          exception instanceof ActionExecutionException
-              ? ((ActionExecutionException) exception).getRootCauses()
+          exception instanceof ActionExecutionException actionExecutionException
+              ? actionExecutionException.getRootCauses()
               : NestedSetBuilder.emptySet(Order.STABLE_ORDER);
     } else {
       BugReport.logUnexpected(
@@ -896,8 +896,8 @@
       throws BuildFailedException, TestExecException {
     Throwables.throwIfUnchecked(cause);
     Throwable innerCause = cause.getCause();
-    if (innerCause instanceof TestExecException) {
-      throw (TestExecException) innerCause;
+    if (innerCause instanceof TestExecException testExecException) {
+      throw testExecException;
     }
     if (cause instanceof ActionExecutionException actionExecutionCause) {
       String message = cause.getMessage();
@@ -914,11 +914,11 @@
           /* errorAlreadyShown= */ !actionExecutionCause.showError(),
           actionExecutionCause.getDetailedExitCode());
     }
-    if (cause instanceof InputFileErrorException) {
-      throw (InputFileErrorException) cause;
+    if (cause instanceof InputFileErrorException inputFileErrorException) {
+      throw inputFileErrorException;
     }
-    if (cause instanceof TopLevelOutputException) {
-      throw (TopLevelOutputException) cause;
+    if (cause instanceof TopLevelOutputException topLevelOutputException) {
+      throw topLevelOutputException;
     }
 
     // We encountered an exception we don't think we should have encountered. This can indicate
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java
index 8009e05..79ddaf2 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java
@@ -1906,8 +1906,8 @@
       ErrorInfo error = firstError.getValue();
       Throwable e = error.getException();
       // Wrap loading failed exceptions
-      if (e instanceof NoSuchThingException) {
-        e = new InvalidConfigurationException(((NoSuchThingException) e).getDetailedExitCode(), e);
+      if (e instanceof NoSuchThingException noSuchThingException) {
+        e = new InvalidConfigurationException(noSuchThingException.getDetailedExitCode(), e);
       } else if (e == null && !error.getCycleInfo().isEmpty()) {
         cyclesReporter.reportCycles(error.getCycleInfo(), firstError.getKey(), eventHandler);
         e =
@@ -4239,8 +4239,8 @@
     graph.parallelForEach(
         node -> {
           SkyKey k = node.getKey();
-          if (k instanceof FileStateKey) {
-            RootedPath rootedPath = ((FileStateKey) k).argument();
+          if (k instanceof FileStateKey fileStateKey) {
+            RootedPath rootedPath = fileStateKey.argument();
             if (workingSetRootedPaths.contains(rootedPath)) {
               leafs.add(k);
             }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeFocuser.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeFocuser.java
index af1e3bf..b00c067 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeFocuser.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeFocuser.java
@@ -246,8 +246,8 @@
         // This is necessary to keep the action inputs encapsulated by a NestedSet. Otherwise,
         // those inputs will be missing. ActionExecutionFunction#lookupInput allows getting a
         // transitive dep without adding a SkyframeDependency on it.
-        if (dep instanceof ArtifactNestedSetKey) {
-          for (Artifact a : ((ArtifactNestedSetKey) dep).expandToArtifacts()) {
+        if (dep instanceof ArtifactNestedSetKey artifactNestedSetKey) {
+          for (Artifact a : artifactNestedSetKey.expandToArtifacts()) {
             SkyKey aKey = Artifact.key(a);
             if (keptDeps.add(aKey)) {
               maybeCollectVerificationSet(aKey);
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeTargetPatternEvaluator.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeTargetPatternEvaluator.java
index f5980e6..09917ef 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeTargetPatternEvaluator.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeTargetPatternEvaluator.java
@@ -128,8 +128,8 @@
           // exception if there is a bug in error handling.
           Exception exception = error.getException();
           errorMessage = exception.getMessage();
-          if (exception instanceof TargetParsingException) {
-            targetParsingException = (TargetParsingException) exception;
+          if (exception instanceof TargetParsingException tpe) {
+            targetParsingException = tpe;
           } else {
             targetParsingException = wrapException(exception, key, key);
           }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/StarlarkAspectFactory.java b/src/main/java/com/google/devtools/build/lib/skyframe/StarlarkAspectFactory.java
index 955d53f..f98bf8c 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/StarlarkAspectFactory.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/StarlarkAspectFactory.java
@@ -130,8 +130,8 @@
     if (requiredConfigFragments != null) {
       builder.addProvider(requiredConfigFragments);
     }
-    if (aspectStarlarkObject instanceof Iterable) {
-      addDeclaredProviders(builder, (Iterable) aspectStarlarkObject);
+    if (aspectStarlarkObject instanceof Iterable<?> iterable) {
+      addDeclaredProviders(builder, iterable);
     } else {
       // Either an old-style struct or a single declared provider (not in a list)
       Info info = (Info) aspectStarlarkObject;
@@ -156,8 +156,8 @@
           }
         }
       } else {
-        if (info instanceof StarlarkInfo) {
-          info = ((StarlarkInfo) info).unsafeOptimizeMemoryLayout();
+        if (info instanceof StarlarkInfo starlarkInfo) {
+          info = starlarkInfo.unsafeOptimizeMemoryLayout();
         }
         builder.addStarlarkDeclaredProvider(info);
       }
@@ -178,8 +178,8 @@
                 + "a sequence of declared providers, instead got a %s at index %d",
             Starlark.type(o), i);
       }
-      if (o instanceof StarlarkInfo) {
-        o = ((StarlarkInfo) o).unsafeOptimizeMemoryLayout();
+      if (o instanceof StarlarkInfo starlarkInfo) {
+        o = starlarkInfo.unsafeOptimizeMemoryLayout();
       }
       builder.addStarlarkDeclaredProvider((Info) o);
       i++;
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/StarlarkBuildSettingsDetailsFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/StarlarkBuildSettingsDetailsFunction.java
index d285236..ea902c5 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/StarlarkBuildSettingsDetailsFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/StarlarkBuildSettingsDetailsFunction.java
@@ -224,8 +224,8 @@
       if (buildSettingTarget.getAssociatedRule().getRuleClass().equals(ALIAS_RULE_NAME)) {
         Object actualValue =
             buildSettingTarget.getAssociatedRule().getAttr(ALIAS_ACTUAL_ATTRIBUTE_NAME);
-        if (actualValue instanceof Label) {
-          actualSettingBuilder.add((Label) actualValue);
+        if (actualValue instanceof Label label) {
+          actualSettingBuilder.add(label);
           continue;
         } else if (actualValue instanceof SelectorList) {
           // configured "actual" value
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/TargetCycleReporter.java b/src/main/java/com/google/devtools/build/lib/skyframe/TargetCycleReporter.java
index 69fdac1..6166b1a 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/TargetCycleReporter.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/TargetCycleReporter.java
@@ -65,10 +65,10 @@
 
   @Override
   public String prettyPrint(Object key) {
-    if (key instanceof ConfiguredTargetKey) {
-      return ((ConfiguredTargetKey) key).prettyPrint();
-    } else if (key instanceof AspectKey) {
-      return ((AspectKey) key).prettyPrint();
+    if (key instanceof ConfiguredTargetKey configuredTargetKey) {
+      return configuredTargetKey.prettyPrint();
+    } else if (key instanceof AspectKey aspectKey) {
+      return aspectKey.prettyPrint();
     } else {
       return getLabel((SkyKey) key).toString();
     }
@@ -78,8 +78,8 @@
   public Label getLabel(SkyKey key) {
     if (key instanceof ActionLookupKey) {
       return Preconditions.checkNotNull(((ActionLookupKey) key.argument()).getLabel(), key);
-    } else if (key instanceof TransitiveTargetKey) {
-      return ((TransitiveTargetKey) key).getLabel();
+    } else if (key instanceof TransitiveTargetKey transitiveTargetKey) {
+      return transitiveTargetKey.getLabel();
     } else {
       throw new UnsupportedOperationException(key.toString());
     }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternFunction.java
index 08eb8185..51af232 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternFunction.java
@@ -81,11 +81,8 @@
               //  won't be listed when doing somepackage:* for the handful of cases still on the
               //  allowlist. This is only a Google-internal problem and the scale of it is
               //  acceptable in the short term while cleaning up the allowlist.
-              if (target instanceof OutputFile
-                  && ((OutputFile) target)
-                      .getGeneratingRule()
-                      .getRuleClass()
-                      .equals("cc_library")) {
+              if (target instanceof OutputFile outputFile
+                  && outputFile.getGeneratingRule().getRuleClass().equals("cc_library")) {
                 continue;
               }
               resolvedTargetsBuilder.add(target);
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveTraversalValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveTraversalValue.java
index 73b533d..6d7ac1f 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveTraversalValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveTraversalValue.java
@@ -77,8 +77,8 @@
         }
 
         AdvertisedProviderSet providers =
-            target instanceof Rule
-                ? ((Rule) target).getRuleClassObject().getAdvertisedProviders()
+            target instanceof Rule rule
+                ? rule.getRuleClassObject().getAdvertisedProviders()
                 : AdvertisedProviderSet.EMPTY;
 
         value = new TransitiveTraversalValueWithoutError(providers, target.getTargetKind());
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/packages/AbstractPackageLoader.java b/src/main/java/com/google/devtools/build/lib/skyframe/packages/AbstractPackageLoader.java
index 07c6949..2eaf03d 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/packages/AbstractPackageLoader.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/packages/AbstractPackageLoader.java
@@ -448,8 +448,8 @@
         return new StarlarkModuleLoadingException("Cycle encountered while loading " + label);
       }
       Throwable e = Preconditions.checkNotNull(error.getException());
-      if (e instanceof BzlLoadFailedException) {
-        return new StarlarkModuleLoadingException((BzlLoadFailedException) e);
+      if (e instanceof BzlLoadFailedException bzlLoadFailedException) {
+        return new StarlarkModuleLoadingException(bzlLoadFailedException);
       }
       throw new IllegalStateException(
           "Unexpected Exception type from BzlLoadValue for " + label + " with error: " + error, e);
@@ -482,8 +482,8 @@
           pkgId, "Cycle encountered while loading package " + pkgId);
     }
     Throwable e = Preconditions.checkNotNull(error.getException());
-    if (e instanceof NoSuchPackageException) {
-      return (NoSuchPackageException) e;
+    if (e instanceof NoSuchPackageException noSuchPackageException) {
+      return noSuchPackageException;
     }
     throw new IllegalStateException(
         "Unexpected Exception type from PackageValue for '" + pkgId + "'' with error: " + error, e);
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/rewinding/ActionRewindStrategy.java b/src/main/java/com/google/devtools/build/lib/skyframe/rewinding/ActionRewindStrategy.java
index d4932b2..db7094d 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/rewinding/ActionRewindStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/rewinding/ActionRewindStrategy.java
@@ -440,8 +440,8 @@
         }
       }
 
-      if (lostInput instanceof Artifact
-          && failedActionDeps.contains(Artifact.key((Artifact) lostInput))) {
+      if (lostInput instanceof Artifact artifact
+          && failedActionDeps.contains(Artifact.key(artifact))) {
         checkDerived((Artifact) lostInput);
 
         lostInputOwningDirectDeps.add((DerivedArtifact) lostInput);
@@ -548,10 +548,10 @@
     for (EndpointPair<SkyKey> edge : edges) {
       SkyKey target = edge.target();
       if (target instanceof Artifact && rewindGraph.addNode(target)) {
-        newlyVisitedArtifacts.add(((DerivedArtifact) target));
+        newlyVisitedArtifacts.add((DerivedArtifact) target);
       }
       if (target instanceof ActionLookupData && rewindGraph.addNode(target)) {
-        newlyVisitedActions.add(((ActionLookupData) target));
+        newlyVisitedActions.add((ActionLookupData) target);
       }
       rewindGraph.putEdge(edge.source(), edge.target());
     }
@@ -584,8 +584,8 @@
       if (newlyVisited) {
         if (artifactKey instanceof Artifact) {
           newlyVisitedArtifacts.add((DerivedArtifact) artifactKey);
-        } else if (artifactKey instanceof ActionLookupData) {
-          newlyVisitedActions.add((ActionLookupData) artifactKey);
+        } else if (artifactKey instanceof ActionLookupData actionLookupData) {
+          newlyVisitedActions.add(actionLookupData);
         }
       }
       rewindGraph.putEdge(actionKey, artifactKey);
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsCycleReporter.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsCycleReporter.java
index 05fca20..4d249e0 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsCycleReporter.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/RegisteredToolchainsCycleReporter.java
@@ -79,8 +79,8 @@
 
     Function<Object, String> printer =
         input -> {
-          if (input instanceof ConfiguredTargetKey) {
-            Label label = ((ConfiguredTargetKey) input).getLabel();
+          if (input instanceof ConfiguredTargetKey ctk) {
+            Label label = ctk.getLabel();
             return label.toString();
           }
           if (input instanceof RegisteredToolchainsValue.Key) {
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/ToolchainException.java b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/ToolchainException.java
index f74a40f..cafc4d8 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/ToolchainException.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/toolchains/ToolchainException.java
@@ -71,8 +71,8 @@
     for (Throwable cause = getCause();
         cause != null && cause != cause.getCause();
         cause = cause.getCause()) {
-      if (cause instanceof ConfiguredValueCreationException) {
-        return (ConfiguredValueCreationException) cause;
+      if (cause instanceof ConfiguredValueCreationException configuredValueCreationException) {
+        return configuredValueCreationException;
       }
     }
     Cause cause =