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] StringToString rule false-positive with field access · Issue #2080 · pmd/pmd

[java] StringToString rule false-positive with field access #2080

New issue
Copy link
New issue
Copy link
Closed
#4154
Closed
[java] StringToString rule false-positive with field access#2080
#4154
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematicin:symbol-tableAffects the symbol table code
Milestone
6.51.0

Description

@niktekusho
niktekusho
opened on Oct 23, 2019
Issue body actions

Affects PMD Version: 6.18.0

Rule: StringToString

Description:

Attached code should help to understand better the following description.

We found a false-positive case for the rule String.toString().
When you assign an object field (of type String) to a field of another object (not of type String) calling toString(), the rule emits a warning about calling toString on a String object only when the field names match (value = otherObj.value.toString()). If either one of the field name changes, Pmd stops emitting the warning (workaround).

Since the enum is not a String object, we think that the warning is a false-positive triggered by the field name equality.

Code Sample demonstrating the issue:

See attached project for a ready-to-go reproduction case.

public enum AnEnum {

	A,
	B,
	C;

}

public class AnEnumUser {

	private final String value;

	public AnEnumUser(final OtherEnumUser otherUser) {
                // The following line triggers the "false-positive" warning
		value = otherUser.value.toString();
	}

	public String getValue() {
		return value;
	}

}

public class OtherEnumUser {
	public final AnEnum value;

	public OtherEnumUser(final AnEnum value) {
		this.value = value;
	}
}

public final class Main {
	private Main() {
	}

	public static void main(final String[] args) {
		final OtherEnumUser a = new OtherEnumUser(AnEnum.A);
		System.out.println("Original " + a.value);

		final AnEnumUser aCopy = new AnEnumUser(a);
		System.out.println("Copy " + aCopy.getValue());

	}
}

Running PMD through: Gradle

Attached reproduction project (forgive the eclipse files)

Activity

  1. jsotuyod
    added
    a:bugPMD crashes or fails to analyse a file.
    in:symbol-tableAffects the symbol table code
    on Oct 23, 2019
  2. oowekyala
    added
    a:false-positivePMD flags a piece of code that is not problematic
    and removed
    a:bugPMD crashes or fails to analyse a file.
    on Jan 17, 2021
  3. oowekyala
    added this to the 7.0.0 milestone on Mar 6, 2022
  4. adangel
    changed the title [-][java] StringToString rule false-positive[/-] [+][java] StringToString rule false-positive with field access[/+] on Oct 13, 2022
  5. adangel commented on Oct 13, 2022

    @adangel
    adangel
    on Oct 13, 2022
    Member
    More actions

    Shorter reproduction sample:

        <test-code>
            <description>[java] StringToString rule false-positive with field access #2080</description>
            <expected-problems>0</expected-problems>
            <code><![CDATA[
    public final class Issue2080 {
        String value;
    
        public void foo() {
            A a = new A();
            value = a.value.toString(); // false positive
        }
    
        class A {
            B value;
        }
    
        class B { }
    }
    ]]></code>
        </test-code>
  6. adangel
    added 2 commits that reference this issue on Oct 13, 2022

    [java] StringToString - use type resolution for field access

    This issue will close once commit 5d08ecd is merged into the 'main' branch.
    5d08ecd

    [doc] Update release notes (pmd#2080, pmd#3437, pmd#3681, pmd#3977)

    This issue will close once commit 39c26d9 is merged into the 'main' branch.
    39c26d9
  7. adangel
    modified the milestones: 7.0.0, 6.51.0 on Oct 13, 2022
  8. adangel
    closed this as completedin #4154on Oct 28, 2022
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 problematicin:symbol-tableAffects the symbol table code

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.51.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.