google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

feat: Add `RemoveUnusedDeclarations`

Open Pankraz76 opened this issue 6 months ago • 6 comments

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:

  1. Developers explicitly declare language defaults (e.g., public in interfaces)
  2. Projects maintain legacy modifier patterns (e.g., final static instead of static final)
  3. 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, abstract modifiers
    • Example: public static final int CONSTint CONST
  • Nested types:

    • Simplifies modifiers in inner classes/interfaces
    • Example: public static class Innerstatic class Inner
  • Enum declarations:

    • Removes redundant modifiers on constants and methods
    • Example: public static final VALUEVALUE
  • Modern Java features:

    • Optimizes record components (public final params → implicit)
    • Simplifies sealed class hierarchies
  • Annotation declarations:

    • Removes redundant modifiers on annotation elements
    • Preserves special syntax (@interface formatting)

Benefits

  1. Reduced Noise:

    • Eliminate redundant modifiers in typical codebases
    • Focuses attention on meaningful declarations
  2. Maintenance Efficiency:

    • Automatic updates when Java language defaults change
    • Consistent application across entire codebase
  3. Modern Java Support:

    • First-class handling of records and sealed classes
    • Future-proof for new language features
  4. 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 private when meaningful)
  • Handles special cases like @Nullable final parameters
  • Comprehensive test coverage

Integration

Works seamlessly with:

  • Google Java Format
  • Spotless (via #2530)
  • Any Java formatting pipeline

Pankraz76 avatar Jun 27 '25 11:06 Pankraz76

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.

google-cla[bot] avatar Jun 27 '25 11:06 google-cla[bot]

kindly request your feedback @nedtwigg @iddeepak.

Thanks.

Pankraz76 avatar Jun 27 '25 11:06 Pankraz76

google seems dead.

  • https://github.com/palantir/palantir-java-format/pull/1342

Pankraz76 avatar Jul 03 '25 10:07 Pankraz76

@Pankraz76, for future reference, Google employees can't even look at your PR until you accept the CLA.

jbduncan avatar Jul 03 '25 15:07 jbduncan

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

Pankraz76 avatar Jul 04 '25 09:07 Pankraz76

as every tweak encountered here is fixed in palantir, consider google dead and will drop accordingly:

  • https://github.com/diffplug/spotless/pull/2541

Pankraz76 avatar Jul 04 '25 09:07 Pankraz76