Repository: detekt/detekt (kotlin)

Issue:
java.lang.IllegalStateException in random cases
<!-- The bug you're experiencing might have already be reported! -->
<!-- Please search in the [issues](https://github.com/detekt/detekt/issues) before creating one. -->

## Expected Behavior
Detekt task should execute successfully


## Observed Behavior
Detekt task failing with
```
java.lang.IllegalStateException: Analyzing ../../../CodeFileLocation  led to an exception.
  Location: java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:314)
  The original exception message was: java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
  Running detekt '1.23.0' on Java '11.0.20+9-LTS-256' on OS 'Mac OS X'
  If the exception message does not help, please feel free to create an issue on our GitHub page.
```

```kotlin
fun DialogFragment.closeSoftKeyboard() {
    view?.let { view ->
        val inputMethodManager = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    }
}
```

`inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)`  failing at this line

same behavior for 
```kotlin
private val endDateOnDateSetListener
        get() = DatePickerDialog.OnDateSetListener { _, year: Int, month: Int, dayOfMonth: Int ->
            val updatedMonth = month + 1
        }
```

here when trying to add int with other int value  build failing with same error message `val updatedMonth = month + 1`

## Steps to Reproduce
<!-- Provide a link to a live example, or an unambiguous set of steps to -->
<!-- reproduce this bug. Include code to reproduce, if relevant -->

## Context
<!-- How has this issue affected you? What are you trying to accomplish? -->
<!-- Providing context helps us come up with a solution that is most useful in the real world -->

## Your Environment
<!-- Include as many relevant details about the environment you experienced the bug in -->
* Version of detekt used: 1.23.0
* Version of Gradle used (if applicable):gradle-7.5-bin
* Gradle scan link (add `--scan` option when running the gradle task):
* Operating System and version: MacOS 14.2.1 (23C71)
* Link to your project (if it's a public repository):

Edited by Brais: Fix format

Pull request:
fixes #6818

This makes sure that a configured list contains strings only or fails with a meaningful error message.

Patch:
diff --git a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/BaseConfig.kt b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/BaseConfig.kt
index 7edaf0d2dde6..5a5bf3caddbf 100644
--- a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/BaseConfig.kt
+++ b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/BaseConfig.kt
@@ -19,8 +19,20 @@ fun Config.valueOrDefaultInternal(
         if (result != null) {
             when {
                 result is String -> parser(result, default)
+                result is List<*> -> {
+                    if (default !is List<*>) {
+                        throw ClassCastException()
+                    }
+                    check(result.all { it is String }) {
+                        "Only lists of strings are supported. Value \"$result\" set " +
+                            "for config parameter \"${keySequence(key)}\" contains non-string values."
+                    }
+                    result.map { it as String }
+                }
+
                 default::class in PRIMITIVES &&
                     result::class != default::class -> throw ClassCastException()
+
                 else -> result
             }
         } else {
@@ -47,6 +59,7 @@ fun tryParseBasedOnDefault(result: String, defaultResult: Any): Any = when (defa
         } else {
             throw ClassCastException()
         }
+
     is Double -> result.toDouble()
     is String -> result
     else -> throw ClassCastException()


Test patch:
diff --git a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/config/YamlConfigSpec.kt b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/config/YamlConfigSpec.kt
index e4ca53e25283..e4d1a211afa5 100644
--- a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/config/YamlConfigSpec.kt
+++ b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/config/YamlConfigSpec.kt
@@ -127,6 +127,16 @@ class YamlConfigSpec {
                     "Value \"[]\" set for config parameter \"RuleSet > Rule > active\" is not of required type Int."
                 )
         }
+
+        @Test
+        fun `prints meaningful message when list of ints is used instead of list of strings`() {
+            assertThatIllegalStateException().isThrownBy {
+                config.valueOrDefaultInternal(key = "key", result = listOf(1, 2), default = listOf("1", "2"))
+            }.withMessage(
+                "Only lists of strings are supported. " +
+                    "Value \"[1, 2]\" set for config parameter \"key\" contains non-string values."
+            )
+        }
     }
 
     @Nested
diff --git a/detekt-core/src/test/resources/wrong-property-type.yml b/detekt-core/src/test/resources/wrong-property-type.yml
index 657fe9a3fda2..599dda146d34 100644
--- a/detekt-core/src/test/resources/wrong-property-type.yml
+++ b/detekt-core/src/test/resources/wrong-property-type.yml
@@ -2,5 +2,6 @@ RuleSet:
   Rule:
     active: []
     threshold: v5.7
+    strings: [ 0, 99 ]
 
 bool: fasle