feat: Add `RemoveUnusedDeclarations`
feat: Add RemoveUnusedDeclarations formatting rule
This PR introduces a new Java formatting rule that automatically removes redundant modifiers and declarations that are either:
- Implicitly provided by Java language specifications
- Unnecessarily verbose without adding clarity
- Obsolete in modern Java versions
Motivation
During codebase modernization efforts (#2524), we identified recurring patterns where:
- Developers explicitly declare language defaults (e.g.,
publicin interfaces) - Projects maintain legacy modifier patterns (e.g.,
final staticinstead ofstatic final) - Modern Java features aren't fully leveraged (records, sealed classes)
As highlighted in this comment, such redundancies:
- Increase cognitive load during code reviews
- Create maintenance overhead
- Reduce code consistency
Key Features
The rule handles:
-
Interface members:
- Removes redundant
public,static,final,abstractmodifiers - Example:
public static final int CONST→int CONST
- Removes redundant
-
Nested types:
- Simplifies modifiers in inner classes/interfaces
- Example:
public static class Inner→static class Inner
-
Enum declarations:
- Removes redundant modifiers on constants and methods
- Example:
public static final VALUE→VALUE
-
Modern Java features:
- Optimizes record components (
public finalparams → implicit) - Simplifies sealed class hierarchies
- Optimizes record components (
-
Annotation declarations:
- Removes redundant modifiers on annotation elements
- Preserves special syntax (
@interfaceformatting)
Benefits
-
Reduced Noise:
- Eliminate redundant modifiers in typical codebases
- Focuses attention on meaningful declarations
-
Maintenance Efficiency:
- Automatic updates when Java language defaults change
- Consistent application across entire codebase
-
Modern Java Support:
- First-class handling of records and sealed classes
- Future-proof for new language features
-
Non-intrusive:
- Preserves all actual semantics
- Only removes truly redundant declarations
Implementation Notes
- Built as a standalone step compatible with existing formatting pipelines
- Preserves all non-redundant modifiers (e.g., keeps
privatewhen meaningful) - Handles special cases like
@Nullable finalparameters - Comprehensive test coverage
Integration
Works seamlessly with:
- Google Java Format
- Spotless (via #2530)
- Any Java formatting pipeline
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).
View this failed invocation of the CLA check for more information.
For the most up to date status, view the checks section at the bottom of the pull request.
kindly request your feedback @nedtwigg @iddeepak.
Thanks.
google seems dead.
- https://github.com/palantir/palantir-java-format/pull/1342
@Pankraz76, for future reference, Google employees can't even look at your PR until you accept the CLA.
its signed:
| Agreement | Name | Date Signed | Manage |
|---|---|---|---|
| Google Individual CLA | vincent potucek | Jun 27, 2025 05:50 PDT | Edit Contact Information |
Agreement Name Date Signed Manage Google Individual CLA vincent potucek Jun 27, 2025 05:50 PDT Edit Contact Information
as every tweak encountered here is fixed in palantir, consider google dead and will drop accordingly:
- https://github.com/diffplug/spotless/pull/2541