GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] [7.0-rc1] UnusedAssignment for used field · Issue #4435 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#4535
Closed
[java] [7.0-rc1] UnusedAssignment for used field#4435
#4535
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.1.0

Description

@ben-manes
ben-manes
opened on Mar 26, 2023
Issue body actions

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

Activity

  1. ben-manes
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Mar 26, 2023
  2. jsotuyod
    added this to the 7.0.0 milestone on Mar 31, 2023
  3. harbulot commented on May 10, 2023

    @harbulot
    harbulot
    on May 10, 2023
    More actions

    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();
            }
        }
    }
  4. jsotuyod
    added
    needs:pmd7-revalidationThe issue hasn't yet been retested vs PMD 7 and may be stale
    and removed
    needs:pmd7-revalidationThe issue hasn't yet been retested vs PMD 7 and may be stale
    on Mar 17, 2024
  5. adangel
    modified the milestones: 7.0.0, 7.0.1, 7.1.0 on Mar 21, 2024
  6. adangel
    modified the milestones: 7.1.0, 7.0.1 on Apr 3, 2024
  7. adangel
    closed this as completedin 8edbb06on Apr 3, 2024
  8. adangel
    added a commit that references this issue on Apr 3, 2024

    Merge pull request #4535 from oowekyala:issue4435-unusedAssignment

    This issue will close once commit 07d96e7 is merged into the 'main' branch.
    07d96e7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.1.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.