Skip to content

Create rule S4348[kotlin]: "iterator" should not return "this"" #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rules/S4348/description.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
There are two classes in the standard library that deal with iterations: `Iterable<T>` and `Iterator<T>`. An `Iterable<T>` represents a data structure that can be the target of the "for-each loop" statement, and an `Iterator<T>` represents the state of an ongoing traversal. An `Iterable<T>` is generally expected to support multiple traversals.

An `Iterator<T>` that also implements `Iterable<t>` by returning itself as its `iterator()` will not support multiple traversals since its state will be carried over.


This rule raises an issue when the `iterator()` method of a class implementing both `Iterable<T>` and `Iterator<t>` returns `this`.
26 changes: 1 addition & 25 deletions rules/S4348/java/metadata.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
{
"title": "\"iterator\" should not return \"this\"",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "10min"
},
"tags": [
"pitfall"
],
"extra": {
"coveredLanguages": [
"Java"
],
"replacementRules": [

]
},
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-4348",
"sqKey": "S4348",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
]

}
7 changes: 1 addition & 6 deletions rules/S4348/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
There are two classes in the Java standard library that deal with iterations: ``++Iterable<T>++`` and ``++Iterator<T>++``. An ``++Iterable<T>++`` represents a data structure that can be the target of the "for-each loop" statement, and an ``++Iterator<T>++`` represents the state of an ongoing traversal. An ``++Iterable<T>++`` is generally expected to support multiple traversals.

An ``++Iterator<T>++`` that also implements ``++Iterable<t>++`` by returning itself as its ``++iterator()++`` will not support multiple traversals since its state will be carried over.


This rule raises an issue when the ``++iterator()++`` method of a class implementing both ``++Iterable<T>++`` and ``++Iterator<t>++`` returns ``++this++``.
include::../description.adoc


== Noncompliant Code Example
Expand Down
3 changes: 3 additions & 0 deletions rules/S4348/kotlin/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
36 changes: 36 additions & 0 deletions rules/S4348/kotlin/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
include::../description.adoc


== Noncompliant Code Example

----
class FooIterator : Iterator<Foo>, Iterable<Foo> {

override fun iterator(): Iterator<Foo> {
return this // Noncompliant
}
// ...
}
----


== Compliant Solution

----
class FooSequence : Iterable<Foo> {

override fun iterator(): Iterator<Foo> {
return object : Iterator<Foo> {
override fun hasNext(): Boolean {
//...
}

override fun next(): Foo {
//...
}
}
}
//...
}
----

25 changes: 25 additions & 0 deletions rules/S4348/metadata.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
{
"title": "\"iterator()\" should not return \"this\"",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "10min"
},
"tags": [
"pitfall"
],
"extra": {
"coveredLanguages": [
"Java"
],
"replacementRules": [

]
},
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-4348",
"sqKey": "S4348",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
]
}