diff --git a/src/main/java/org/walkmod/sonar/visitors/UseCollectionIsEmpty.java b/src/main/java/org/walkmod/sonar/visitors/UseCollectionIsEmpty.java index 00d2c64..96a598f 100644 --- a/src/main/java/org/walkmod/sonar/visitors/UseCollectionIsEmpty.java +++ b/src/main/java/org/walkmod/sonar/visitors/UseCollectionIsEmpty.java @@ -75,7 +75,7 @@ public void visit(BinaryExpr n, VisitorContext ctx) { if (Collection.class.isAssignableFrom(msd.getMethod().getDeclaringClass())) { Expression newExpr = new MethodCallExpr(mce.getScope(), "isEmpty"); - if (n.getOperator().equals(BinaryExpr.Operator.notEquals)) { + if (n.getOperator().equals(BinaryExpr.Operator.notEquals) || n.getOperator().equals(BinaryExpr.Operator.greater)) { newExpr = new UnaryExpr(newExpr, UnaryExpr.Operator.not); } diff --git a/src/test/java/org/walkmod/sonar/visitors/UseCollectionIsEmptyTest.java b/src/test/java/org/walkmod/sonar/visitors/UseCollectionIsEmptyTest.java index dc1c6ce..75b0237 100644 --- a/src/test/java/org/walkmod/sonar/visitors/UseCollectionIsEmptyTest.java +++ b/src/test/java/org/walkmod/sonar/visitors/UseCollectionIsEmptyTest.java @@ -7,6 +7,7 @@ import org.walkmod.javalang.ast.expr.BinaryExpr; import org.walkmod.javalang.ast.expr.EnclosedExpr; import org.walkmod.javalang.ast.expr.MethodCallExpr; +import org.walkmod.javalang.ast.expr.UnaryExpr; import org.walkmod.javalang.ast.stmt.BlockStmt; import org.walkmod.javalang.ast.stmt.ReturnStmt; import org.walkmod.javalang.test.SemanticTest; @@ -32,6 +33,25 @@ public void testEqualsToZero() throws Exception { Assert.assertEquals("isEmpty", mce.getName()); } + + @Test + public void testMoreThanZero() throws Exception { + CompilationUnit cu = compile( + "import java.util.List; public class Foo { public boolean testIsEmpty(List list){ return list.size() > 0; }}"); + + UseCollectionIsEmpty visitor = new UseCollectionIsEmpty(); + cu.accept(visitor, null); + + MethodDeclaration md = (MethodDeclaration) cu.getTypes().get(0).getMembers().get(0); + + BlockStmt block = md.getBody(); + ReturnStmt returnStmt = (ReturnStmt) block.getStmts().get(0); + + UnaryExpr ue = (UnaryExpr)returnStmt.getExpr(); + MethodCallExpr mce = (MethodCallExpr)ue.getExpr(); + Assert.assertTrue(ue.getOperator() == UnaryExpr.Operator.not); + Assert.assertEquals("isEmpty", mce.getName()); + } @Test public void testEqualsToZeroWithOtherBinOp() throws Exception {