Skip to content

Commit 4f7192f

Browse files
committed
RUM-11445: Check if false alarm still exists
1 parent 61409f2 commit 4f7192f

5 files changed

Lines changed: 77 additions & 14 deletions

File tree

features/dd-sdk-android-trace/src/main/kotlin/com/datadog/android/trace/internal/DatadogPropagationHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,15 @@ class DatadogPropagationHelper internal constructor() {
391391
addHeader(
392392
W3CHttpCodec.TRACE_PARENT_KEY,
393393
// TODO RUM-11445 InvalidStringFormat false alarm
394-
@Suppress("UnsafeThirdPartyFunctionCall", "InvalidStringFormat") // Format string is static
394+
@Suppress("UnsafeThirdPartyFunctionCall") // Format string is static
395395
W3C_TRACE_PARENT_DROP_SAMPLING_DECISION.format(
396396
traceId.padStart(length = W3C_TRACE_ID_LENGTH, padChar = '0'),
397397
spanId.padStart(length = W3C_PARENT_ID_LENGTH, padChar = '0')
398398
)
399399
)
400400
// TODO RUM-2121 3rd party vendor information will be erased
401401
// TODO RUM-11445 InvalidStringFormat false alarm
402-
@Suppress("UnsafeThirdPartyFunctionCall", "InvalidStringFormat") // Format string is static
402+
@Suppress("UnsafeThirdPartyFunctionCall") // Format string is static
403403
var traceStateHeader = W3C_TRACE_STATE_DROP_SAMPLING_DECISION
404404
.format(spanId.padStart(length = W3C_PARENT_ID_LENGTH, padChar = '0'))
405405
if (traceOrigin != null) {

gradle/libs.versions.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ kover = "0.7.6"
6060
kspTesting = "1.5.0"
6161

6262
# Tools
63-
detekt = "1.23.0"
63+
detekt = "1.23.4"
6464
dokka = "2.1.0"
6565
unmock = "0.9.0"
6666
robolectric = "4.4_r1-robolectric-r2"
@@ -209,7 +209,6 @@ androidLintTests = { module = "com.android.tools.lint:lint-tests", version.ref =
209209
systemStubsJupiter = { module = "uk.org.webcompere:system-stubs-jupiter", version.ref = "systemStubsJupiter" }
210210

211211
# Tools
212-
detektCli = { module = "io.gitlab.arturbosch.detekt:detekt-cli", version.ref = "detekt" }
213212
detektApi = { module = "io.gitlab.arturbosch.detekt:detekt-api", version.ref = "detekt" }
214213
detektTest = { module = "io.gitlab.arturbosch.detekt:detekt-test", version.ref = "detekt" }
215214
okHttpMock = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "okHttp" }

integrations/dd-sdk-android-okhttp/src/main/kotlin/com/datadog/android/okhttp/trace/TracingInterceptor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,15 +529,15 @@ internal constructor(
529529
requestBuilder.addHeader(
530530
W3C_TRACEPARENT_KEY,
531531
// TODO RUM-11445 InvalidStringFormat false alarm
532-
@Suppress("UnsafeThirdPartyFunctionCall", "InvalidStringFormat") // Format string is static
532+
@Suppress("UnsafeThirdPartyFunctionCall") // Format string is static
533533
W3C_TRACEPARENT_DROP_SAMPLING_DECISION.format(
534534
traceId.padStart(length = W3C_TRACE_ID_LENGTH, padChar = '0'),
535535
spanId.padStart(length = W3C_PARENT_ID_LENGTH, padChar = '0')
536536
)
537537
)
538538
// TODO RUM-2121 3rd party vendor information will be erased
539539
// TODO RUM-11445 InvalidStringFormat false alarm
540-
@Suppress("UnsafeThirdPartyFunctionCall", "InvalidStringFormat") // Format string is static
540+
@Suppress("UnsafeThirdPartyFunctionCall") // Format string is static
541541
var traceStateHeader = W3C_TRACESTATE_DROP_SAMPLING_DECISION
542542
.format(spanId.padStart(length = W3C_PARENT_ID_LENGTH, padChar = '0'))
543543
if (traceOrigin != null) {

tools/detekt/src/main/kotlin/com/datadog/tools/detekt/rules/sdk/InvalidStringFormat.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import org.jetbrains.kotlin.descriptors.VariableDescriptor
1919
import org.jetbrains.kotlin.psi.KtCallExpression
2020
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
2121
import org.jetbrains.kotlin.psi.KtExpression
22+
import org.jetbrains.kotlin.psi.KtProperty
2223
import org.jetbrains.kotlin.psi.KtReferenceExpression
2324
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
2425
import org.jetbrains.kotlin.psi.psiUtil.children
2526
import org.jetbrains.kotlin.resolve.BindingContext
27+
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
2628
import org.jetbrains.kotlin.resolve.bindingContextUtil.getReferenceTargets
2729
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
2830
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
@@ -137,8 +139,18 @@ class InvalidStringFormat : Rule() {
137139
if (compileTimeInitializer != null) {
138140
compileTimeInitializer.value as? String
139141
} else {
140-
println("Unable to get compileTimeInitializer for $referenceTarget")
141-
null
142+
// compileTimeInitializer can be null when the CLI binding context
143+
// doesn't fully resolve companion object constants. Fall back to
144+
// reading the initializer from the PSI source declaration.
145+
val declaration = DescriptorToSourceUtils
146+
.descriptorToDeclaration(referenceTarget) as? KtProperty
147+
val initializer = declaration?.initializer
148+
if (initializer != null) {
149+
resolveStringExpression(initializer)
150+
} else {
151+
println("Unable to get compileTimeInitializer for $referenceTarget")
152+
null
153+
}
142154
}
143155
} else {
144156
println("Unknown referenceTarget:$referenceTarget")

tools/detekt/src/test/kotlin/com/datadog/tools/detekt/rules/sdk/InvalidStringFormatTest.kt

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal class InvalidStringFormatTest {
3737
fun `Warns unresolved String format {variable, ext}`() {
3838
// Given
3939
val code =
40-
"""
40+
"""
4141
fun test(s: String): String {
4242
var pattern = "Called %s with %s"
4343
return pattern.format(s)
@@ -56,7 +56,7 @@ internal class InvalidStringFormatTest {
5656
fun `Warns unresolved String format {variable, static}`() {
5757
// Given
5858
val code =
59-
"""
59+
"""
6060
fun test(s: String): String {
6161
var pattern = "Called %s with %s"
6262
return String.format(pattern, s)
@@ -75,7 +75,7 @@ internal class InvalidStringFormatTest {
7575
fun `Warns unresolved String format {argument, ext}`() {
7676
// Given
7777
val code =
78-
"""
78+
"""
7979
fun test(pattern: String, s: String): String {
8080
return pattern.format(s)
8181
}
@@ -93,7 +93,7 @@ internal class InvalidStringFormatTest {
9393
fun `Warns unresolved String format {argument, static}`() {
9494
// Given
9595
val code =
96-
"""
96+
"""
9797
fun test(pattern: String, s: String): String {
9898
return String.format(pattern, s)
9999
}
@@ -111,7 +111,7 @@ internal class InvalidStringFormatTest {
111111
fun `Warns unresolved String format {argument with defaults, ext}`() {
112112
// Given
113113
val code =
114-
"""
114+
"""
115115
fun test(pattern: String = "%s", s: String): String {
116116
return pattern.format(s)
117117
}
@@ -129,7 +129,7 @@ internal class InvalidStringFormatTest {
129129
fun `Warns unresolved String format {argument with defaults, static}`() {
130130
// Given
131131
val code =
132-
"""
132+
"""
133133
fun test(pattern: String = "%s", s: String): String {
134134
return String.format(pattern, s)
135135
}
@@ -620,4 +620,56 @@ internal class InvalidStringFormatTest {
620620
}
621621

622622
// endregion
623+
624+
// region Test companion object const val
625+
626+
@Test
627+
fun `Ignores valid format with const val in same class companion object`() {
628+
// Given
629+
val code =
630+
"""
631+
class Foo {
632+
fun test(s: String, i: Int): String {
633+
return PATTERN.format(s, i)
634+
}
635+
636+
companion object {
637+
const val PATTERN = "Called %s with %d"
638+
}
639+
}
640+
""".trimIndent()
641+
642+
// When
643+
val findings = InvalidStringFormat()
644+
.compileAndLintWithContext(kotlinEnv.env, code)
645+
646+
// Then
647+
assertThat(findings).hasSize(0)
648+
}
649+
650+
@Test
651+
fun `Ignores valid format with const val in same class companion object {static call}`() {
652+
// Given
653+
val code =
654+
"""
655+
class Foo {
656+
fun test(traceId: String, spanId: String): String {
657+
return String.format(W3C_PATTERN, traceId, spanId)
658+
}
659+
660+
companion object {
661+
const val W3C_PATTERN = "00-%s-%s-00"
662+
}
663+
}
664+
""".trimIndent()
665+
666+
// When
667+
val findings = InvalidStringFormat()
668+
.compileAndLintWithContext(kotlinEnv.env, code)
669+
670+
// Then
671+
assertThat(findings).hasSize(0)
672+
}
673+
674+
// endregion
623675
}

0 commit comments

Comments
 (0)