Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] [7.0-rc1] UnusedAssignment for used field #4435

Closed
ben-manes opened this issue Mar 26, 2023 · 1 comment · Fixed by #4535
Closed

[java] [7.0-rc1] UnusedAssignment for used field #4435

ben-manes opened this issue Mar 26, 2023 · 1 comment · Fixed by #4535
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@ben-manes
Copy link

Affects PMD Version:
7.0-rc1 when upgrading from 6.55

Rule:
UnusedAssignment

Description:
A class field is not the same as a method variable as it is in scope for other method calls. This also only restores the value when an unhandled error occurs to rollback the state, so the detection is misunderstanding the behavior.

Code Sample demonstrating the issue:

public void advance(BoundedLocalCache<K, V> cache, long currentTimeNanos) {
  long previousTimeNanos = nanos;
  nanos = currentTimeNanos;

  try {
    // ...
  } catch (Throwable t) {
    nanos = previousTimeNanos;
    throw t;
  }
}

Expected outcome:
PMD reports a violation but that should not apply to a class field.

# File Line Problem
1 TimerWheel.java 90 The value assigned to field 'nanos' is never used (overwritten on line 111)

Running PMD through: Gradle
This requires workaround in gradle/gradle#24502

@ben-manes ben-manes added the a:false-positive PMD flags a piece of code that is not problematic label Mar 26, 2023
@jsotuyod jsotuyod added this to the 7.0.0 milestone Mar 31, 2023
oowekyala added a commit to oowekyala/pmd that referenced this issue May 2, 2023
@harbulot
Copy link

Not sure if I should open a different issue, but I've noticed a very similar false-positive for UnusedAssigment in cases where it assumes that something won't be null in the control flow that follows (no exception catching involved).

Here is a full example (slightly contrived), using maven-pmd-plugin version 3.20.1-pmd-7-SNAPSHOT configured with PMD 7.0.0-rc2:

package com.example;

import java.util.Iterator;
import java.util.List;
import java.util.Arrays;

public class Example {    
    private int testResult;
    private List<Integer> testList;
    
    public int getTestResult() {
        return testResult;
    }
    
    public List<Integer> getTestList() {
        return testList;
    }
    
    public void setTestList(List<Integer> testList) {
        this.testList = testList;
    }
    
    public void run() {
        testResult = -1; // This is flagged as UnusedAssigment
        if (testList == null) {
            return;
        }
        
        int i = 0;
        Iterator<Integer> it = testList.iterator();
        while(it.hasNext()) {
            Integer value = it.next();
            if (value != null && value > 10) {
                testResult = i;
                break;
            }
            i++;
        }
    }
}

This flags the assignment at line 24 (testResult = -1;) as UnusedAssigment. That's of course not necessarily the case if testList is indeed null.

I've also seen this false positive in a piece of code that goes through a conditional while loop in a similar way, along these lines:

// Sample data structure
public class Node {
    String value;
    Node nextNode;
}

public class Example {
    private String matchingEntry;
    
    private String getMatchingEntry() { return matchingEntry; }
    
    private void findPosition() {
        matchingEntry = null; // This is flagged as UnusedAssigment, even if it doesn't know the result of loadNodeChain()
        
        Node head = loadNodeChain(); // May or may not be null...
        while (head != null) {
            if (head.getValue() != null && head.getValue().startsWith("a")) {
                matchingEntry = head.getValue();
                break;
            }
            head = head.nextNode();
        }
    }
}

@jsotuyod jsotuyod added needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale and removed needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale labels Mar 17, 2024
@adangel adangel modified the milestones: 7.0.0, 7.0.1, 7.1.0 Mar 21, 2024
@adangel adangel modified the milestones: 7.1.0, 7.0.1 Apr 3, 2024
@adangel adangel closed this as completed in 8edbb06 Apr 3, 2024
adangel added a commit that referenced this issue Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants