Quick fix for S6877 "Replace if with pattern match guard" produces uncompilable code on constant cas

Please provide

  • Operating system:
NAME="CachyOS Linux"
PRETTY_NAME="CachyOS"
ID=cachyos
ID_LIKE=arch
BUILD_ID=rolling
ANSI_COLOR="38;2;23;147;209"
HOME_URL="https://cachyos.org/"
DOCUMENTATION_URL="https://wiki.cachyos.org/"
SUPPORT_URL="https://discuss.cachyos.org/"
BUG_REPORT_URL="https://github.com/cachyos"
PRIVACY_POLICY_URL="https://terms.archlinux.org/docs/privacy-policy/"
LOGO=cachyos
  • SonarQube for IntelliJ plugin version: 12.2.0.84584
  • IntelliJ Version:

IntelliJ IDEA 2026.1.1
Build #IU-261.23567.138, built on April 23, 2026
Source revision: a789291cd24df
Licensed to JB Alumni / Pasha Finkelshteyn
You have a perpetual fallback license for this version.
Subscription is active until December 31, 2029.
Runtime version: 25.0.2+10-b329.117 amd64137.0.17-261-b81
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Toolkit: sun.awt.wl.WLToolkit
Linux 7.0.3-1-cachyos
CachyOS; glibc: 2.43; desktop: KDE (plasmashell 6.6.4)
Exception reporter ID: 2026-04-11_0f2ae742-e8de-4a5d-a94d-806de8a9e7d0
GC: G1 Young Generation, G1 Concurrent GC, G1 Old Generation
Memory: 2048M
Cores: 16
Registry:
  ide.experimental.ui=true
  trace.state.event.service.url=https://api.jetbrains.cloud/trace-status
Non-Bundled Plugins:
  com.intellij.java (261.23567.138)
  JavaScript (261.23567.138)
  org.jetbrains.junie (261.1218.186)
  org.jetbrains.completion.full.line (261.23567.181)
  com.intellij.spring (261.23567.176)
  hg4idea (261.23567.28)
  Subversion (261.23567.176)
  com.jonnyzzz.mcp-steroid (0.94.0-8682a5ce)
  com.intellij.wiremock (261.1.0)
  PerforceDirectPlugin (261.23567.28)
  com.intellij.nativeDebug (261.23567.80)
  com.intellij.ml.llm (261.23567.193)
  com.intellij.spring.debugger (261.23567.28)
  com.intellij.spring.websocket (261.22158.182)
  org.jetbrains.plugins.node-remote-interpreter (261.23567.143)
  com.intellij.reactivestreams (261.22158.182)
  org.sonarlint.idea (12.2.0.84584)
Kotlin: 261.23567.138-IJ
Current Desktop: KDE

  • Programming language you’re coding in: Java
  • Is connected mode used:
    • SonarQube Cloud, SonarQube Server, or SonarQube Community Build? (if one of the latter two, which version?): none/don’t know

And a thorough description of the problem / question:

Reproducer:

enum Type { DATE, TEXT }

record Match(Object value) {}

public class Example {
    void process(Type type, Match match) {
        switch (type) {
            case DATE -> { if (match.value() instanceof String s) System.out.println(s); }
            case TEXT -> {}
        }
    }
}

What happens:

S6877 suggests replacing the if with a pattern match guard. Applying the quick fix produces:

case DATE when match.value() instanceof String s -> { System.out.println(s); }

This does not compile. The when guard is only valid after a pattern case label (JEP 441), not after a constant case label like an enum value. The Java grammar:

case Pattern [Guard] ->    // âś… guard allowed
case CaseConstant ->       // ❌ no guard allowed

Expected: Rule should not trigger (or quick fix should not be offered) when the case label is a constant rather than a pattern.

Hello, thanks for the report, I created a ticket to fix the bug :slight_smile: