Skip to content

Commit

Permalink
Fix S6524 to comply with its description
Browse files Browse the repository at this point in the history
  • Loading branch information
leveretka committed Nov 22, 2024
1 parent de3dbb4 commit 12cd69f
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static void addLanguagePlugins(OrchestratorBuilder builder) {
}

@Test
@EnabledIfEnvironmentVariable(named = "KOTLIN_COMPILER_IT_ENABLED", matches = "true")
//@EnabledIfEnvironmentVariable(named = "KOTLIN_COMPILER_IT_ENABLED", matches = "true")
void test_kotlin_compiler() throws IOException {
List<String> exclusions = List.of(
"**/testData/**/*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"kotlin-corda-project:sources/kotlin/corda/experimental/behave/src/main/kotlin/net/corda/behave/file/LogSource.kt": [
27
],
"kotlin-corda-project:sources/kotlin/corda/node-api/src/main/kotlin/net/corda/nodeapi/internal/network/NetworkBootstrapper.kt": [
170
],
"kotlin-corda-project:sources/kotlin/corda/node/src/test/kotlin/net/corda/node/utilities/PersistentMapTests.kt": [
152
],
"kotlin-corda-project:sources/kotlin/corda/serialization/src/test/kotlin/net/corda/serialization/internal/amqp/EnumEvolvabilityTests.kt": [
183
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"kotlin-kotlin-language-server-project:server/src/main/kotlin/org/javacs/kt/completion/RenderCompletionItem.kt": [
96
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
package checks

fun compliantScopedFunctions(
a: MutableList<Int>, // Compliant
b: MutableList<Int>, // Compliant
c: MutableList<Int>, // Compliant
d: MutableList<Int>, // Compliant
e: MutableList<Int>, // Compliant
f: MutableList<Int>, // Compliant
crazy: MutableList<Int>,
): Unit { // Compliant
a.let { it.add(1) }
b.also { it.add(1) }
c.apply { add(1) }
d.run { add(1) }
with(e) { add(1) }
f.let { it.let { it.let { it.add(1) } } }
crazy.let { it.also { it.apply { it.run { with(this) { it.add(1) } } } } }
}

class CollectionShouldBeImmutableCheckSample {
fun MutableCollection<Int>.doSomething(): Unit {} // Noncompliant
fun MutableCollection<Int>.doSomething(): Unit {} // Don't report extension functions

//let also apply run with
fun nonCompliant(
fun nonCompliantXYZ(
x: MutableList<Int>, // Noncompliant {{Make this collection immutable.}}
y: MutableSet<String>, // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -37,31 +55,15 @@ class CollectionShouldBeImmutableCheckSample {
fun List<Int>.toList(): List<Int> = this // compliant
fun baz(list : MutableList<Int>): Unit {} // Noncompliant


//Declared on immutable
fun List<Int>.foo(): Unit {} // Compliant
fun doNothing(a : List<Int>, b : Set<String>, c : Map<Int,Int>): Unit {}
fun doNothing(a : List<Int>): List<Int> { return a } // Compliant

fun <A>id(a : A): A = a // Compliant

fun compliantScopedFunctions(
a: MutableList<Int>, // Compliant
b: MutableList<Int>, // Compliant
c: MutableList<Int>, // Compliant
d: MutableList<Int>, // Compliant
e: MutableList<Int>, // Compliant
f: MutableList<Int>, // Compliant
crazy: MutableList<Int>,
): Unit { // Compliant
a.let { it.add(1) }
b.also { it.add(1) }
c.apply { add(1) }
d.run { add(1) }
with(e) { add(1) }
f.let { it.let { it.let { it.add(1) } } }
crazy.let { it.also { it.apply { it.run { with(this) { it.add(1) } } } } }
}

fun MutableList<Int>.doSomething2(): MutableList<Int> { return this } // Noncompliant
fun MutableList<Int>.doSomething2(): MutableList<Int> { return this } // Don't report extension functions

fun compliantFunctionsCalledOn(
a: MutableList<Int>, // Compliant
Expand Down Expand Up @@ -163,8 +165,7 @@ class CollectionShouldBeImmutableCheckSample {
if(add(1)) {}
}

fun MutableList<Int>.noncompliantDelegate(): Int { // Noncompliant
// ^^^^^^^^^^^^^^^^
fun MutableList<Int>.noncompliantDelegate(): Int { // Don't report extension functions
return reduce { acc, it -> acc + it}
}

Expand Down Expand Up @@ -229,7 +230,7 @@ class CollectionShouldBeImmutableCheckSample {

interface A {
fun foo(list : MutableList<Int>): Unit // compliant
fun bar(list : MutableList<Int>): Int { // compliant
fun bar(list : MutableList<Int>): Int { // Noncompliant
return list.reduce { acc, it -> acc + it}
}
}
Expand All @@ -253,3 +254,11 @@ private fun nonCompliantParameterOnFileLevel(list: MutableList<Int>): Int { // N

// https://sonarsource.atlassian.net/browse/SONARKT-388
private fun <T> intersectionType(t: T) = if (t is String) listOf(t) else emptyList()

fun sum123(acc: List<Int>): Int {
val list = mutableListOf(1,2,3) // Noncompliant
val list2: List<Int> = mutableListOf(1,2,3) // Compliant, immutable type specified

list2.size
return list.reduce { acc, item -> acc + item}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package checks

class CollectionShouldBeImmutableCheckSampleNoSemantics {
fun MutableCollection<Int>.doSomething(): Unit {} // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^
fun MutableCollection<Int>.doSomething(): Unit {} // Don't report extension functions

//let also apply run with
fun nonCompliant(
Expand Down Expand Up @@ -63,8 +62,7 @@ class CollectionShouldBeImmutableCheckSampleNoSemantics {
crazy.let { it.also { it.apply { it.run { with(this) { it.add(1) } } } } }
}

fun MutableList<Int>.doSomething2(): MutableList<Int> { return this } // Noncompliant
// ^^^^^^^^^^^^^^^^
fun MutableList<Int>.doSomething2(): MutableList<Int> { return this } // Don't report extension functions

fun compliantFunctionsCalledOn(
a: MutableList<Int>, // Compliant
Expand Down Expand Up @@ -219,4 +217,4 @@ class CollectionShouldBeImmutableCheckSampleNoSemantics {
fun foo(configure: (MutableMap<String, Any?>) -> Unit): Unit { // compliant
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ fun KtProperty.determineTypeAsString(bindingContext: BindingContext, printTypeAr
determineType(bindingContext)?.getKotlinTypeFqName(printTypeArguments)

fun KtExpression.determineTypeAsString(bindingContext: BindingContext, printTypeArguments: Boolean = false) =
determineType(bindingContext)?.getKotlinTypeFqName(printTypeArguments)
determineType(bindingContext)?.let {
if (it.constructor.declarationDescriptor != null) it.getKotlinTypeFqName(printTypeArguments)
else null
}

private fun KtParameter.determineType(bindingContext: BindingContext) =
bindingContext[BindingContext.TYPE, typeReference]
Expand Down
Loading

0 comments on commit 12cd69f

Please sign in to comment.