walkmod-core
walkmod-core copied to clipboard
UseCollectionIsEmpty is not working as expected : changing the implementation logic
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.");
}
}
}
`
Made Is Empty Fix.zip Little bit correction
Thanks, but this is not the standard way to proceed: fork + pull request. I will take a look.
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.
@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.
Fixed in walkmod-sonar-plugin
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 -
- isEmpty
- !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.
Ok, you are right. Checking it again.