Skip to content

Conversation

Pankraz76
Copy link

@Pankraz76 Pankraz76 commented Jul 3, 2025

implementation for:

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

@changelog-app
Copy link

changelog-app bot commented Jul 3, 2025

Generate changelog in changelog/@unreleased

Type (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

feat: Add RemoveUnusedDeclarations

Check the box to generate changelog(s)

  • Generate changelog entry

break;
}
return true;
return input.getKind() == Diagnostic.Kind.ERROR
Copy link
Author

@Pankraz76 Pankraz76 Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract this.

@Pankraz76
Copy link
Author

kindly request some feedback.

@CRogers
Copy link
Contributor

CRogers commented Jul 16, 2025

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?

@CRogers CRogers closed this Jul 16, 2025
@CRogers CRogers reopened this Jul 16, 2025
@Pankraz76
Copy link
Author

Pankraz76 commented Jul 16, 2025

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.

@Pankraz76
Copy link
Author

could live in google as well but they seem absent.

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