Skip to content

Commit

Permalink
SONARKT-286 Add issue suppression mappings for S6619 (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
johann-beleites-sonarsource committed Nov 28, 2023
1 parent 54506e1 commit 3e82ecd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,19 @@ private fun S1479(values: List<Int>): List<String> {
}
}
}

private fun UselessNullChecks(foo: String) {
@Suppress("kotlin:S6619")
foo ?: "bar"

@Suppress("UNNECESSARY_SAFE_CALL")
foo?.plus("bar")

@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
foo != null

@Suppress("USELESS_ELVIS")
foo ?: "bar"

foo ?: "bar" // Noncompliant
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ private val COMPILER_KEY_TO_SONAR_KEYS = mapOf(
"ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE" to sequenceOf("kotlin:S6615"),
"UNUSED_VALUE" to sequenceOf("kotlin:S6615"),
"UNUSED_CHANGED_VALUE" to sequenceOf("kotlin:S6615"),
"UNNECESSARY_SAFE_CALL" to sequenceOf("kotlin:S6619"),
"UNNECESSARY_NOT_NULL_ASSERTION" to sequenceOf("kotlin:S6619"),
"USELESS_ELVIS" to sequenceOf("kotlin:S6619"),
"USELESS_NULLABLE_CHECK" to sequenceOf("kotlin:S6619"),
)

class IssueSuppressionVisitor : KotlinFileVisitor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.sonarsource.kotlin.checks.BadFunctionNameCheck
import org.sonarsource.kotlin.checks.DeprecatedCodeUsedCheck
import org.sonarsource.kotlin.checks.TooManyCasesCheck
import org.sonarsource.kotlin.checks.UnusedLocalVariableCheck
import org.sonarsource.kotlin.checks.UselessNullCheckCheck
import org.sonarsource.kotlin.checks.VariableAndParameterNameCheck
import org.sonarsource.kotlin.testapi.DEFAULT_KOTLIN_CLASSPATH
import org.sonarsource.kotlin.testapi.KOTLIN_BASE_DIR
Expand All @@ -54,16 +55,36 @@ class IssueSuppressionVisitorTest {
}

private fun scanWithSuppression(path: Path) =
scanFile(path, true, BadClassNameCheck(), BadFunctionNameCheck(), VariableAndParameterNameCheck(), UnusedLocalVariableCheck(), TooManyCasesCheck(), DeprecatedCodeUsedCheck())
scanFile(
path,
true,
BadClassNameCheck(),
BadFunctionNameCheck(),
VariableAndParameterNameCheck(),
UnusedLocalVariableCheck(),
TooManyCasesCheck(),
DeprecatedCodeUsedCheck(),
UselessNullCheckCheck(),
)

private fun scanWithoutSuppression(path: Path) =
scanFile(path, false, BadClassNameCheck(), BadFunctionNameCheck(), VariableAndParameterNameCheck(), UnusedLocalVariableCheck(), TooManyCasesCheck(), DeprecatedCodeUsedCheck())
scanFile(
path,
false,
BadClassNameCheck(),
BadFunctionNameCheck(),
VariableAndParameterNameCheck(),
UnusedLocalVariableCheck(),
TooManyCasesCheck(),
DeprecatedCodeUsedCheck(),
UselessNullCheckCheck(),
)

private fun scanFile(path: Path, suppress: Boolean, check: AbstractCheck, vararg checks: AbstractCheck): SingleFileVerifier {
val env = Environment(DEFAULT_KOTLIN_CLASSPATH, LanguageVersion.LATEST_STABLE)
val env = Environment(System.getProperty("java.class.path").split(System.getProperty("path.separator")) + DEFAULT_KOTLIN_CLASSPATH, LanguageVersion.LATEST_STABLE)
val verifier = SingleFileVerifier.create(path, StandardCharsets.UTF_8)
val testFileContent = String(Files.readAllBytes(path), StandardCharsets.UTF_8)
val inputFile = TestInputFileBuilder("moduleKey", "src/org/foo/kotlin.kt")
val inputFile = TestInputFileBuilder("moduleKey", "src/org/foo/kotlin.kt")
.setCharset(StandardCharsets.UTF_8)
.initMetadata(testFileContent)
.build()
Expand Down

0 comments on commit 3e82ecd

Please sign in to comment.