Skip to content

Guard Conditions are not recognized #1960

@polarene

Description

@polarene

Describe the bug

A block of code having a when() with guard conditions is incorrectly reported as an error.

Example code:

when (val node = path.node) {
    // Handle property declarations (val/const val)
    is PropertyDeclaration if node.initializerExpression != null -> addIfValid(node.initializerExpression!!)
    // ...
}

the relevant part of error log:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':diktatCheck'.
> 124:44 Expecting '->'

Notice the reported text position is at the first character of if.

Expected behavior

Code passes checks

Observed behavior

  • Code fails the check
  • The message reports an error but no specific rule is violated
  • The Diktat reports are empty

Steps to Reproduce

  1. Write a block of code using when with guards
  2. run gradle diktatCheck

Environment information

  • diktat version: 2.0.0
  • build tool (maven/gradle): gradle
  • how is diktat run (CLI, plugin, etc.): plugin
  • kotlin version: 2.2.21
  • operating system: MacOS 26.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions