Compare commits

...

1 Commits

Author SHA1 Message Date
Johann Beleites
8dcacc2717 Update rule S2272: "Iterator.next()" methods should throw "NoSuchElementException" 2021-09-15 14:56:17 +02:00
4 changed files with 61 additions and 26 deletions

View File

@ -1,28 +1,2 @@
{ {
"title": "\"Iterator.next()\" methods should throw \"NoSuchElementException\"",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"error-handling",
"unpredictable"
],
"extra": {
"coveredLanguages": [
"Java"
],
"replacementRules": [
]
},
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-2272",
"sqKey": "S2272",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
]
} }

View File

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

View File

@ -0,0 +1,32 @@
By contract, any implementation of the `kotlin.collections.Iterator.next()` method should throw a `kotlin.NoSuchElementException` when the iteration has no more elements. Any other behavior when the iteration is done could lead to unexpected behavior for users of this `Iterator`.
== Noncompliant Code Example
----
class MyIterator : Iterator<String?> {
// ...
override fun next(): String? {
if(!hasNext()){
return null
}
// ...
}
}
----
== Compliant Solution
----
class MyIterator : Iterator<String?> {
// ...
override fun next(): String? {
if(!hasNext()){
throw NoSuchElementException()
}
// ...
}
}
----

View File

@ -1,2 +1,29 @@
{ {
"title": "\"Iterator.next()\" methods should throw \"NoSuchElementException\"",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"error-handling",
"unpredictable"
],
"extra": {
"coveredLanguages": [
"Java",
"Kotlin"
],
"replacementRules": [
]
},
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-2272",
"sqKey": "S2272",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
]
} }