Skip to content

Commit

Permalink
Merge branch 'master' into feature/magic-number-not-work-in-test
Browse files Browse the repository at this point in the history
  • Loading branch information
kentr0w authored May 12, 2021
2 parents 55e28dc + fd9b54f commit 5ca01f2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.cqfn.diktat.ruleset.rules.chapter1.PackageNaming

import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.ANNOTATED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.CONST_KEYWORD
Expand Down Expand Up @@ -503,7 +504,7 @@ fun ASTNode?.isAccessibleOutside(): Boolean =
*/
fun ASTNode.hasSuppress(warningName: String) = parent({ node ->
val annotationNode = if (node.elementType != FILE) {
node.findChildByType(MODIFIER_LIST)
node.findChildByType(MODIFIER_LIST) ?: node.findChildByType(ANNOTATED_EXPRESSION)
} else {
node.findChildByType(FILE_ANNOTATION_LIST)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.cqfn.diktat.ruleset.utils

import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.ruleset.rules.chapter3.CollapseIfStatementsRule
import org.cqfn.diktat.util.LintTestBase

import com.pinterest.ktlint.core.LintError
import org.junit.jupiter.api.Test

class SuppressAnnotatedExpressionTest : LintTestBase(::CollapseIfStatementsRule) {
private val ruleId: String = "$DIKTAT_RULE_SET_ID:collapse-if"

@Test
fun `should lint errors without suppress`() {
val code =
"""
|fun foo() {
| if (true) {
| if (true) {
| if (true) {
|
| }
| }
| }
|}
""".trimMargin()
lintMethod(code,
LintError(3, 8, ruleId, "${Warnings.COLLAPSE_IF_STATEMENTS.warnText()} avoid using redundant nested if-statements", true),
LintError(4, 12, ruleId, "${Warnings.COLLAPSE_IF_STATEMENTS.warnText()} avoid using redundant nested if-statements", true)
)
}

@Test
fun `should suppress annotated expressions`() {
val code =
"""
|fun foo() {
| if (true) {
| @Suppress("COLLAPSE_IF_STATEMENTS")
| if (true) {
| if (true) {
|
| }
| }
| }
|}
""".trimMargin()
lintMethod(code)
}
}

0 comments on commit 5ca01f2

Please sign in to comment.