feat: Add `RemoveUnusedDeclarations`
implementation for:
- https://github.com/diffplug/spotless/pull/2547
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
Generate changelog in changelog/@unreleased
changelog/@unreleasedType (Select exactly one)
- [ ] Feature (Adding new functionality)
- [ ] Improvement (Improving existing functionality)
- [ ] Fix (Fixing an issue with existing functionality)
- [ ] Break (Creating a new major version by breaking public APIs)
- [ ] Deprecation (Removing functionality in a non-breaking way)
- [ ] Migration (Automatically moving data/functionality to a new system)
Description
RemoveUnusedDeclarations
Check the box to generate changelog(s)
- [ ] Generate changelog entry
kindly request some feedback.
I feel that formatters should focus on formatting the code and avoid changing the code.
At Palantir we use other tools (eg error-prone) to achieve these kind of code changes, and I'd prefer to keep it that way rather than add extra complexity to this formatter.
Is there buy-in from spotless for this feature?
Is there buy-in from spotless for this feature?
they rejected due to backwards compability.
Will integrate it there when its done. Already created draft PR for this.
- https://github.com/diffplug/spotless/pull/2547
could live in google as well but they seem absent.