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] [regression from 6.55] UseDiamondOperator false-positive with Java 8 code #4619

Closed
kohlschuetter opened this issue Jul 2, 2023 · 2 comments
Labels
a:false-positive PMD flags a piece of code that is not problematic was:invalid

Comments

@kohlschuetter
Copy link

Affects PMD Version:
7.0.0-SNAPSHOT

Rule:
UseDiamondOperator

Description:
A class member that initializes an anonymous inner class with generics must not use the diamond operator (for code that targets Java 8).

Code Sample demonstrating the issue:

from https://github.com/kohlschutter/junixsocket/blob/72110d0/junixsocket-tipc/src/main/java/org/newsclub/net/unix/tipc/AFTIPCSelectorProvider.java

  static final AFAddressFamily<@NonNull AFTIPCSocketAddress> AF_TIPC = AFAddressFamily
      .registerAddressFamilyImpl("tipc", AFTIPCSocketAddress.addressFamily(),
          new AFAddressFamilyConfig<AFTIPCSocketAddress>() {   // here

          /// ...
         });

Expected outcome:

PMD reports a violation at the line marked with "// here"", but that's wrong. That's a false positive.

Running PMD through: CLI

Only occurs with "aux-classpath" set. Download junixsocket-selftest-2.6.2-jar-with-dependencies.jar to reproduce. Use attached pmd-ruleset.xml.zip

pmd check AFTIPCSelectorProvider.java --aux-classpath=junixsocket-selftest-2.6.2-jar-with-dependencies.jar --use-version=java-8 -f text -R pmd-ruleset.xml

Warning shown:

AFTIPCSelectorProvider.java:128:	UseDiamondOperator:	Explicit type arguments can be replaced by a diamond: `new AFTIPCSocketPair<>(s1, s2)`

After changing to the diamond operator, compilation for Java 8 fails with:

[ERROR] .../AFTIPCSelectorProvider.java:[48,36] cannot infer type arguments for org.newsclub.net.unix.AFAddressFamilyConfig<A>
  reason: '<>' with anonymous inner classes is not supported in -source 8
    (use -source 9 or higher to enable '<>' with anonymous inner classes)
@kohlschuetter kohlschuetter added the a:false-positive PMD flags a piece of code that is not problematic label Jul 2, 2023
@oowekyala
Copy link
Member

I cannot reproduce this. When I try with PMD 7 snapshot (built locally), the rule behaves as expected:

# java 8
$ sandbox/pmd-bin-7.0.0-SNAPSHOT/bin/pmd check -d ~/Downloads/AFTIPCSelectorProvider.java --aux-classpath ~/Downloads/junixsocket-selftest-2.6.2-jar-with-dependencies.jar -R category/java/codestyle.xml/UseDiamondOperator --no-progress  --use-version java-8 --no-cache
[main] INFO net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand - Log level is at INFO
/home/clem/Downloads/AFTIPCSelectorProvider.java:128:	UseDiamondOperator:	Explicit type arguments can be replaced by a diamond: `new AFTIPCSocketPair<>(s1, s2)`
# java 9
$ sandbox/pmd-bin-7.0.0-SNAPSHOT/bin/pmd check -d ~/Downloads/AFTIPCSelectorProvider.java --aux-classpath ~/Downloads/junixsocket-selftest-2.6.2-jar-with-dependencies.jar -R category/java/codestyle.xml/UseDiamondOperator --no-progress  --use-version java-9 --no-cache
[main] INFO net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand - Log level is at INFO
/home/clem/Downloads/AFTIPCSelectorProvider.java:48:	UseDiamondOperator:	Explicit type arguments can be replaced by a diamond: `new AFAddressFamilyConfig<>()`
/home/clem/Downloads/AFTIPCSelectorProvider.java:128:	UseDiamondOperator:	Explicit type arguments can be replaced by a diamond: `new AFTIPCSocketPair<>(s1, s2)`

I got the same results with the released binary for PMD7 RC3 (there has been no changes to the rule since).

Can you give more info to reproduce this?

@oowekyala oowekyala added the is:unreproducible A bug report that cannot be reproduced - reporter should provide a MWE label Jul 2, 2023
oowekyala added a commit that referenced this issue Jul 2, 2023
@kohlschuetter
Copy link
Author

My bad, can no longer reproduce either. I think I missed the "targetJdk" config in my POM.
Thanks for the fast response, though!

@oowekyala oowekyala added was:invalid and removed is:unreproducible A bug report that cannot be reproduced - reporter should provide a MWE labels Jul 2, 2023
@oowekyala oowekyala closed this as not planned Won't fix, can't repro, duplicate, stale Jul 2, 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 was:invalid
Projects
None yet
Development

No branches or pull requests

2 participants