Skip to content
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

fix: Improve EC69 #45

Open
wants to merge 3 commits into
base: main
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- [#21](https://github.com/green-code-initiative/ecoCode-java/issues/21) Improvement: some method calls are legitimate in a for loop expression.
- [#49](https://github.com/green-code-initiative/ecoCode-java/pull/49) Add test to ensure all Rules are registered
- [#336](https://github.com/green-code-initiative/ecoCode/issues/336) [Adds Maven Wrapper](https://github.com/green-code-initiative/ecoCode-java/pull/67)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,28 @@ public void visitNode(Tree tree) {
method.condition().accept(invocationMethodVisitor);
}
// update
// initaliser
method.update().accept(invocationMethodVisitor);
method.initializer().accept(invocationMethodVisitor);
}

private class MethodInvocationInForStatementVisitor extends BaseTreeVisitor {

@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
if (!lineAlreadyHasThisIssue(tree)) {
if (!lineAlreadyHasThisIssue(tree) && !isIteratorMethod(tree)) {
report(tree);
return;
}
super.visitMethodInvocation(tree);
}

private boolean isIteratorMethod(MethodInvocationTree tree) {
boolean isIterator = tree.methodSymbol().owner().type().isSubtypeOf("java.util.Iterator");
String methodName = tree.methodSelect().lastToken().text();
boolean isMethodNext = methodName.equals("next");
boolean isMethodHasNext = methodName.equals("hasNext");
return isIterator && (isMethodNext || isMethodHasNext);
}

private boolean lineAlreadyHasThisIssue(Tree tree) {
if (tree.firstToken() != null) {
final String classname = getFullyQualifiedNameOfClassOf(tree);
Expand Down
47 changes: 45 additions & 2 deletions src/test/files/NoFunctionCallWhenDeclaringForLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Arrays;
class NoFunctionCallWhenDeclaringForLoop {
NoFunctionCallWhenDeclaringForLoop(NoFunctionCallWhenDeclaringForLoop mc) {
}
Expand All @@ -30,7 +34,7 @@ public int incrementeMyValue(int i) {
public void test1() {
for (int i = 0; i < 20; i++) {
System.out.println(i);
boolean b = getMyValue() > 6;
boolean b = this.getMyValue() > 6;
}
}

Expand All @@ -42,8 +46,9 @@ public void test2() {

}

// compliant, the function is called only once in the initialization so it's not a performance issue
public void test3() {
for (int i = getMyValue(); i < 20; i++) { // Noncompliant {{Do not call a function when declaring a for-type loop}}
for (int i = getMyValue(); i < 20; i++) {
System.out.println(i);
boolean b = getMyValue() > 6;
}
Expand All @@ -70,4 +75,42 @@ public void test6() {
}
}

// compliant, iterators are allowed to be called in a for loop
public void test7() {
List<String> joursSemaine = Arrays.asList("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche");

String jour;
// iterator is allowed
for (Iterator<String> iterator = joursSemaine.iterator(); iterator.hasNext(); jour = iterator.next()) {
System.out.println(jour);
}

// subclass of iterator is allowed
for (ListIterator<String> iterator = joursSemaine.listIterator(); iterator.hasNext(); jour = iterator.next()) {
System.out.println(jour);
}

// iterator called in an indirect way is allowed
for (OtherClassWithIterator otherClass = new OtherClassWithIterator(joursSemaine); otherClass.iterator.hasNext(); jour = otherClass.iterator.next()) {
System.out.println(jour);
}
// but using a method that returns an iterator causes an issue
for (OtherClassWithIterator otherClass = new OtherClassWithIterator(joursSemaine); otherClass.getIterator().hasNext(); jour = otherClass.getIterator().next()) { // Noncompliant {{Do not call a function when declaring a for-type loop}}
System.out.println(jour);
}

}

}

class OtherClassWithIterator {
public Iterator<String> iterator;

public OtherClassWithIterator(Iterator<String> iterator){
this.iterator = iterator;
}

public Iterator getIterator(){
return iterator;
}
}
Loading