Repository: analysis-dev/diktat (kotlin)

Issue:
WRONG_MULTIPLE_MODIFIERS_ORDER triggers on SAM interface
## Describe the bug

Having the following valid code snippet:
```kotlin
public fun interface Factory {
    public fun create(): List<Int>
}
```

Diktat fails with the error:
```
[WRONG_MULTIPLE_MODIFIERS_ORDER] sequence of modifier-keywords is incorrect: public should be on position 2, but is on position 1 (diktat-ruleset:multiple-modifiers)
[WRONG_MULTIPLE_MODIFIERS_ORDER] sequence of modifier-keywords is incorrect: fun should be on position 1, but is on position 2 (diktat-ruleset:multiple-modifiers
```

## Expected behavior
`public fun interface` should not trigger this rule


## Environment information
* diktat version: diktat-1.2.4.2.jar
* how is diktat run (CLI, plugin, etc.): CLI
* kotlin version: 1.8.0

Pull request:
### What's done:
* fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface

Closes #1598

Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MultipleModifiersSequence.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MultipleModifiersSequence.kt
index f132c8baa2..da78ecb787 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MultipleModifiersSequence.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MultipleModifiersSequence.kt
@@ -3,13 +3,17 @@ package org.cqfn.diktat.ruleset.rules.chapter3
 import org.cqfn.diktat.common.config.rules.RulesConfig
 import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_MULTIPLE_MODIFIERS_ORDER
 import org.cqfn.diktat.ruleset.rules.DiktatRule
+import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType
 
 import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY
+import com.pinterest.ktlint.core.ast.ElementType.FUN
+import com.pinterest.ktlint.core.ast.ElementType.FUN_KEYWORD
 import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
 import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
 import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
 import org.jetbrains.kotlin.lexer.KtTokens
+import org.jetbrains.kotlin.psi.KtClass
 import org.jetbrains.kotlin.psi.psiUtil.children
 
 /**
@@ -31,6 +35,9 @@ class MultipleModifiersSequence(configRules: List<RulesConfig>) : DiktatRule(
         val modifierListOfPair = node
             .getChildren(KtTokens.MODIFIER_KEYWORDS)
             .toList()
+            .filter {
+                !isSamInterfaces(node, it)
+            }
             .map { Pair(it, modifierOrder.indexOf(it.elementType)) }
         val sortModifierListOfPair = modifierListOfPair.sortedBy { it.second }.map { it.first }
         modifierListOfPair.forEachIndexed { index, (modifierNode, _) ->
@@ -46,6 +53,15 @@ class MultipleModifiersSequence(configRules: List<RulesConfig>) : DiktatRule(
         }
     }
 
+    private fun isSamInterfaces(parent: ASTNode, node: ASTNode): Boolean {
+        val parentPsi = parent.treeParent.psi
+        return if (parentPsi is KtClass) {
+            (parentPsi.isInterface()) && node.elementType == FUN_KEYWORD && parent.treeParent.findAllDescendantsWithSpecificType(FUN).size == 1
+        } else {
+            false
+        }
+    }
+
     private fun checkAnnotation(node: ASTNode) {
         val firstModifierIndex = node
             .children()


Test patch:
diff --git a/diktat-rules/src/test/resources/test/paragraph3/multiple_modifiers/ModifierExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/multiple_modifiers/ModifierExpected.kt
index 776b9ed4c3..0e7f8c8029 100644
--- a/diktat-rules/src/test/resources/test/paragraph3/multiple_modifiers/ModifierExpected.kt
+++ b/diktat-rules/src/test/resources/test/paragraph3/multiple_modifiers/ModifierExpected.kt
@@ -11,3 +11,7 @@ protected data class Counter(val dayIndex: Int) {
 public final fun foo() {
     protected open lateinit var a: List<ASTNode>
 }
+
+public internal fun interface Factory {
+    public fun create(): List<Int>
+}
diff --git a/diktat-rules/src/test/resources/test/paragraph3/multiple_modifiers/ModifierTest.kt b/diktat-rules/src/test/resources/test/paragraph3/multiple_modifiers/ModifierTest.kt
index ba502915f8..ec902aa8e1 100644
--- a/diktat-rules/src/test/resources/test/paragraph3/multiple_modifiers/ModifierTest.kt
+++ b/diktat-rules/src/test/resources/test/paragraph3/multiple_modifiers/ModifierTest.kt
@@ -11,3 +11,7 @@ data protected class Counter(val dayIndex: Int) {
 final public fun foo() {
     lateinit open protected var a: List<ASTNode>
 }
+
+internal public fun interface Factory {
+    public fun create(): List<Int>
+}