On Wed, Jun 1, 2022 at 11:11 AM Alex Herbert <alex.d.herb...@gmail.com> wrote: > > On Wed, 1 Jun 2022 at 14:04, Gary Gregory <garydgreg...@gmail.com> wrote: > > > Hi, > > > > If you run 'mvn pmd:check' on Commons Text, you'll get: > > > > Note: You can suppress the 'Avoid empty catch blocks' by using 'ignored' as > the exception name. That is an easy fix.
Good tip! Done. Gary > > > > <file > > name="/Users/garydgregory/git/commons-text/src/main/java/org/apache/commons/text/TextStringBuilder.java"> > > <violation beginline="2057" endline="2057" begincolumn="13" > > endcolumn="21" rule="AvoidBranchingStatementAsLastInLoop" > > ruleset="Error Prone" package="org.apache.commons.text" > > class="TextStringBuilder" method="indexOf" > > externalInfoUrl=" > > https://pmd.github.io/pmd-6.46.0/pmd_rules_java_errorprone.html#avoidbranchingstatementaslastinloop > > " > > priority="2"> > > Avoid using a branching statement as the last in a loop. > > </violation> > > > > The maven-pmd-plugin is using the default configuration. So it is not easy > to just drop in a suppression for this. > > You can configure the rule to ignore checking for return statements [1]. > I took the default ruleset [2] and changed the following which suppressed > the violation: > > <rule > ref="category/java/errorprone.xml/AvoidBranchingStatementAsLastInLoop"> > <properties> > <property name="checkReturnLoopTypes" value="" /> > </properties> > </rule> > > You can also suppress this with some xpath. This works and is a finer tool > than a blunt disabling of the rule: > > <rule > ref="category/java/errorprone.xml/AvoidBranchingStatementAsLastInLoop"> > <properties> > <property name="violationSuppressXPath" > value="./ancestor-or-self::MethodDeclaration[@Name='indexOf' or > @Name='lastIndexOf'] > > /ancestor::ClassOrInterfaceDeclaration[@SimpleName='TextStringBuilder' or > @SimpleName='StrBuilder']" /> > </properties> > </rule> > > Or you rewrite the code to remove the return as the last statement in the > loop. This works at one violation site be removing the inner loop to a > method: > > --- > > final char[] thisBuf = buffer; > final int len = size - strLen + 1; > for (int i = startIndex; i < len; i++) { > if (match(str, thisBuf, i, strLen)) { > return i; > } > } > return StringUtils.INDEX_NOT_FOUND; > } > > private static boolean match(String str, char[] buffer, int i, int strLen) { > for (int j = 0; j < strLen; j++) { > if (str.charAt(j) != buffer[i + j]) { > return false; > } > } > return true; > } > > --- > > This adds a method call inside the loop and may be less efficient. I prefer > to add suppressions to the PMD ruleset. But since text is using the default > ruleset then you have some extra work to do in configuring the POM. > > Alex > > > > [1] > https://pmd.github.io/pmd-6.46.0/pmd_rules_java_errorprone.html#avoidbranchingstatementaslastinloop > [2] > https://gitbox.apache.org/repos/asf?p=maven-pmd-plugin.git;a=blob_plain;f=src/main/resources/rulesets/java/maven-pmd-plugin-default.xml;hb=HEAD --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org