Skip to content

feat: Add RemoveUnusedDeclarations #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

Pankraz76
Copy link

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

Copy link

google-cla bot commented Jun 27, 2025

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.

@Pankraz76 Pankraz76 force-pushed the RemoveUnusedDeclarations branch 2 times, most recently from 6e0d142 to 304839c Compare June 27, 2025 11:31
@Pankraz76
Copy link
Author

kindly request your feedback @nedtwigg @iddeepak.

Thanks.

@Pankraz76 Pankraz76 marked this pull request as ready for review June 27, 2025 11:32
@Pankraz76 Pankraz76 changed the title feat: Add ReplaceObsoletesStep feat: Add RemoveUnusedDeclarations Jun 27, 2025
@Pankraz76 Pankraz76 force-pushed the RemoveUnusedDeclarations branch from 304839c to 3e2ecb4 Compare June 27, 2025 11:37
@Pankraz76 Pankraz76 force-pushed the RemoveUnusedDeclarations branch from 3e2ecb4 to 83b6daa Compare June 27, 2025 12:45
@Pankraz76
Copy link
Author

@Pankraz76 Pankraz76 closed this Jul 3, 2025
@jbduncan
Copy link
Contributor

jbduncan commented Jul 3, 2025

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

@Pankraz76
Copy link
Author

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants