Repository: detekt/detekt (kotlin)

Issue:
`configWithAndroidVariants`must check ruleset config for current android setting
This follows up on an issue identified in the #6767 PR (see [this comment](https://github.com/detekt/detekt/pull/6767#discussion_r1444075312)).

### Context
Some rules in the formatting ruleset (aka. ktlint wrapper) use different default values if the code is part of an android project. The user may configure this setting at the ruleset level.

```yaml
formatting:
  active: true
  android: true
``` 

Prior to #6767 a `Rule` was always instantiated with the rule**set** config and the default function of the `ConfigAware` interface allowed access to the rule config.

### The change
This PR would change the delegated `configWithAndroidVariants` property to use the parent of the rule config to check if the android flag is set. This makes the solution very brittle and dangerous to use in different contexts. Therefor there is another followup task (#6833) to clean this up.

Pull request:
fixes #6838

Patch:
diff --git a/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/ConfigProperty.kt b/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/ConfigProperty.kt
index efda525f38f6..f500ce848787 100644
--- a/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/ConfigProperty.kt
+++ b/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/ConfigProperty.kt
@@ -113,6 +113,7 @@ private fun <T : Any> getValueOrDefault(config: Config, propertyName: String, de
         is String,
         is Boolean,
         is Int -> config.valueOrDefault(propertyName, defaultValue)
+
         else -> error(
             "${defaultValue.javaClass} is not supported for delegated config property '$propertyName'. " +
                 "Use one of String, Boolean, Int or List<String> instead."
@@ -175,7 +176,10 @@ private class TransformedConfigPropertyWithAndroidVariants<T : Any, U : Any>(
     private val transform: (T) -> U
 ) : MemoizedConfigProperty<U>() {
     override fun doGetValue(thisRef: Rule, property: KProperty<*>): U {
-        val isAndroid = getValueOrDefault(thisRef.config, "android", false)
+        val rulesetConfig = requireNotNull(thisRef.config.parent) {
+            "A rule that uses the 'configWithAndroidVariants' property delegate must have a parent config."
+        }
+        val isAndroid = getValueOrDefault(rulesetConfig, "android", false)
         val value = if (isAndroid) defaultAndroidValue else defaultValue
         return transform(getValueOrDefault(thisRef.config, property.name, value))
     }


Test patch:
diff --git a/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/TrailingCommaOnCallSiteSpec.kt b/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/TrailingCommaOnCallSiteSpec.kt
index 80b89b839c54..4182428c43e5 100644
--- a/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/TrailingCommaOnCallSiteSpec.kt
+++ b/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/TrailingCommaOnCallSiteSpec.kt
@@ -94,6 +94,34 @@ class TrailingCommaOnCallSiteSpec {
         }
     }
 
+    @Nested
+    inner class DefaultSettings {
+        val code = """
+            val withoutTrailingComma = listOf(
+                "a", 
+                "b"
+            )
+        """.trimIndent()
+
+        @Test
+        fun `trailing commas are discouraged on android`() {
+            val rulesetConfig = TestConfig("android" to true)
+            val ruleConfig = TestConfig(parent = rulesetConfig)
+
+            val findings = TrailingCommaOnCallSite(ruleConfig).lint(code)
+            assertThat(findings).isEmpty()
+        }
+
+        @Test
+        fun `trailing commas are required on non-android in multiline statements`() {
+            val rulesetConfig = TestConfig("android" to false)
+            val ruleConfig = TestConfig(parent = rulesetConfig)
+
+            val findings = TrailingCommaOnCallSite(ruleConfig).lint(code)
+            assertThat(findings).hasSize(1)
+        }
+    }
+
     private companion object {
         private const val USE_TRAILING_COMMA = "useTrailingCommaOnCallSite"
     }