diff --git a/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/RuleExecutionContext.kt b/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/RuleExecutionContext.kt index 0db529371c..7e6564d345 100644 --- a/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/RuleExecutionContext.kt +++ b/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/RuleExecutionContext.kt @@ -103,7 +103,7 @@ internal class RuleExecutionContext private constructor( } else { executeRuleWithoutAutocorrectApproveHandlerOnNodeRecursively(node, rule, autocorrectHandler, emitAndApprove) } - } catch (e: Exception) { + } catch (e: Throwable) { if (autocorrectHandler is NoneAutocorrectHandler) { val (line, col) = positionInTextLocator(node.startOffset) throw RuleExecutionException( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt index 80ae9adaf2..b1058b77da 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt @@ -258,7 +258,7 @@ public class TrailingCommaOnDeclarationSiteRule : val trailingCommaNode = prevLeaf?.findPreviousTrailingCommaNodeOrNull() val trailingCommaState = when { - hasWhenGuard() -> { + hasWhenEntryGuard() -> { // The compiler won't allow any comma in the when-entry in case it contains a guard clause TrailingCommaState.NOT_EXISTS } @@ -444,7 +444,16 @@ public class TrailingCommaOnDeclarationSiteRule : return codeLeaf?.takeIf { it.elementType == COMMA } } - private fun ASTNode.hasWhenGuard() = elementType == WHEN_ENTRY && children().any { it.elementType == WHEN_ENTRY_GUARD } + private fun ASTNode.hasWhenEntryGuard() = elementType == WHEN_ENTRY && hasWhenEntryGuardKotlin21() + + private fun ASTNode.hasWhenEntryGuardKotlin21(): Boolean = + // TODO: Remove try-catch wrapping once kotlin version is upgraded to 2.1 or above + try { + children().any { it.elementType == WHEN_ENTRY_GUARD } + } catch (e: NoSuchFieldError) { + // Prior to Kotlin 2.1 the WHEN_ENTRY_GUARD can not be retrieved successfully. + false + } private fun containsLineBreakInLeavesRange( from: PsiElement,