Skip to content

OuterTypeNumberCheck reports false positive on JEP 512 compact source files #20268

Closed

Description

@vivek-0509

Parent Issue #19971

I have read check documentation: https://checkstyle.org/checks/sizes/outertypenumber.html
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

vivek@Viveks-MacBook-Air checkstyle % cat > config.xml <<'EOF'
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
    "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <module name="OuterTypeNumber"/>
  </module>
</module>
EOF

vivek@Viveks-MacBook-Air checkstyle % cat > InputOuterTypeNumberCompact.java <<'EOF'
void main() { }

class A { }
class B { }
EOF

vivek@Viveks-MacBook-Air checkstyle % RUN_LOCALE="-Duser.language=en -Duser.country=US"
java $RUN_LOCALE -jar target/checkstyle-13.6.0-SNAPSHOT-all.jar -c config.xml InputOuterTypeNumberCompact.java

Starting audit...
[ERROR] InputOuterTypeNumberCompact.java:1:1: Outer types defined is 2 (max allowed is 1). [OuterTypeNumber]
Audit done.
Checkstyle ends with 1 errors.

Describe what you expect in detail.

OuterTypeNumberCheck must not flag the compact file. In JEP 512 semantics, class A and class B are nested types of the implicit class that the compiler synthesises for a compact source file. They are members of the implicit class, not outer types. The compact file contains exactly one outer type (the implicit class), so the check should report zero violations.

The check currently counts every CLASS_DEF (and the other type-def tokens) child of the root and increments currentOuterTypeCount once per child. Under the new grammar the root is COMPACT_COMPILATION_UNIT and its CLASS_DEF children are member declarations, but the check treats them the same as COMPILATION_UNIT children.

Expected on the compact input: zero violations.

Same code inside an ordinary class:

vivek@Viveks-MacBook-Air checkstyle % cat > InputOuterTypeNumberOrdinary.java <<'EOF'
public class InputOuterTypeNumberOrdinary {
    public void main() { }

    class A { }
    class B { }
}
EOF

vivek@Viveks-MacBook-Air checkstyle % java $RUN_LOCALE -jar target/checkstyle-13.6.0-SNAPSHOT-all.jar -c config.xml InputOuterTypeNumberOrdinary.java

Starting audit...
Audit done.

Activity

  1. added
    false negative
    false positive
    and removed
    false negative
    on Jun 7, 2026
  2. vivek-0509 commented on Jun 7, 2026

    @vivek-0509
    MemberAuthor

    To other contributors: please do not pick this issue for now to avoid duplicate work, as I may already be working on it. There are many other open issues available, so please consider picking those and help us improve the project. Thanks for understanding!

  3. added a commit that references this issue on Jun 18, 2026
  4. added this to the 13.7.0 milestone on Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

approvedbugfalse positive

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions