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] ConsecutiveLiteralAppends false positive #1645

Merged
merged 2 commits into from Feb 6, 2019

Conversation

prophet1906
Copy link
Contributor

PR Description:
ASTTryStatement added to BLOCK_PARENTS.
fixes #1632

ASTTryStatement added to BLOCK_PARENTS.
@pmd-test
Copy link

pmd-test commented Feb 4, 2019

1 Message
📖 This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 2 configuration errors.
Full report
This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 2 configuration errors.
Full report

Generated by 🚫 Danger

Copy link
Member

@jsotuyod jsotuyod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shubham-2k17 thanks for the PR! This does cover the reported scenario, but I think we can improve it a little to cover some related use cases.

@@ -74,6 +75,7 @@
BLOCK_PARENTS.add(ASTIfStatement.class);
BLOCK_PARENTS.add(ASTSwitchStatement.class);
BLOCK_PARENTS.add(ASTMethodDeclaration.class);
BLOCK_PARENTS.add(ASTTryStatement.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch and finally should be added too. A simple test-case having 2 catch blocks do different appends / a catch + finally doing appends would trigger these FP too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking, we actually may have similar FPs with lambdas, and we should add ASTLambdaExpression.class here…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to check lambdas. No FPs.

@vbrandl
Copy link

vbrandl commented Feb 5, 2019

I don't know the PMD codebase so I don't know what exactly this PR will change, but I would guess, it would just ignore literals, that are appended inside a try/catch block?

So this won't trigger the rule while it should:

final StringBuilder sb = new StringBuilder();
sb.append("foo");
try {
  sb.append("bar");
  final String res = methodThatMightThrow();
  sb.append(res);
} catch (IOException ioe) {
  ...
}

This is only a false positive, if the literal is appended inside the try/catch block, after a throwing method was called. I don't know if this case is covered by the PR.

@prophet1906
Copy link
Contributor Author

Hi @vbrandl,
The PR is currently not covering the case where method throwing exception comes after append in try catch.

@prophet1906
Copy link
Contributor Author

Hi @jsotuyod ,
I think we should not add ASTTryStatement, ASTCatchStatement, ASTFinallyStatement to BLOCK_PARENTS & check if the parameter inside append is a literal/constant expression. This may solve the problem. Do suggest what is the correct way.

@jsotuyod
Copy link
Member

jsotuyod commented Feb 6, 2019

@Shubham-2k17 @vbrandl interesting! I think you are actually right. ASTTryStatement shouldn't be in the block parents, but catch / finally do.

I'm updating, adding that test case along with the lambda ones (which works as already stated), and merging.

@jsotuyod jsotuyod added this to the 6.12.0 milestone Feb 6, 2019
@jsotuyod jsotuyod merged commit 709f9ed into pmd:master Feb 6, 2019
jsotuyod added a commit that referenced this pull request Feb 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[java] ConsecutiveLiteralAppends false positive over catch
4 participants