Enable UseEmptyCounterpart for detekt code base (#4000)

Co-authored-by: Markus Schwarz <post@markus-schwarz.net>
This commit is contained in:
marschwar
2021-08-03 11:36:31 +02:00
committed by GitHub
parent d3c603e7f5
commit c356897858
22 changed files with 45 additions and 32 deletions

View File

@@ -157,3 +157,5 @@ style:
allowedNames: '(_|ignored|expected)'
UseCheckOrError:
active: true
UseEmptyCounterpart:
active: true

View File

@@ -14,8 +14,8 @@ open class CodeSmell(
final override val issue: Issue,
override val entity: Entity,
override val message: String,
override val metrics: List<Metric> = listOf(),
override val references: List<Entity> = listOf()
override val metrics: List<Metric> = emptyList(),
override val references: List<Entity> = emptyList()
) : Finding {
internal var internalSeverity: SeverityLevel = SeverityLevel.WARNING
@@ -50,8 +50,8 @@ open class CorrectableCodeSmell(
issue: Issue,
entity: Entity,
message: String,
metrics: List<Metric> = listOf(),
references: List<Entity> = listOf(),
metrics: List<Metric> = emptyList(),
references: List<Entity> = emptyList(),
val autoCorrectEnabled: Boolean
) : CodeSmell(
issue,

View File

@@ -38,7 +38,7 @@ class AnnotationExcluderSpec : Spek({
}
it("should not exclude when no annotations should be excluded") {
val excluder = AnnotationExcluder(file, listOf())
val excluder = AnnotationExcluder(file, emptyList())
assertThat(excluder.shouldExclude(listOf(jvmFieldAnnotation))).isFalse()
}

View File

@@ -258,7 +258,7 @@ class ConfigPropertySpec : Spek({
object : TestConfigAware() {
val defaultValue: List<String> = emptyList()
val prop1: List<Int> by config(defaultValue) { it.map(String::toInt) }
val prop2: List<Int> by config(listOf<String>()) { it.map(String::toInt) }
val prop2: List<Int> by config(emptyList<String>()) { it.map(String::toInt) }
}
}
it("can be defined as variable") {

View File

@@ -18,7 +18,7 @@ internal class CliArgsSpec : Spek({
describe("Parsing the input path") {
it("the current working directory is used if parameter is not set") {
val cli = parseArguments(arrayOf())
val cli = parseArguments(emptyArray())
assertThat(cli.inputPaths).hasSize(1)
assertThat(cli.inputPaths.first()).isEqualTo(Paths.get(System.getProperty("user.dir")))
}

View File

@@ -20,7 +20,7 @@ class YamlConfig internal constructor(
) : Config, ValidatableConfiguration {
override fun subConfig(key: String): Config {
val subProperties = properties.getOrElse(key) { mapOf<String, Any>() }
val subProperties = properties.getOrElse(key) { emptyMap<String, Any>() }
return YamlConfig(
subProperties as Map<String, Any>,
if (parentPath == null) key else "$parentPath $CONFIG_SEPARATOR $key"

View File

@@ -23,7 +23,7 @@ class TestProvider(override val ruleSetId: String = "Test") : RuleSetProvider {
class TestProvider2(override val ruleSetId: String = "Test2") : RuleSetProvider {
override fun instance(config: Config): RuleSet {
return RuleSet("Test", listOf())
return RuleSet("Test", emptyList())
}
}

View File

@@ -23,10 +23,20 @@ class YamlConfigSpec : Spek({
it("should create a sub config") {
val subConfig = config.subConfig("style")
assertThat(subConfig.valueOrDefault("WildcardImport", mapOf<String, Any>())).isNotEmpty
assertThat(subConfig.valueOrDefault("WildcardImport", mapOf<String, Any>())["active"].toString()).isEqualTo("true")
assertThat(subConfig.valueOrDefault("WildcardImport", mapOf<String, Any>())["active"] as Boolean).isTrue()
assertThat(subConfig.valueOrDefault("NotFound", mapOf<String, Any>())).isEmpty()
assertThat(subConfig.valueOrDefault("WildcardImport", emptyMap<String, Any>())).isNotEmpty
assertThat(
subConfig.valueOrDefault(
"WildcardImport",
emptyMap<String, Any>()
)["active"].toString()
).isEqualTo("true")
assertThat(
subConfig.valueOrDefault(
"WildcardImport",
emptyMap<String, Any>()
)["active"] as Boolean
).isTrue()
assertThat(subConfig.valueOrDefault("NotFound", emptyMap<String, Any>())).isEmpty()
assertThat(subConfig.valueOrDefault("NotFound", "")).isEmpty()
}

View File

@@ -15,7 +15,7 @@ import org.jetbrains.kotlin.psi.psiUtil.referenceExpression
data class MultiRule(
val name: String,
val rules: List<String> = listOf()
val rules: List<String> = emptyList()
) {
operator fun contains(ruleName: String) = ruleName in this.rules

View File

@@ -10,7 +10,7 @@ data class Rule(
var debt: String,
var aliases: String?,
val parent: String,
val configuration: List<Configuration> = listOf(),
val configuration: List<Configuration> = emptyList(),
val autoCorrect: Boolean = false,
var inMultiRule: String? = null,
val requiresTypeResolution: Boolean = false

View File

@@ -18,8 +18,8 @@ data class RuleSetProvider(
val name: String,
val description: String,
val defaultActivationStatus: DefaultActivationStatus,
val rules: List<String> = listOf(),
val configuration: List<Configuration> = listOf()
val rules: List<String> = emptyList(),
val configuration: List<Configuration> = emptyList()
)
class RuleSetProviderCollector : Collector<RuleSetProvider> {

View File

@@ -14,7 +14,7 @@ val exclusions = arrayOf(TestExclusions, KotlinScriptExclusions, LibraryExclusio
abstract class Exclusions {
abstract val pattern: String
open val ruleSets: Set<String> = setOf()
open val ruleSets: Set<String> = emptySet()
abstract val rules: Set<String>
fun isExcluded(rule: Rule) = rule.name in rules || rule.inMultiRule in rules

View File

@@ -14,7 +14,7 @@ internal class DefaultCliInvokerSpec : Spek({
assertThatCode {
DefaultCliInvoker(stubbedCache)
.invokeCli(listOf(), TestFileCollection(), "detekt", ignoreFailures = false)
.invokeCli(emptyList(), TestFileCollection(), "detekt", ignoreFailures = false)
}.isInstanceOf(GradleException::class.java)
.hasMessageContaining("testing reflection wrapper...")
}

View File

@@ -90,7 +90,7 @@ class CyclomaticComplexitySpec : Spek({
it("does not count when forEach is not specified") {
assertThat(
CyclomaticComplexity.calculate(code) {
nestingFunctions = setOf()
nestingFunctions = emptySet()
}
).isEqualTo(defaultFunctionComplexity)
}

View File

@@ -69,7 +69,7 @@ class LabeledExpression(config: Config = Config.empty) : Rule(config) {
)
@Configuration("allows to provide a list of label names which should be ignored by this rule")
private val ignoredLabels: List<String> by config(listOf<String>()) { list ->
private val ignoredLabels: List<String> by config(emptyList<String>()) { list ->
list.map { it.removePrefix("*").removeSuffix("*") }
}

View File

@@ -68,7 +68,7 @@ class LongParameterList(config: Config = Config.empty) : Rule(config) {
"the most common cases are for dependency injection where constructors are annotated with `@Inject` " +
"or parameters are annotated with `@Value` and should not be counted for the rule to trigger"
)
private val ignoreAnnotated: List<String> by config(listOf<String>()) { list ->
private val ignoreAnnotated: List<String> by config(emptyList<String>()) { list ->
list.map { it.removePrefix("*").removeSuffix("*") }
}

View File

@@ -62,7 +62,7 @@ class UselessPostfixExpression(config: Config = Config.empty) : Rule(config) {
Debt.TWENTY_MINS
)
var properties = setOf<String?>()
var properties = emptySet<String?>()
override fun visitClass(klass: KtClass) {
properties = klass.getProperties()

View File

@@ -26,7 +26,7 @@ class ForbiddenClassName(config: Config = Config.empty) : Rule(config) {
)
@Configuration("forbidden class names")
private val forbiddenName: List<String> by config(listOf<String>()) { names ->
private val forbiddenName: List<String> by config(emptyList<String>()) { names ->
names.map { it.removePrefix("*").removeSuffix("*") }
}

View File

@@ -34,9 +34,9 @@ class QualifiedNameProcessorSpec : Spek({
private val result = object : Detektion {
override val findings: Map<String, List<Finding>> = mapOf()
override val notifications: Collection<Notification> = listOf()
override val metrics: Collection<ProjectMetric> = listOf()
override val findings: Map<String, List<Finding>> = emptyMap()
override val notifications: Collection<Notification> = emptyList()
override val metrics: Collection<ProjectMetric> = emptyList()
private var userData = KeyFMap.EMPTY_MAP
override fun <V> getData(key: Key<V>): V? = userData.get(key)

View File

@@ -28,6 +28,7 @@ class KotlinCoreEnvironmentWrapper(
*
* @param additionalRootPaths the optional JVM classpath roots list.
*/
fun createEnvironment(additionalRootPaths: List<File> = listOf()): KotlinCoreEnvironmentWrapper = KtTestCompiler.createEnvironment(additionalRootPaths)
fun createEnvironment(additionalRootPaths: List<File> = emptyList()): KotlinCoreEnvironmentWrapper =
KtTestCompiler.createEnvironment(additionalRootPaths)
fun createPsiFactory(): KtPsiFactory = KtPsiFactory(KtTestCompiler.project(), false)

View File

@@ -37,7 +37,7 @@ internal object KtTestCompiler : KtCompiler() {
* Not sure why but this function only works from this context.
* Somehow the Kotlin language was not yet initialized.
*/
fun createEnvironment(additionalRootPaths: List<File> = listOf()): KotlinCoreEnvironmentWrapper {
fun createEnvironment(additionalRootPaths: List<File> = emptyList()): KotlinCoreEnvironmentWrapper {
val configuration = CompilerConfiguration()
configuration.put(CommonConfigurationKeys.MODULE_NAME, "test_module")
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)

View File

@@ -14,7 +14,7 @@ internal class PluginsSpec : Spek({
it("throws when both sources are supplied via dsl") {
assertThatCode {
ExtensionsSpecBuilder().apply {
fromPaths { listOf() }
fromPaths { emptyList() }
fromClassloader { javaClass.classLoader }
}.build()
}.isInstanceOf(IllegalArgumentException::class.java)
@@ -22,13 +22,13 @@ internal class PluginsSpec : Spek({
assertThatCode {
ExtensionsSpecBuilder().apply {
fromClassloader { javaClass.classLoader }
fromPaths { listOf() }
fromPaths { emptyList() }
}.build()
}.isInstanceOf(IllegalArgumentException::class.java)
}
it("throws when both sources are supplied via internal helper class") {
assertThatCode { PluginsHolder(listOf(), javaClass.classLoader) }
assertThatCode { PluginsHolder(emptyList(), javaClass.classLoader) }
.isInstanceOf(IllegalArgumentException::class.java)
}