walkmod-core icon indicating copy to clipboard operation
walkmod-core copied to clipboard

UseCollectionIsEmpty is not working as expected : changing the implementation logic

Open subisueno opened this issue 7 years ago • 8 comments

Example below class -

`package sonar.autofix;

import java.util.List;

public class CollectionIsEmpty {

public void check(List<String> myList){
	if(myList.size() > 0){
		System.out.println("My List is not Empty.");
	}
	if(myList.size() <= 0){
		System.out.println("My List is Empty.");
	}
	if(myList.size() == 0){
		System.out.println("My List is Empty.");
	}
}

} `

Run -

walkmod apply sonar:UseCollectionIsEmpty

After modification first "if-block" is expected to be changed to "!list.isEmpty()" (not operator is missing)

`package sonar.autofix;

import java.util.List;

public class CollectionIsEmpty {

public void check(List<String> myList){
	if(myList.isEmpty()){
		System.out.println("My List is not Empty.");
	}
	if(myList.size() <= 0){
		System.out.println("My List is Empty.");
	}
	if(myList.isEmpty()){
		System.out.println("My List is Empty.");
	}
}

}

`

subisueno avatar Jul 21 '17 11:07 subisueno

I have fixed this, attaching the file. If you like to merge, please merge the fix.

Is Empty Fix.zip

subisueno avatar Jul 24 '17 11:07 subisueno

Made Is Empty Fix.zip Little bit correction

subisueno avatar Jul 24 '17 13:07 subisueno

Thanks, but this is not the standard way to proceed: fork + pull request. I will take a look.

rpau avatar Jul 25 '17 06:07 rpau

I am not able to do anything - may be due to my network issue. I would like to have the fix in the repository and if you help on copying the attached code yourself and create a pull request and then merge that would be good.

Apart from this defect, I have got many others defects causing multiple issues in the real project. I hope, your team is working hard to improve this tool with the time.

subisueno avatar Jul 25 '17 07:07 subisueno

@rpau Can you plz check below link - https://github.com/walkmod/walkmod-sonar-plugin/pull/12

Please let me know, in case I need to do anything else.

subisueno avatar Jul 26 '17 07:07 subisueno

Fixed in walkmod-sonar-plugin

rpau avatar Jul 31 '17 18:07 rpau

It is not working for below logic - if(0 < myList.size()){ System.out.println("My List is not Empty."); } I personally feel, the code can be made simpler. I saw the code logic is too complex and that is why it is really difficult to maintain this code working in so many test-cases.

That is why I tried to make the code simpler - there are 2 main cases -

  1. isEmpty
  2. !isEmpty

Now, If we write the code such a way that a simple change would include / exclude (while fixing any bug) a particular condition from deriving any of the above. So, a Set data-structure would help us to hold the set of conditions. If anyone of them is working the entire set of conditions should be working.

subisueno avatar Aug 01 '17 11:08 subisueno

Ok, you are right. Checking it again.

rpau avatar Aug 01 '17 21:08 rpau