RedundantCollectionOperation: remove by index and contains before add/remove

IDEA-182694 Inefficient uses of Collection/List.remove
This commit is contained in:
Tagir Valeev
2017-12-25 17:39:36 +07:00
parent 66c8afddfa
commit 4c568edfc9
15 changed files with 267 additions and 8 deletions
@@ -0,0 +1,9 @@
// "Remove the 'contains' check" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
/*contains!!!*/
list.remove(/*remove!!!*/key);
}
}
@@ -0,0 +1,10 @@
// "Remove the 'contains' check" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
if(key != null) {
list.remove(key);
}
}
}
@@ -0,0 +1,11 @@
// "Remove the 'contains' check" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
if(key !=/*nullcheck*/ null && !key./*check*/isEmpty() /*and*/ /*key*/ //line comment
) {
list.remove(key);
}
}
}
@@ -0,0 +1,8 @@
// "Use removal by object" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
list.remove(key);
}
}
@@ -0,0 +1,8 @@
// "Use removal by object" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
list.remove(key);
}
}
@@ -0,0 +1,10 @@
// "Remove the 'contains' check" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
if(list.co<caret>ntains(/*contains!!!*/key)) {
list.remove(/*remove!!!*/key);
}
}
}
@@ -0,0 +1,10 @@
// "Remove the 'contains' check" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
if(key != null && list.co<caret>ntains(key)) {
list.remove(key);
}
}
}
@@ -0,0 +1,11 @@
// "Remove the 'contains' check" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
if(key !=/*nullcheck*/ null && !key./*check*/isEmpty() && /*and*/ list.co<caret>ntains(/*key*/key//line comment
)) {
list.remove(key);
}
}
}
@@ -0,0 +1,10 @@
// "Remove the 'contains' check" "false"
import java.util.List;
class Test {
void test(List<String> list, String key) {
if(list.co<caret>ntains(key) && key != null) {
list.remove(key);
}
}
}
@@ -0,0 +1,8 @@
// "Use removal by object" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
list.r<caret>emove(list.indexOf(key));
}
}
@@ -0,0 +1,9 @@
// "Use removal by object" "false"
import java.util.List;
class Test {
void test(List<String> list, String key) {
int idx = list.indexOf(key);
String x = list.r<caret>emove(idx);
}
}
@@ -0,0 +1,9 @@
// "Use removal by object" "true"
import java.util.List;
class Test {
void test(List<String> list, String key) {
int idx = list.indexOf(key);
list.r<caret>emove(idx);
}
}
@@ -0,0 +1,9 @@
// "Use removal by object" "false"
import java.util.List;
class Test {
void test(List<String> list, List<String> list2, String key) {
int idx = list.indexOf(key);
list2.r<caret>emove(idx);
}
}