Skip to content
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

[java] UseExplicitTypes: cases where 'var' should be unobjectionable #4916

Open
StevanWhite opened this issue Apr 3, 2024 · 0 comments
Open
Labels
an:enhancement An improvement on existing features / rules

Comments

@StevanWhite
Copy link

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

The rule UseExplicitTypes regarding the Java 10 type var has two properties, allowLiterals and allowCtors.

There are two other legitimate uses of var that I run into often, where the type is obvious:
explicit casts, and iteration over an Iterable whose type is explicit and apparent.
But PMD complains about these, and cramps my style.

Direct cast is still a reasonable construct:

// direct cast
    var other = (MyClass<E>)obj;

An iteration over a Collection whose type is immediately apparent:

    method( List<E> list) {
        for( var e : list ) {
            ...
        }
    }

Generally, iteration over an explicitly-typed Iterable:

    method( Iterable<E> it) {
        for( var e : it ) {
            ...
        }
    }

Describe the solution you'd like

For the rule UseExplicitTypes,

A property allowExplicitCasts, to allow var in the first situation.

A property allowExplicitIterables to allow var in the second situation.

Describe alternatives you've considered

The exact property names aren't important. Those were the best I came up with.

Additional context

@StevanWhite StevanWhite added the an:enhancement An improvement on existing features / rules label Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
an:enhancement An improvement on existing features / rules
Projects
None yet
Development

No branches or pull requests

1 participant