Skip to content

[java] Numeric conditional with a char operand and an int constant is typed as int #7150

Closed

Description

@saryn17

Affects PMD Version: 7.28.0 (also 7.29.0-SNAPSHOT)

Description:

In the sample, the second argument has type char, so c(Integer, char) is applicable by strict invocation and javac selects it in phase 1. c(int, int) needs the unboxing of the first argument and is left to phase 2. PMD types the argument as int. c(Integer, char) is then not applicable in any phase, because int does not convert to char. PMD selects c(int, int).

JLS 15.25.2 gives a numeric conditional expression the type T when one operand has type byte, short or char and the other is a constant expression of type int representable in T. flag ? 'a' : 1 has type char. ConditionalMirrorImpl.getConditionalStandaloneType applies binary numeric promotion to the two operand types instead, which gives int:

if (thenType.isNumeric() && elseType.isNumeric()) {
return TypeConversion.binaryNumericPromotion(thenType.unbox(), elseType.unbox());
}

The same lines type flag ? b : s with a byte b and a short s as int, where JLS 15.25.2 gives short. javac boxes Object o = flag ? b : s; with Short.valueOf.

Found while working on #7136.

Code Sample demonstrating the issue:

class Cond {
    static void c(int x, int y) { }
    static void c(Integer x, char y) { }

    void t(Integer value, boolean flag) {
        c(value, flag ? 'a' : 1);
    }
}

Steps to reproduce:

  1. javac Cond.java, also with --release 8, then javap -c -p Cond: the call is c:(Ljava/lang/Integer;C)V.
  2. Run PMD on Cond.java with the XPath rule //MethodCall[pmd-java:matchesSig("Cond#c(int,int)")]. It reports line 6.

Running PMD through: Other (PmdAnalysis API)

Activity

  1. added
    in:type-resolution
    on Oct 26, 2026
  2. added this to the 7.29.0 milestone on Oct 26, 2026
  3. added 2 commits that reference this issue on Oct 26, 2026
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

    in:type-resolution

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions