Merge pull request #4871 from oowekyala:issue3953-treat-formal-comments-as-normal-comments

[apex] EmptyCatchBlock: Fix FP with doc comments #4871
This commit is contained in:
Andreas Dangel
2024-04-04 16:53:37 +02:00
3 changed files with 16 additions and 18 deletions

View File

@ -23,6 +23,8 @@ This is a {{ site.pmd.release_type }} release.
### 🐛 Fixed Issues
* cli
* [#4791](https://github.com/pmd/pmd/issues/4791): \[cli] Could not find or load main class
* apex-errorprone
* [#3953](https://github.com/pmd/pmd/issues/3953): \[apex] EmptyCatchBlock false positive with formal (doc) comments
* java-bestpractices
* [#1084](https://github.com/pmd/pmd/issues/1084): \[java] Allow JUnitTestsShouldIncludeAssert to configure verification methods
* [#4435](https://github.com/pmd/pmd/issues/4435): \[java] \[7.0-rc1] UnusedAssignment for used field

View File

@ -4,7 +4,6 @@
package net.sourceforge.pmd.lang.apex.ast;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import java.util.AbstractList;
@ -43,7 +42,7 @@ final class ApexCommentBuilder {
TextRegion nodeRegion = commentContainer.getTextRegion();
// find the first comment after the start of the container node
int index = Collections.binarySearch(commentInfo.nonDocTokensByStartIndex, nodeRegion.getStartOffset());
int index = Collections.binarySearch(commentInfo.allCommentsByStartIndex, nodeRegion.getStartOffset());
// no exact hit found - this is expected: there is no comment token starting at
// the very same index as the node
@ -52,8 +51,8 @@ final class ApexCommentBuilder {
index = ~index;
// now check whether the next comment after the node is still inside the node
if (index >= 0 && index < commentInfo.nonDocTokensByStartIndex.size()) {
int commentStartIndex = commentInfo.nonDocTokensByStartIndex.get(index);
if (index >= 0 && index < commentInfo.allCommentsByStartIndex.size()) {
int commentStartIndex = commentInfo.allCommentsByStartIndex.get(index);
return nodeRegion.getStartOffset() < commentStartIndex
&& nodeRegion.getEndOffset() >= commentStartIndex;
}
@ -141,19 +140,16 @@ final class ApexCommentBuilder {
private static class CommentInformation {
final Map<Integer, String> suppressMap;
final List<Integer> nonDocTokensByStartIndex;
final List<Integer> allCommentsByStartIndex;
final List<ApexDocToken> docTokens;
CommentInformation(Map<Integer, String> suppressMap, List<Token> allCommentTokens) {
this.suppressMap = suppressMap;
this.docTokens = allCommentTokens.stream()
.filter((token) -> token.getType() == ApexLexer.DOC_COMMENT)
.map((token) -> new ApexDocToken(token))
.filter(token -> token.getType() == ApexLexer.DOC_COMMENT)
.map(ApexDocToken::new)
.collect(toList());
this.nonDocTokensByStartIndex = new TokenListByStartIndex(
allCommentTokens.stream()
.filter((token) -> token.getType() != ApexLexer.DOC_COMMENT)
.collect(toCollection(ArrayList::new)));
this.allCommentsByStartIndex = new TokenListByStartIndex(new ArrayList<>(allCommentTokens));
}
}

View File

@ -297,27 +297,27 @@ private class FunctionalityTest {
///////////////////////////////
try {
} catch (Exception e) {
/** NOK: doc comment inside of empty catch block; should be reported */
/** OK: doc comment inside of empty catch block; should be reported */
}
try {
} catch (Exception e) {
/** NOK: doc comment inside of empty catch block;
/** OK: doc comment inside of empty catch block;
* multiple lines
* should be reported
*/
}
try {
} catch (Exception e) { /** NOK: doc comment inside of empty catch block, same line as begin; should be reported */
} catch (Exception e) { /** OK: doc comment inside of empty catch block, same line as begin; should be reported */
}
try {
} catch (Exception e) {
/** NOK: doc comment inside of empty catch block, same line as end; should be reported */ }
/** OK: doc comment inside of empty catch block, same line as end; should be reported */ }
try {
} catch (Exception e) { /** NOK: doc comment inside catch block, same line as begin/end; should be reported */ }
} catch (Exception e) { /** OK: doc comment inside catch block, same line as begin/end; should be reported */ }
}
}
]]></code-fragment>
@ -333,8 +333,8 @@ private class FunctionalityTest {
<test-code>
<description>#3569 - Verify use of allowCommentedBlocks=true, binary search boundaries verification</description>
<rule-property name="allowCommentedBlocks">true</rule-property>
<expected-problems>9</expected-problems>
<expected-linenumbers>19,23,54,58,65,70,78,82,86</expected-linenumbers>
<expected-problems>4</expected-problems>
<expected-linenumbers>19,23,54,58</expected-linenumbers>
<code-ref id="with-comments"/>
</test-code>
</test-data>