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 false-positive with local method name confusion #3977

Closed
adangel opened this issue May 25, 2022 · 1 comment · Fixed by #4154
Closed

[java] StringToString false-positive with local method name confusion #3977

adangel opened this issue May 25, 2022 · 1 comment · Fixed by #4154
Assignees
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@adangel
Copy link
Member

adangel commented May 25, 2022

Affects PMD Version: 6.43.0

Rule: StringToString

Description:

When you have a method chain call and at the end the toString() call. And there is a local method with the same name as the method call, then this rule is confused. If the local method returns a String, then this rule is triggered.

The rule searches always for local method declarations, but that's wrong - at least for method chains.

Code Sample demonstrating the issue:

public class StringToString_FP {
  private String s;

  public void print(A a) {
    this.s = a.getB().getC().toString(); // line 5 - false positive
    this.s = String.valueOf(a.getB().getC()); // workaround
    System.out.println(s);
  }

  public String getC() {
    return "";
  }
}

interface A {
    B getB();
}

interface B {
    Character getC();
}

B::getC returns a Character, StringToString_FP::getC returns a String.
At line 5, B::getC is called.

Expected outcome:

PMD reports a violation at line 5, but that's wrong. That's a false positive.

@adangel adangel added the a:false-positive PMD flags a piece of code that is not problematic label May 25, 2022
@adangel
Copy link
Member Author

adangel commented May 25, 2022

Similar to #2080 - here it is a method declaration, there it is a field declaration.

@adangel adangel self-assigned this Oct 12, 2022
adangel added a commit to adangel/pmd that referenced this issue Oct 13, 2022
@adangel adangel added this to the 6.51.0 milestone 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant