Skip to content

[java] UnnecessaryLocalBeforeReturn false positive when cloning Maps #282

Closed

Description

@posto

Rule Set:

    <rule ref="rulesets/java/basic.xml"/>
    <rule ref="rulesets/java/braces.xml"/>
    <rule ref="rulesets/java/codesize.xml"/>

    <rule ref="rulesets/java/coupling.xml">
        <exclude name="LawOfDemeter"/>
        <exclude name="LoosePackageCoupling"/>
    </rule>

    <rule ref="rulesets/java/design.xml">
        <exclude name="UseVarargs"/>
        <exclude name="UseUtilityClass"/>
        <exclude name="SingularField"/>
        <exclude name="CloseResource"/>
    </rule>

    <rule ref="rulesets/java/empty.xml"/>
    <rule ref="rulesets/java/imports.xml"/>

    <rule ref="rulesets/java/naming.xml/LongVariable" />
    <rule ref="rulesets/java/naming.xml/ClassNamingConventions" />
    <rule ref="rulesets/java/naming.xml/MethodWithSameNameAsEnclosingClass" />
    <rule ref="rulesets/java/naming.xml/SuspiciousHashcodeMethodName" />
    <rule ref="rulesets/java/naming.xml/SuspiciousConstantFieldName" />
    <rule ref="rulesets/java/naming.xml/SuspiciousEqualsMethodName" />
    <rule ref="rulesets/java/naming.xml/AvoidFieldNameMatchingTypeName" />
    <rule ref="rulesets/java/naming.xml/PackageCase" />
    <rule ref="rulesets/java/naming.xml/BooleanGetMethodName" />

    <rule ref="rulesets/java/optimizations.xml/UseArrayListInsteadOfVector" />
    <rule ref="rulesets/java/optimizations.xml/UseArraysAsList" />
    <rule ref="rulesets/java/optimizations.xml/AvoidArrayLoops" />
    <rule ref="rulesets/java/optimizations.xml/UnnecessaryWrapperObjectCreation" />

    <rule ref="rulesets/java/typeresolution.xml/LooseCoupling"/>
    <rule ref="rulesets/java/typeresolution.xml/UnusedImports"/>

    <rule ref="rulesets/java/unnecessary.xml"/>

Description:
False positive when making a clone of a hashmap

Code Sample demonstrating the issue:

public class CustomerErrorCollector {

    private final ConcurrentHashMap<String, String> customerErrors = new ConcurrentHashMap<>();

    public void error(String customerNr, String errorMsg) {
        customerErrors.put(customerNr, errorMsg);
    }

    public Map<String, String> getAndReset() {
        final Map<String, String> copy = new HashMap<>(customerErrors);
        customerErrors.clear();
        return copy; // PMD complains that variable could be avoided
    }


}

Running PMD through: Gradle

Activity

  1. added
    a:bug
    on Mar 2, 2017
  2. added this to the 5.5.5 milestone on Mar 2, 2017
  3. self-assigned this
    on Mar 2, 2017
  4. jsotuyod commented on Mar 2, 2017

    @jsotuyod
    Member

    @posto thanks for the report! I'll look into it.

    One little advice though, your code seems to try to be thread-safe (uses ConcurrentHashMap) but isn't. Items added by another thread between copying all elements to copy and calling clear() would be lost. You would probably rather do customerErrors.keySet().removeAll(copy.keySet()) instead of simply calling clear() to avoid this.

  5. NickButcher1 commented on Mar 2, 2017

    @NickButcher1

    I see many false positives with UnnecessaryLocalBeforeReturn after upgrading PMD 5.5.3 -> 5.5.4.

    For example:

      public String getAndResetSomeString()
      {
        String temp = mSomeString;
        mSomeString = null;
        return temp;
      }
    

    And (simplified from my actual code):

    int count = cursor.getCount();
    cursor.close();
    return count;
    

    Thanks for your help.

  6. jsotuyod commented on Mar 13, 2017

    @jsotuyod
    Member

    Just submitted a PR fixing these scenarios, should be part of the next PMD release.

  7. added 2 commits that reference this issue on Mar 18, 2017
    8e9f938
    bdd6f20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions