Create rule S2189[kotlin]: Loops should not be infinite (#327)

This commit is contained in:
margarita-nedzelska-sonarsource 2021-09-16 16:49:23 +03:00 committed by GitHub
parent 4618d16b53
commit 08f4f27d48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 1 deletions

View File

@ -0,0 +1,3 @@
{
}

View File

@ -0,0 +1,44 @@
include::../description.adoc[]
== Noncompliant Code Example
----
var j: Int
while (true) { // Noncompliant; end condition omitted
j++
}
var k: Int
val b = true
while (b) { // Noncompliant; b never written to in loop
k++
}
----
== Compliant Solution
----
var j: Int = 0
while (true) { // reachable end condition added
j++
if (j == Int.MIN_VALUE) { // true at Integer.MAX_VALUE +1
break
}
}
var k: Int = 0
var b = true
while (b) {
k++
b = k < Int.MAX_VALUE
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]

View File

@ -12,7 +12,8 @@
"extra": { "extra": {
"coveredLanguages": [ "coveredLanguages": [
"Java", "Java",
"JavaScript" "JavaScript",
"Kotlin"
], ],
"replacementRules": [ "replacementRules": [