Skip to content

Commit

Permalink
add check for Optional.equals(Optional.empty())
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Brosius committed Oct 22, 2021
1 parent d0e02e9 commit db220ba
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions etc/bugrank.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
+0 BugPattern OC_OVERZEALOUS_CASTING
+0 BugPattern ODN_ORPHANED_DOM_NODE
+0 BugPattern OI_OPTIONAL_ISSUES_CHECKING_REFERENCE
+0 BugPattern OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED
+0 BugPattern OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED
+0 BugPattern OI_OPTIONAL_ISSUES_USES_DELAYED_EXECUTION
+0 BugPattern OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION
Expand Down
3 changes: 2 additions & 1 deletion etc/findbugs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@

<Detector class="com.mebigfatguy.fbcontrib.detect.UnsynchronizedSingletonFieldWrites" speed="fast" reports="USFW_UNSYNCHRONIZED_SINGLETON_FIELD_WRITES"/>

<Detector class="com.mebigfatguy.fbcontrib.detect.OptionalIssues" speed="fast" reports="OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION,OI_OPTIONAL_ISSUES_USES_DELAYED_EXECUTION,OI_OPTIONAL_ISSUES_CHECKING_REFERENCE,OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED,OI_OPTIONAL_ISSUES_USES_ORELSEGET_WITH_NULL"/>
<Detector class="com.mebigfatguy.fbcontrib.detect.OptionalIssues" speed="fast" reports="OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION,OI_OPTIONAL_ISSUES_USES_DELAYED_EXECUTION,OI_OPTIONAL_ISSUES_CHECKING_REFERENCE,OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED,OI_OPTIONAL_ISSUES_USES_ORELSEGET_WITH_NULL,OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED"/>

<Detector class="com.mebigfatguy.fbcontrib.detect.UnnecessaryApiConversion" speed="fast" reports="UAC_UNNECESSARY_API_CONVERSION_DATE_TO_INSTANT,UAC_UNNECESSARY_API_CONVERSION_FILE_TO_PATH"/>

Expand Down Expand Up @@ -629,6 +629,7 @@
<BugPattern abbrev="OI" type="OI_OPTIONAL_ISSUES_CHECKING_REFERENCE" category="CORRECTNESS"/>
<BugPattern abbrev="OI" type="OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED" category="CORRECTNESS"/>
<BugPattern abbrev="OI" type="OI_OPTIONAL_ISSUES_USES_ORELSEGET_WITH_NULL" category="CORRECTNESS" />
<BugPattern abbrev="OI" type="OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED" category="CORRECTNESS"/>
<BugPattern abbrev="UAC" type="UAC_UNNECESSARY_API_CONVERSION_DATE_TO_INSTANT" category="CORRECTNESS"/>
<BugPattern abbrev="UAC" type="UAC_UNNECESSARY_API_CONVERSION_FILE_TO_PATH" category="CORRECTNESS"/>
<BugPattern abbrev="RFI" type="RFI_SET_ACCESSIBLE" category="CORRECTNESS"/>
Expand Down
12 changes: 12 additions & 0 deletions etc/messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6115,6 +6115,18 @@ if (shouldCalcHalting && (calculateHaltingProbability() &gt; 0) { }
]]>
</Details>
</BugPattern>

<BugPattern type="OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED">
<ShortDescription>Method uses Optional.equals(Optional.empty()), when Optional.isPresent is more readable</ShortDescription>
<LongDescription>Method {1} uses Optional.equals(Optional.empty()), when Optional.isPresent is more readable</LongDescription>
<Details>
<![CDATA[
<p>This method uses Optional.equals(Optional.empty()). It is more readable and more clear just to use the Optional.isPresent()
method to determine whether the reference exists or not.
</p>
]]>
</Details>
</BugPattern>

<BugPattern type="UAC_UNNECESSARY_API_CONVERSION_DATE_TO_INSTANT">
<ShortDescription>Method constructs a Date object, merely to convert it to an Instant object</ShortDescription>
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/com/mebigfatguy/fbcontrib/detect/OptionalIssues.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,23 @@ public void sawOpcode(int seen) {
String methodName = getNameConstantOperand();
curCalledMethod = new FQMethod(clsName, methodName, getSigConstantOperand());

if ("java/util/Optional".equals(clsName) && "of".equals(methodName)) {
if (stack.getStackDepth() > 0) {
OpcodeStack.Item itm = stack.getStackItem(0);
String itmSig = itm.getSignature();
if (BOXED_OPTIONAL_TYPES.contains(itmSig)) {
bugReporter.reportBug(
new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED.name(),
LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
if ("java/util/Optional".equals(clsName)) {
if ("of".equals(methodName)) {
if (stack.getStackDepth() > 0) {
OpcodeStack.Item itm = stack.getStackItem(0);
String itmSig = itm.getSignature();
if (BOXED_OPTIONAL_TYPES.contains(itmSig)) {
bugReporter.reportBug(
new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED.name(),
LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
} else if ("equals".equals(methodName)) {
bugReporter.reportBug(
new BugInstance(this, BugType.OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED.name(),
LOW_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));

}
}
break;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/mebigfatguy/fbcontrib/utils/BugType.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public enum BugType {
ODN_ORPHANED_DOM_NODE,
OI_OPTIONAL_ISSUES_CHECKING_REFERENCE,
OI_OPTIONAL_ISSUES_PRIMITIVE_VARIANT_PREFERRED,
OI_OPTIONAL_ISSUES_ISPRESENT_PREFERRED,
OI_OPTIONAL_ISSUES_USES_DELAYED_EXECUTION,
OI_OPTIONAL_ISSUES_USES_IMMEDIATE_EXECUTION,
OI_OPTIONAL_ISSUES_USES_ORELSEGET_WITH_NULL,
Expand Down
4 changes: 4 additions & 0 deletions src/samples/java/ex/OI_Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ private Optional<String> get(String name) {
public String fpGet384(String parameterName, Supplier<String> defaultValueSupplier) {
return this.<String>get(parameterName).orElseGet(defaultValueSupplier);
}

public boolean equalsToEmpty(Optional<String> foo) {
return foo.equals(Optional.empty());
}
}

0 comments on commit db220ba

Please sign in to comment.