Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix. Not all types of nodes for annotation Suppress are considered #878

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -489,7 +490,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)
}
}