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

UseCollectionIsEmpty is not working as expected : changing the implementation logic #73

Open
subisueno opened this issue Jul 21, 2017 · 8 comments
Labels

Comments

@subisueno
Copy link

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
Copy link
Author

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

Is Empty Fix.zip

@subisueno
Copy link
Author

Made
Is Empty Fix.zip
Little bit correction

@rpau
Copy link
Collaborator

rpau commented Jul 25, 2017

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

@subisueno
Copy link
Author

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
Copy link
Author

@rpau Can you plz check below link -
walkmod/walkmod-sonar-plugin#12

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

@rpau
Copy link
Collaborator

rpau commented Jul 31, 2017

Fixed in walkmod-sonar-plugin

@rpau rpau closed this as completed Jul 31, 2017
@subisueno
Copy link
Author

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.

@rpau
Copy link
Collaborator

rpau commented Aug 1, 2017

Ok, you are right. Checking it again.

@rpau rpau reopened this Aug 1, 2017
@rpau rpau added the bug label Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants