Skip to content

[java] Enhance UseExplicitTypes to allow verbose long explicit types #6903

Closed

Description

@AB-xdev

Is your feature request related to a problem? Please describe.

Original backstory and arguments can be found in xdev-software/java-setup-template#3

I'd basically like a rule that flags usage of the var keyword when the (normally) written type would be below a certain length.
Writing var x = 0 instead of int x = 0 makes not a lot of sense and just makes reading/reviewing the code (without an IDE) harder.
Also most IDEs have a "variable extraction feature" that automatically adds the correct type.

Using var for something like Iterator<? extends Map.Entry<? extends MySuperCoolCacheKey, ? extends MySuperCoolValue>> however is a lot reasonable IMHO.

For further reading I can also recommend OpenJDK's Local Variable Type Inference CSG.

Code Sample:

// Could have just used an `int`
// Will create an `int` however you might want a `byte` or `short`
var x = 0;

// DANGEROUS: infers as List<Object>
var list = List.of();

// What is customers? Maybe a List? a Set?
var customers = dbconn.executeQuery("SELECT ...");
// A dozen lines of other code go here...
customers.map(...).toList(); // Nevermind it was a Stream...

Describe the solution you'd like
Enhance the existing rule UseExplicitTypes

  • add a property to allow types with a given minimum length, e.g. lengthLimit - The rule will be triggered when the length of the type is below this limit

Describe alternatives you've considered
a new rule

Additional context
Add any other context about the feature request here.

Activity

  1. added
    a:new-rule
    on Jul 22, 2026
  2. AB-xdev commented on Jul 22, 2026

    @AB-xdev
    Author

    As I already did a reference implementation I will send a PR for this

  3. UncleOwen commented on Jul 22, 2026

    @UncleOwen
    Member

    // DANGEROUS: infers as List<Object>
    var list = List.of();

    Maybe find a better example here. The list returned by List.of() is empty and immutable, so this is safe.

    But I like the idea, awaiting your PR!

  4. changed the title [-][java] New rule: AvoidVarForShortType[/-] [+][java] Enhance UseExplicitTypes to allow verbose long explicit types[/+] on Oct 3, 2026
  5. added
    an:enhancement
    and removed
    a:new-rule
    on Oct 3, 2026
  6. added a commit that references this issue on Oct 3, 2026
  7. added this to the 7.29.0 milestone on Oct 3, 2026
  8. added a commit that references this issue on Oct 6, 2026
    732214e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions