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

Closed
niktekusho opened this issue Oct 23, 2019 · 1 comment · Fixed by #4154
Closed

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

niktekusho opened this issue Oct 23, 2019 · 1 comment · Fixed by #4154
Labels
a:false-positive PMD flags a piece of code that is not problematic in:symbol-table Affects the symbol table code
Milestone

Comments

@niktekusho
Copy link

niktekusho commented Oct 23, 2019

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)

@jsotuyod jsotuyod added a:bug PMD crashes or fails to analyse a file. in:symbol-table Affects the symbol table code labels Oct 23, 2019
@oowekyala oowekyala added a:false-positive PMD flags a piece of code that is not problematic and removed a:bug PMD crashes or fails to analyse a file. labels Jan 17, 2021
@oowekyala oowekyala added this to the 7.0.0 milestone Mar 6, 2022
@adangel adangel changed the title [java] StringToString rule false-positive [java] StringToString rule false-positive with field access Oct 13, 2022
@adangel
Copy link
Member

adangel commented Oct 13, 2022

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>

adangel added a commit to adangel/pmd that referenced this issue Oct 13, 2022
adangel added a commit to adangel/pmd that referenced this issue Oct 13, 2022
@adangel adangel modified the milestones: 7.0.0, 6.51.0 Oct 13, 2022
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 in:symbol-table Affects the symbol table code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants