Skip to content

Commit

Permalink
New warning: Unused imports should be removed
Browse files Browse the repository at this point in the history
### What's done:
* Added new warning
* Added new test
  • Loading branch information
Cheshiriks committed Jan 21, 2021
1 parent 26cea55 commit 3026e13
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.pinterest.ktlint.core.ast.ElementType.ELSE
import com.pinterest.ktlint.core.ast.ElementType.ELSE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.FINALLY
import com.pinterest.ktlint.core.ast.ElementType.FINALLY_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.FOR
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.core.ast.ElementType.IF
Expand All @@ -31,7 +30,6 @@ import com.pinterest.ktlint.core.ast.ElementType.SECONDARY_CONSTRUCTOR
import com.pinterest.ktlint.core.ast.ElementType.THEN
import com.pinterest.ktlint.core.ast.ElementType.TRY
import com.pinterest.ktlint.core.ast.ElementType.WHEN
import com.pinterest.ktlint.core.ast.ElementType.WHILE
import com.pinterest.ktlint.core.ast.ElementType.WHILE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_IN_NON_SINGLE_C
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_NOT_BEFORE_PACKAGE
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_WRONG_FORMAT
import org.cqfn.diktat.ruleset.constants.Warnings.KDOC_CONTAINS_DATE_OR_AUTHOR
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_COPYRIGHT_YEAR
import org.cqfn.diktat.ruleset.utils.copyrightWords
import org.cqfn.diktat.ruleset.utils.findChildAfter
import org.cqfn.diktat.ruleset.utils.findChildBefore
import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
import org.cqfn.diktat.ruleset.utils.kDocTags
import org.cqfn.diktat.ruleset.utils.log
import org.cqfn.diktat.ruleset.utils.moveChildBefore

Expand All @@ -31,12 +29,8 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag
import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoField

/**
* Visitor for header comment in .kt file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
}
}

@Suppress("UnsafeCallOnNullableType")
private fun checkUnusedImport(
node: ASTNode,
unusedImportConfig: UnusedImportConfig
Expand All @@ -224,9 +225,9 @@ class FileStructureRule(private val configRules: List<RulesConfig>) : Rule("file
?.forEach { import ->
val ktImportDirective = import.psi as KtImportDirective
val importName = ktImportDirective.importPath?.importedName?.asString()
val importPath = ktImportDirective.importPath?.pathStr
val importPath = ktImportDirective.importPath?.pathStr!! // importPath - ifNOtParsed & Nullable
if (ktImportDirective.aliasName == null &&
packageName.isNotEmpty() && importPath!!.startsWith("$packageName.") &&
packageName.isNotEmpty() && importPath.startsWith("$packageName.") &&
importPath.substring(packageName.length + 1).indexOf('.') == -1
) {
if (unusedImportConfig.deleteUnusedImport) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import com.pinterest.ktlint.core.ast.ElementType.WHILE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.isWhiteSpace
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithoutNewline
import com.pinterest.ktlint.core.ast.nextCodeLeaf
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.prevSibling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
fixAndCompare("Example2Expected.kt", "Example2Test.kt")
unfixedLintErrors.assertEquals(
LintError(1, 1, "$DIKTAT_RULE_SET_ID:header-comment", "${HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE.warnText()} there are 2 declared classes and/or objects", false),
LintError(14, 17, "$DIKTAT_RULE_SET_ID:comments", "${Warnings.COMMENTED_OUT_CODE.warnText()} private class Test : RuntimeException()", false)
LintError(12, 26, "$DIKTAT_RULE_SET_ID:comments", "${Warnings.COMMENTED_OUT_CODE.warnText()} private class Test : RuntimeException()", false)
)
}

Expand Down
3 changes: 0 additions & 3 deletions diktat-rules/src/test/resources/test/smoke/build.gradle_.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import org.cqfn.diktat.generation.docs.generateAvailableRules
import org.cqfn.diktat.generation.docs.generateCodeStyle
import org.cqfn.diktat.generation.docs.generateFullDoc
import org.cqfn.diktat.generation.docs.generateRulesMapping

plugins {
kotlin("jvm") version "1.4.21"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import org.slf4j.LoggerFactory
import java.io.IOException
import java.util.Properties

import kotlin.system.exitProcess

/**
* @property foo
* @property bar
Expand All @@ -32,5 +30,7 @@ private fun foo(node: ASTNode) {
if (x) {
foo()
}

setOf<Object>(IOException(), Properties(), LoggerFactory())
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ private fun foo (node: ASTNode) {

if (x) // comment
foo()

setOf<Object>(IOException(), Properties(), LoggerFactory())
}

0 comments on commit 3026e13

Please sign in to comment.