From a43bc03f7d04efbd6a3a1d4f9d4e53cd41874dd2 Mon Sep 17 00:00:00 2001 From: Dominic Zirbel Date: Thu, 26 May 2022 12:26:42 -0700 Subject: [PATCH] UnnecessaryInnerClass: add test for safe qualified expressions Add a unit test for a bug in which safe qualified references to a nullable field in the parent class (i.e. as `?.`) would not be marked as warnings in release 1.20.0. This issue has been inadvertently fixed by https://github.com/detekt/detekt/pull/4738, but adding a unit test will guard against future regressions. I was able to confirm this test fails by checking out the 1.20.0 tag and running the unit test; against that tag the fix was to override the visitSafeQualifiedExpression() function with the same logic as visitCallExpression(). --- .../rules/style/UnnecessaryInnerClassSpec.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInnerClassSpec.kt b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInnerClassSpec.kt index a20068ddcf4..e3f6d02cc6a 100644 --- a/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInnerClassSpec.kt +++ b/detekt-rules-style/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/style/UnnecessaryInnerClassSpec.kt @@ -274,6 +274,23 @@ class UnnecessaryInnerClassSpec(val env: KotlinCoreEnvironment) { assertThat(subject.lintWithContext(env, code)).isEmpty() } + @Test + fun `as a safe qualified expression`() { + val code = """ + class A { + var foo: String? = null + + inner class B { + fun fooLength() { + foo?.length + } + } + } + """.trimIndent() + + assertThat(subject.lintWithContext(env, code)).isEmpty() + } + @Test fun `to call a function of the member`() { val code = """