IDEA-163991 ifPresent is not suggested when value is not used inside expression; OptionalIsPresentInspection uses CommentTracker now

This commit is contained in:
Tagir Valeev
2016-11-23 17:26:57 +07:00
parent 4ec11eb771
commit 0f7c78a5a6
12 changed files with 166 additions and 90 deletions
@@ -0,0 +1,15 @@
// "Replace Optional.isPresent() condition with functional style expression" "INFORMATION"
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Optional;
public class Main<T> {
public static <A extends Annotation> Optional<A> findAnnotation(Optional<? extends AnnotatedElement> element) {
return element.<Optional<A>>map(annotatedElement -> Optional.empty()).orElseGet(() -> findAnnotation((AnnotatedElement) null));
}
private static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element) {
return Optional.empty();
}
}
@@ -3,13 +3,14 @@
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
String val;
// line comment
// another line comment
//before trim
/* block comment *//*block comment*/
val = str.map(String::trim).orElse("");
System.out.println(val);
}
public void testOptional(Optional<String> str) {
String val;
// line comment
// another line comment
/* block comment */
/*block comment*/
//before trim
val = str.map(String::trim).orElse("");
System.out.println(val);
}
}
@@ -1,4 +1,4 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
// "Replace Optional.isPresent() condition with functional style expression" "GENERIC_ERROR_OR_WARNING"
import java.util.*;
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with functional style expression" "INFORMATION"
import java.util.Optional;
public class Main {
public void test(Optional<String> opt) {
opt.ifPresent(s -> {
if (s.equals("abc"))
System.out.println(s);
});
}
}
@@ -0,0 +1,13 @@
// "Replace Optional.isPresent() condition with functional style expression" "INFORMATION"
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
str.ifPresent(s -> {
System.out.println(s);
// once again!
System.out.println(s);
});
}
}
@@ -11,7 +11,10 @@ public class Main {
}
public Number testOptionalComments(Optional<MyList> strList) {
/* optional is present *//*return something *//*too big*//* optional is absent *//* return null*/
return strList.map(myList -> myList.size() > 1 ? myList.get(1) : 1.0).orElse(null);
/* optional is present */
/*return something */
/* optional is absent */
/* return null*/
return strList.map(myList -> myList.size() > /*too big*/ 1 ? myList.get(1) : 1.0).orElse(null);
}
}
@@ -1,4 +1,4 @@
// "Replace Optional.isPresent() condition with functional style expression" "false"
// "Replace Optional.isPresent() condition with functional style expression" "INFORMATION"
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
@@ -3,16 +3,16 @@
import java.util.*;
public class Main {
public void testOptional(Optional<String> str) {
String val;
if (str.isPrese<caret>nt()) {
val = // line comment
// another line comment
str.get()//before trim
.trim() /* block comment *//*block comment*/;
} else {
val = "";
public void testOptional(Optional<String> str) {
String val;
if (str.isPrese<caret>nt()) {
val = // line comment
// another line comment
str.get()//before trim
.trim() /* block comment *//*block comment*/;
} else {
val = "";
}
System.out.println(val);
}
System.out.println(val);
}
}
@@ -1,4 +1,4 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
// "Replace Optional.isPresent() condition with functional style expression" "GENERIC_ERROR_OR_WARNING"
import java.util.*;
@@ -0,0 +1,12 @@
// "Replace Optional.isPresent() condition with functional style expression" "INFORMATION"
import java.util.Optional;
public class Main {
public void test(Optional<String> opt) {
if(opt.isPres<caret>ent()) {
if(opt.get().equals("abc"))
System.out.println(opt.get());
}
}
}
@@ -1,4 +1,4 @@
// "Replace Optional.isPresent() condition with functional style expression" "false"
// "Replace Optional.isPresent() condition with functional style expression" "INFORMATION"
import java.util.*;
@@ -6,6 +6,7 @@ public class Main {
public void testOptional(Optional<String> str) {
if (str.isPrese<caret>nt()) {
System.out.println(str.get());
// once again!
System.out.println(str.get());
}
}