Skip to content

feat: Add RemoveUnusedDeclarations #2547

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

Conversation

Pankraz76
Copy link
Contributor

integration of:

  • feat: Add RemoveUnusedDeclarations palantir/palantir-java-format#1342

    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 (Call getProperty can be simplified for line.separator #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


@Override
public String apply(String input) throws Exception {
return formatter.formatSourceAndFixImportsAndDeclarations(input);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cannot resolve method 'formatSourceAndFixImportsAndDeclarations' in 'Formatter'

@Pankraz76 Pankraz76 closed this Aug 6, 2025
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.

1 participant