IDEA-173177 Inspection: Comparing 'compareTo()` or 'Comparator.compare()' result with 1 / -1

This commit is contained in:
Tagir Valeev
2017-05-24 14:14:16 +07:00
parent 86c6da36f7
commit 1db8b83dff
11 changed files with 262 additions and 1 deletions
@@ -0,0 +1,10 @@
// "Replace with '> 0'" "true"
import java.util.*;
class Test {
void test(String str) {
if(str.compareTo("xyz") > 0) {
System.out.println("Oops");
}
}
}
@@ -0,0 +1,17 @@
// "Fix all 'Comparison of compare method result with specific constant' problems in file" "true"
import java.util.*;
class Test {
String test(Comparable<?> c1, Comparable<?> c2) {
int result = c1.compareTo(c2);
if(0 == result) {
return "equal";
} else if(0 > result) {
return "less";
} else if(0 < result) {
return "greater";
} else {
return "impossible";
}
}
}
@@ -0,0 +1,10 @@
// "Replace with '>= 0'" "true"
import java.util.*;
class Test {
void test(Comparator<String> cmp) {
if(cmp.compare("a", "b") >= 0) {
System.out.println("Oops");
}
}
}
@@ -0,0 +1,10 @@
// "Replace with '> 0'" "true"
import java.util.*;
class Test {
void test(String str) {
if(str.compareTo("xyz") =<caret>= 1) {
System.out.println("Oops");
}
}
}
@@ -0,0 +1,17 @@
// "Fix all 'Comparison of compare method result with specific constant' problems in file" "true"
import java.util.*;
class Test {
String test(Comparable<?> c1, Comparable<?> c2) {
int result = c1.compareTo(c2);
if(0 == result) {
return "equal";
} else if(-1 =<caret>= result) {
return "less";
} else if(1 == result) {
return "greater";
} else {
return "impossible";
}
}
}
@@ -0,0 +1,10 @@
// "Replace with '>= 0'" "true"
import java.util.*;
class Test {
void test(Comparator<String> cmp) {
if(cmp.compare("a", "b") !<caret>= -1) {
System.out.println("Oops");
}
}
}