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] UnnecessaryCast false positive with unchecked cast #2748

Closed
maikelsteneker opened this issue Aug 27, 2020 · 3 comments · Fixed by #3204
Closed

[java] UnnecessaryCast false positive with unchecked cast #2748

maikelsteneker opened this issue Aug 27, 2020 · 3 comments · Fixed by #3204
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
Milestone

Comments

@maikelsteneker
Copy link
Contributor

maikelsteneker commented Aug 27, 2020

Affects PMD Version: 6.26.0

Description:

The rule UnnecessaryCast tells me that I'm doing a cast that is not necessary, but omitting it results in a compiler error.

This refers to the cast of (T[])list.toArray() from the example below. The toArray() methods returns an Object[], not a T[]. There is an alternative method available that takes an array as argument and uses the type of that for its return value, but in a case like this, where T is a type parameter instead of a concrete type for which an array can be initialized, that alternative method can't be used either.

Code Sample demonstrating the issue:

    /** Returns all elements from the given iterator in a new array */
    @SuppressWarnings("unchecked")
    public static <T> T[] asArray(Iterator<T> elements) {
        final List<T> list = new ArrayList<T>();
        while( elements.hasNext() ) list.add( elements.next() );
 
        T[] result = toArrayC( (Class<T>)null, list );
        if( result == null ) result = (T[])list.toArray();  // <----- this cast is really necessary
        return result;
    }

Running PMD through: CLI

@maikelsteneker maikelsteneker added the a:bug PMD crashes or fails to analyse a file. label Aug 27, 2020
@tweimer
Copy link
Contributor

tweimer commented Aug 28, 2020

That cast (T[])list.toArray() will even cause a ClassCastException at runtime if T is not an Object.

See the PMD rule ClassCastExceptionWithToArray for an example.

@adangel adangel added a:false-positive PMD flags a piece of code that is not problematic and removed a:bug PMD crashes or fails to analyse a file. labels Aug 31, 2020
@oowekyala oowekyala linked a pull request Apr 12, 2021 that will close this issue
5 tasks
@oowekyala oowekyala added this to In progress in PMD 7 Apr 12, 2021
@oowekyala oowekyala changed the title [java] UnnecessaryCast false positive [java] UnnecessaryCast false positive with unchecked cast Apr 12, 2021
oowekyala added a commit to oowekyala/pmd that referenced this issue Apr 12, 2021
@oowekyala
Copy link
Member

That cast (T[])list.toArray() will even cause a ClassCastException at runtime if T is not an Object.

For reference, the cast is unchecked because T is erased to Object. So (T[]) is erased to (Object[]) and is a noop. But a caller might see a ClassCastException if they store the result in a reified array type that is not Object[], eg String[]:

Iterator<String> iter = Arrays.asList("a").iterator();
String[] array = asArray(iter); // boom

@oowekyala oowekyala moved this from In progress to Done in PMD 7 Apr 12, 2021
@adangel adangel added this to the 7.0.0 milestone Apr 23, 2021
oowekyala added a commit to oowekyala/pmd that referenced this issue Apr 25, 2021
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@adangel
Copy link
Member

adangel commented Apr 22, 2023

This has been fixed with PMD 7.0.0-rc1.

@adangel adangel closed this as completed Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
No open projects
PMD 7
  
Done
Development

Successfully merging a pull request may close this issue.

4 participants