Skip to content

Commit c8db7d4

Browse files
committed
ignore suppress/noinspection names which don't fit the FindingName pattern
fixes #827
1 parent 714b679 commit c8db7d4

7 files changed

Lines changed: 84 additions & 13 deletions

File tree

modulecheck-finding/name/src/main/kotlin/modulecheck/finding/FindingName.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -52,6 +52,18 @@ data class FindingName(
5252

5353
companion object {
5454

55+
/**
56+
* @return a [FindingName] if [maybeFindingName] is `kebab-case`, otherwise `null`.
57+
* @since 0.12.4
58+
*/
59+
fun safe(maybeFindingName: String): FindingName? {
60+
return if (CaseMatcher.KebabCaseMatcher().matches(maybeFindingName)) {
61+
FindingName(maybeFindingName)
62+
} else {
63+
null
64+
}
65+
}
66+
5567
@Deprecated("This will be removed soon.")
5668
fun migrateLegacyIdOrNull(legacyID: String, logger: McLogger): String? {
5769

modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractDependenciesBlock.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -33,7 +33,6 @@ import modulecheck.parsing.gradle.model.ProjectPath.StringProjectPath
3333
import modulecheck.reporting.logging.McLogger
3434
import modulecheck.utils.lazy.ResetManager
3535
import modulecheck.utils.lazy.lazyResets
36-
import modulecheck.utils.mapToSet
3736
import modulecheck.utils.remove
3837

3938
abstract class AbstractDependenciesBlock(
@@ -53,7 +52,9 @@ abstract class AbstractDependenciesBlock(
5352
allModuleDeclarations.forEach { (configuredModule, declarations) ->
5453

5554
val cached = getOrPut(configuredModule) {
56-
blockSuppressed.mapTo(mutableSetOf()) { FindingName(it) }
55+
blockSuppressed
56+
.mapNotNull { FindingName.safe(it) }
57+
.mapTo(mutableSetOf()) { it }
5758
}
5859

5960
declarations.forEach { moduleDependencyDeclaration ->
@@ -170,7 +171,7 @@ abstract class AbstractDependenciesBlock(
170171
}
171172

172173
private fun Collection<String>.asFindingNames(): Set<FindingName> {
173-
return mapToSet { FindingName(it) }
174+
return mapNotNull { FindingName.safe(it) }.toSet()
174175
}
175176

176177
override fun getOrEmpty(

modulecheck-parsing/gradle/dsl/internal/src/main/kotlin/modulecheck/parsing/gradle/dsl/internal/AbstractPluginsBlock.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -21,7 +21,6 @@ import modulecheck.parsing.gradle.dsl.PluginsBlock
2121
import modulecheck.reporting.logging.McLogger
2222
import modulecheck.utils.lazy.ResetManager
2323
import modulecheck.utils.lazy.lazyResets
24-
import modulecheck.utils.mapToSet
2524
import java.io.File
2625

2726
abstract class AbstractPluginsBlock(
@@ -48,7 +47,7 @@ abstract class AbstractPluginsBlock(
4847
_allDeclarations.forEach { pluginDeclaration ->
4948

5049
val cached = getOrPut(pluginDeclaration) {
51-
blockSuppressed.mapTo(mutableSetOf()) { FindingName(it) }
50+
blockSuppressed.mapNotNullTo(mutableSetOf()) { FindingName.safe(it) }
5251
}
5352

5453
cached += pluginDeclaration.suppressed.updateOldSuppresses()
@@ -101,7 +100,7 @@ abstract class AbstractPluginsBlock(
101100
}
102101

103102
private fun Collection<String>.asFindingNames(): Set<FindingName> {
104-
return mapToSet { FindingName(it) }
103+
return mapNotNullTo(mutableSetOf()) { FindingName.safe(it) }
105104
}
106105
}
107106

modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyDependencyBlockParserTest.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -80,6 +80,20 @@ internal class GroovyDependenciesBlockParserTest : BaseTest() {
8080
)
8181
}
8282

83+
@Test
84+
fun `suppression which doesn't match finding name regex should be ignored`() = parse(
85+
"""
86+
//noinspection DSL_SCOPE_VIOLATION
87+
dependencies {
88+
api project(':core:android')
89+
api project(':core:jvm')
90+
}
91+
"""
92+
) {
93+
94+
allSuppressions.values.flatten() shouldBe emptyList()
95+
}
96+
8397
@Test
8498
fun `declaration with noinspection with old IDs should include suppressed with argument`() =
8599
parse(

modulecheck-parsing/groovy-antlr/src/test/kotlin/modulecheck/parsing/groovy/antlr/GroovyPluginsBlockParserTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -119,6 +119,19 @@ internal class GroovyPluginsBlockParserTest : BaseTest() {
119119
)
120120
}
121121

122+
@Test
123+
fun `suppression which doesn't match finding name regex should be ignored`() = parse(
124+
"""
125+
//noinspection DSL_SCOPE_VIOLATION
126+
plugins {
127+
id 'com.squareup.anvil'
128+
}
129+
"""
130+
) {
131+
132+
allSuppressions.values.flatten() shouldBe emptyList()
133+
}
134+
122135
@Test
123136
fun `suppress with old ID should be updated`() = parse(
124137
"""

modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinDependenciesBlockParserTest.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -111,6 +111,24 @@ internal class KotlinDependenciesBlockParserTest : ProjectTest() {
111111
)
112112
}
113113

114+
@Test
115+
fun `suppression which doesn't match finding name regex should be ignored`() {
116+
val block = parser
117+
.parse(
118+
"""
119+
@Suppress("DSL_SCOPE_VIOLATION")
120+
dependencies {
121+
api(project(":core:android"))
122+
api(project(":core:jvm"))
123+
@Suppress("DSL_SCOPE_VIOLATION")
124+
testImplementation(project(":core:test"))
125+
}
126+
"""
127+
).single()
128+
129+
block.allSuppressions.values.flatten() shouldBe emptyList()
130+
}
131+
114132
@Test
115133
fun `declaration with annotation should include annotation with argument`() {
116134
val block = parser

modulecheck-parsing/psi/src/test/kotlin/modulecheck/parsing/psi/KotlinPluginsBlockParserTest.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -104,6 +104,20 @@ internal class KotlinPluginsBlockParserTest : BaseTest() {
104104
)
105105
}
106106

107+
@Test
108+
fun `suppression which doesn't match finding name regex should be ignored`() {
109+
val block = parse(
110+
"""
111+
@Suppress("DSL_SCOPE_VIOLATION")
112+
plugins {
113+
id("com.squareup.anvil")
114+
}
115+
"""
116+
)
117+
118+
block.allSuppressions.values.flatten() shouldBe emptyList()
119+
}
120+
107121
@Test
108122
fun `suppressed id function`() {
109123
val block = parse(

0 commit comments

Comments
 (0)