Menu

#322 Warn about useless void methods

3.0.1
closed-fixed
None
5
2015-01-23
2014-11-21
No

This is a further improvement of my side effect methods analysis. This analysis can help to detect void non-empty methods which execution will have no visible effect. I think I found very good false-positive/false-negative balance, so false-positives rate will be quite low.

First tests show that usually this warns about partially outcommented methods like this:

void myMethod() {
  if(someCondition) {
     /* outcommented code blob is here */
  }
}

In such cases it's better to outcomment the condition as well as empty method have much more chances to be inlined.

Also it uncovers really cool bugs. For example, this SortedSet#merge method which has 26 lines, but actually does nothing. Another example is VirtualManager#removeIndicesFromTo:

public void removeIndicesFromTo(int from, int to) {
    int indexAfterTo = to + 1;
    Object[] newCache = new Object[cachedElements.length
            - (indexAfterTo - from)];
    System.arraycopy(cachedElements, 0, newCache, 0, from);
    if (indexAfterTo < cachedElements.length) {
        System.arraycopy(cachedElements, indexAfterTo, newCache, from,
                cachedElements.length - indexAfterTo);
    }
}

It was forgotten to write cachedElements = newCache at the end of this method.

Will commit after final polishing and testing.

Related

Feature Requests: #318

Discussion

  • Tagir Valeev

    Tagir Valeev - 2014-11-22

    The code is committed:
    https://code.google.com/p/findbugs/source/detail?r=92591277209d995ef6154e0d7d6adeccfb706269

    One more interesting case found by this pattern: CheatSheetRegistryReader#pruneEmptyCategories

    /**
     * Removes the empty categories from a cheatsheet collection. 
     */
    private void pruneEmptyCategories(CheatSheetCollectionElement parent) {
       Object[] children = parent.getChildren();
       for (int nX = 0; nX < children.length; nX++) {
          CheatSheetCollectionElement child = (CheatSheetCollectionElement) children[nX];
          pruneEmptyCategories(child);
       }
    }
    

    Here recursive tree traversal is performed without any side-effect.

    Some improvements for RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT were also implemented to reduce false-positives.

     
  • Tagir Valeev

    Tagir Valeev - 2014-11-22
    • status: open-accepted --> closed-fixed
     

Log in to post a comment.