Menu

#933 [java] UnnecessaryLocalBeforeReturn false positive for SuppressWarnings annotation

PMD-5.5.4
closed
pmd (543)
PMD
1-Blocker
Bug
2017-02-19
2010-06-21
No

Effective Java, Second Edition recommends (Item 24) to suppress warnings when necessary, but at the smallest scope possible. Sometimes this means the need to declare a local variable that would normally be flagged by this rule. Would be nice to enhance this rule such that variables with such a SuppressWarnings do not trigger the rule. Here is an example function.

private <T extends UserVisibleEventTypeValue<?>> T findEventTypeValueByName(Class<T> entityClass, String eventTypeName, String eventTypeValueName) {
    Criteria criteria = this.session.createCriteria(entityClass)
        .add(Restrictions.eq(UserVisibleEventTypeValue_.internalName, eventTypeValueName))
        .createCriteria(UserVisibleEventTypeValue_.type)
            .add(Restrictions.eq(UserVisibleEventType_.name, eventTypeName))
            .setCacheable(true)
    ;

    @SuppressWarnings("unchecked")
    T result = (T) criteria.uniqueResult();
    return result;        
}

The result variable is triggering this. It is prefered to do this however rather than put the annotation on the method level.

Related

Issues: #848

Discussion

  • Andreas Dangel

    Andreas Dangel - 2017-02-16
    • summary: UnnecessaryLocalBeforeReturn allow for Warning supression --> [java] UnnecessaryLocalBeforeReturn false positive for SuppressWarnings annotation
    • Module: --> PMD
    • Milestone: --> New Tickets
    • Priority: 5 --> 1-Blocker
    • Type: --> Bug
    • Affects version: -->
    • Ruleset / Rule: -->
     
  • Andreas Dangel

    Andreas Dangel - 2017-02-16
    • status: open --> in-progress
    • assigned_to: Tom Copeland --> Andreas Dangel
    • Milestone: New Tickets --> PMD-5.4.5
     
  • Andreas Dangel

    Andreas Dangel - 2017-02-16
     
  • Andreas Dangel

    Andreas Dangel - 2017-02-19
    • status: in-progress --> closed
    • Milestone: PMD-5.4.5 --> PMD-5.5.4
     

Log in to post a comment.