|
17 | 17 |
|
18 | 18 | import java.util.Optional;
|
19 | 19 |
|
| 20 | +/** Includes true-positive and false-negative cases. */ |
20 | 21 | public class OptionalNotPresentPositiveCases {
|
21 | 22 |
|
22 |
| - public void test(Optional<String> testStr) { |
| 23 | + // False-negative |
| 24 | + public String getWhenUnknown(Optional<String> optional) { |
| 25 | + return optional.get(); |
| 26 | + } |
| 27 | + |
| 28 | + // False-negative |
| 29 | + public String getWhenUnknown_testNull(Optional<String> optional) { |
| 30 | + if (optional.get() != null) { |
| 31 | + return optional.get(); |
| 32 | + } |
| 33 | + return ""; |
| 34 | + } |
| 35 | + |
| 36 | + // False-negative |
| 37 | + public String getWhenAbsent_testAndNestUnrelated(Optional<String> optional) { |
| 38 | + if (true) { |
| 39 | + String str = optional.get(); |
| 40 | + if (!optional.isPresent()) { |
| 41 | + return ""; |
| 42 | + } |
| 43 | + return str; |
| 44 | + } |
| 45 | + return ""; |
| 46 | + } |
| 47 | + |
| 48 | + public String getWhenAbsent(Optional<String> testStr) { |
23 | 49 | if (!testStr.isPresent()) {
|
24 | 50 | // BUG: Diagnostic contains: Optional
|
25 |
| - String str = testStr.get(); |
| 51 | + return testStr.get(); |
26 | 52 | }
|
| 53 | + return ""; |
27 | 54 | }
|
28 | 55 |
|
29 |
| - public void testMultipleStatements(Optional<String> optional) { |
| 56 | + public String getWhenAbsent_multipleStatements(Optional<String> optional) { |
30 | 57 | if (!optional.isPresent()) {
|
31 | 58 | String test = "test";
|
32 | 59 | // BUG: Diagnostic contains: Optional
|
33 |
| - String str = optional.get(); |
| 60 | + return test + optional.get(); |
34 | 61 | }
|
| 62 | + return ""; |
35 | 63 | }
|
36 | 64 |
|
37 |
| - public void testNestedIf(Optional<String> optional) { |
38 |
| - if (!optional.isPresent()) { |
39 |
| - if (optional == Optional.of("")) { |
40 |
| - // BUG: Diagnostic contains: Optional |
41 |
| - String str = optional.get(); |
42 |
| - } |
| 65 | + // False-negative |
| 66 | + public String getWhenAbsent_nestedCheck(Optional<String> optional) { |
| 67 | + if (!optional.isPresent() || true) { |
| 68 | + return !optional.isPresent() ? optional.get() : ""; |
43 | 69 | }
|
| 70 | + return ""; |
44 | 71 | }
|
45 | 72 |
|
46 |
| - public void testAnd(Optional<String> optional) { |
47 |
| - if (!optional.isPresent() && 7 == 7) { |
| 73 | + public String getWhenAbsent_compoundIf_false(Optional<String> optional) { |
| 74 | + if (!optional.isPresent() && true) { |
48 | 75 | // BUG: Diagnostic contains: Optional
|
49 |
| - String str = optional.get(); |
| 76 | + return optional.get(); |
50 | 77 | }
|
| 78 | + return ""; |
51 | 79 | }
|
52 | 80 |
|
53 |
| - public String checkedInElse(Optional<String> optional) { |
| 81 | + // False-negative |
| 82 | + public String getWhenAbsent_compoundIf_true(Optional<String> optional) { |
| 83 | + if (!optional.isPresent() || true) { |
| 84 | + return optional.get(); |
| 85 | + } |
| 86 | + return ""; |
| 87 | + } |
| 88 | + |
| 89 | + public String getWhenAbsent_elseClause(Optional<String> optional) { |
54 | 90 | if (optional.isPresent()) {
|
55 | 91 | return optional.get();
|
56 | 92 | } else {
|
57 | 93 | // BUG: Diagnostic contains: Optional
|
58 | 94 | return optional.get();
|
59 | 95 | }
|
60 | 96 | }
|
| 97 | + |
| 98 | + // False-negative |
| 99 | + public String getWhenAbsent_localReassigned(Optional<String> optional) { |
| 100 | + if (!optional.isPresent()) { |
| 101 | + optional = Optional.empty(); |
| 102 | + } |
| 103 | + return optional.get(); |
| 104 | + } |
| 105 | + |
| 106 | + // False-negative |
| 107 | + public String getWhenAbsent_methodScoped(Optional<String> optional) { |
| 108 | + if (optional.isPresent()) { |
| 109 | + return ""; |
| 110 | + } |
| 111 | + return optional.get(); |
| 112 | + } |
61 | 113 | }
|
0 commit comments