From 6266153f1c5786c7f0a59e627bb564a6e3d62b16 Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Mon, 30 Jan 2023 14:11:11 +0300 Subject: [PATCH 1/5] Fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface ### What's done: * fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface Closes #1598 --- .../rules/chapter3/MultipleModifiersSequence.kt | 14 ++++++++++++++ .../multiple_modifiers/ModifierExpected.kt | 4 ++++ .../paragraph3/multiple_modifiers/ModifierTest.kt | 4 ++++ 3 files changed, 22 insertions(+) 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..b67589d8aa 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 @@ -5,11 +5,13 @@ import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_MULTIPLE_MODIFIERS_ORDER import org.cqfn.diktat.ruleset.rules.DiktatRule import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY +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 +33,9 @@ class MultipleModifiersSequence(configRules: List) : 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 +51,15 @@ class MultipleModifiersSequence(configRules: List) : 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 + } else { + false + } + } + private fun checkAnnotation(node: ASTNode) { val firstModifierIndex = node .children() 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 } + +public internal fun interface Factory { + public fun create(): List +} 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 } + +internal public fun interface Factory { + public fun create(): List +} From bca7baa48d0bd89a4accb748138622ad7724c9b7 Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Mon, 30 Jan 2023 15:02:33 +0300 Subject: [PATCH 2/5] Fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface ### What's done: * fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface Closes #1598 --- .../cqfn/diktat/ruleset/chapter2/HeaderCommentRuleTest.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/HeaderCommentRuleTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/HeaderCommentRuleTest.kt index 1b030758ea..e96b4b614d 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/HeaderCommentRuleTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter2/HeaderCommentRuleTest.kt @@ -221,7 +221,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) { lintMethod( """ /* - * Copyright (c) 2022 My Company, Ltd. All rights reserved. + * Copyright (c) 2023 My Company, Ltd. All rights reserved. */ /** * Very useful description, why this file has two classes @@ -244,7 +244,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) { lintMethod( """ /* - * Copyright (c) My Company, Ltd. 2012-2022. All rights reserved. + * Copyright (c) My Company, Ltd. 2012-2023. All rights reserved. */ /** * Very useful description, why this file has two classes @@ -267,7 +267,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) { lintMethod( """ /* - Copyright (c) My Company, Ltd. 2021-2022. All rights reserved. + Copyright (c) My Company, Ltd. 2021-2023. All rights reserved. */ /** * Very useful description, why this file has two classes @@ -290,7 +290,7 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) { lintMethod( """ /* - * Copyright (c) My Company, Ltd. 2002-2022. All rights reserved. + * Copyright (c) My Company, Ltd. 2002-2023. All rights reserved. */ /** * Very useful description, why this file has two classes From 58807ee99da5af25c7cfe0eb9ee5bc8d8e16d998 Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Mon, 30 Jan 2023 15:08:16 +0300 Subject: [PATCH 3/5] Fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface ### What's done: * fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface Closes #1598 --- .../resources/test/smoke/src/main/kotlin/Example5Expected.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt index baa4d420ed..9a5929c2ca 100644 --- a/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt +++ b/diktat-ruleset/src/test/resources/test/smoke/src/main/kotlin/Example5Expected.kt @@ -1,6 +1,6 @@ // ;warn:$line:1: [FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: Example5Expected.kt vs Some{{.*}} /* - Copyright 2018-2022 John Doe. + Copyright 2018-2023 John Doe. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at From 3c43463692edff32a8d07eaff797e845a8921ef0 Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Mon, 30 Jan 2023 15:29:58 +0300 Subject: [PATCH 4/5] Fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface ### What's done: * fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface Closes #1598 --- .../test/paragraph2/header/AutoCopyrightApplyPatternExpected.kt | 2 +- .../resources/test/paragraph2/header/AutoCopyrightExpected.kt | 2 +- .../paragraph2/header/CopyrightAbsentInvalidPatternExpected.kt | 2 +- .../test/paragraph2/header/CopyrightDifferentYearExpected.kt | 2 +- .../header/CopyrightInvalidPatternValidCodeExpected.kt | 2 +- .../paragraph2/header/CopyrightInvalidPatternValidCodeTest.kt | 2 +- .../header/MisplacedHeaderKdocAppendedCopyrightExpected.kt | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/diktat-rules/src/test/resources/test/paragraph2/header/AutoCopyrightApplyPatternExpected.kt b/diktat-rules/src/test/resources/test/paragraph2/header/AutoCopyrightApplyPatternExpected.kt index 55b85e7f45..ca03365b06 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/header/AutoCopyrightApplyPatternExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/header/AutoCopyrightApplyPatternExpected.kt @@ -1,5 +1,5 @@ /* - Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved. + Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved. */ package test.paragraph2.header diff --git a/diktat-rules/src/test/resources/test/paragraph2/header/AutoCopyrightExpected.kt b/diktat-rules/src/test/resources/test/paragraph2/header/AutoCopyrightExpected.kt index 55b85e7f45..ca03365b06 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/header/AutoCopyrightExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/header/AutoCopyrightExpected.kt @@ -1,5 +1,5 @@ /* - Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved. + Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved. */ package test.paragraph2.header diff --git a/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightAbsentInvalidPatternExpected.kt b/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightAbsentInvalidPatternExpected.kt index a74048af2b..1cb0fb58c5 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightAbsentInvalidPatternExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightAbsentInvalidPatternExpected.kt @@ -1,5 +1,5 @@ /* - Copyright (c) My Company., Ltd. 2012-2022. All rights reserved. + Copyright (c) My Company., Ltd. 2012-2023. All rights reserved. */ /** * Lorem ipsum diff --git a/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightDifferentYearExpected.kt b/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightDifferentYearExpected.kt index a74048af2b..1cb0fb58c5 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightDifferentYearExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightDifferentYearExpected.kt @@ -1,5 +1,5 @@ /* - Copyright (c) My Company., Ltd. 2012-2022. All rights reserved. + Copyright (c) My Company., Ltd. 2012-2023. All rights reserved. */ /** * Lorem ipsum diff --git a/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightInvalidPatternValidCodeExpected.kt b/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightInvalidPatternValidCodeExpected.kt index ed21f8ab19..1fba87826c 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightInvalidPatternValidCodeExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightInvalidPatternValidCodeExpected.kt @@ -1,5 +1,5 @@ /* - Copyright (c) My Company., Ltd. 2021-2022. All rights reserved. + Copyright (c) My Company., Ltd. 2021-2023. All rights reserved. */ /** * Lorem ipsum diff --git a/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightInvalidPatternValidCodeTest.kt b/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightInvalidPatternValidCodeTest.kt index ed21f8ab19..1fba87826c 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightInvalidPatternValidCodeTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/header/CopyrightInvalidPatternValidCodeTest.kt @@ -1,5 +1,5 @@ /* - Copyright (c) My Company., Ltd. 2021-2022. All rights reserved. + Copyright (c) My Company., Ltd. 2021-2023. All rights reserved. */ /** * Lorem ipsum diff --git a/diktat-rules/src/test/resources/test/paragraph2/header/MisplacedHeaderKdocAppendedCopyrightExpected.kt b/diktat-rules/src/test/resources/test/paragraph2/header/MisplacedHeaderKdocAppendedCopyrightExpected.kt index 36487e56c7..3e3c35295f 100644 --- a/diktat-rules/src/test/resources/test/paragraph2/header/MisplacedHeaderKdocAppendedCopyrightExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph2/header/MisplacedHeaderKdocAppendedCopyrightExpected.kt @@ -1,5 +1,5 @@ /* - Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved. + Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved. */ /** * Lorem ipsum From 29618d82287795414b92c3b041db1e088926389a Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Mon, 30 Jan 2023 15:50:13 +0300 Subject: [PATCH 5/5] Fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface ### What's done: * fixed WRONG_MULTIPLE_MODIFIERS_ORDER bug in SAM interface Closes #1598 --- .../ruleset/rules/chapter3/MultipleModifiersSequence.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 b67589d8aa..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,8 +3,10 @@ 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 @@ -54,7 +56,7 @@ class MultipleModifiersSequence(configRules: List) : 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 + (parentPsi.isInterface()) && node.elementType == FUN_KEYWORD && parent.treeParent.findAllDescendantsWithSpecificType(FUN).size == 1 } else { false }