FallThrough check now can check last case group (property checkLastCaseGroup, bug 1245942).
This commit is contained in:
parent
73e2e24fca
commit
d46dc44e79
|
|
@ -38,6 +38,9 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
|||
*/
|
||||
public class FallThroughCheck extends Check
|
||||
{
|
||||
/** Do we need to check last case group. */
|
||||
private boolean mCheckLastGroup;
|
||||
|
||||
/** Creates new instance of the check. */
|
||||
public FallThroughCheck()
|
||||
{
|
||||
|
|
@ -56,19 +59,35 @@ public class FallThroughCheck extends Check
|
|||
return getDefaultTokens();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures whether we need to check last case group or not.
|
||||
* @param aValue new value of the property.
|
||||
*/
|
||||
public void setCheckLastCaseGroup(boolean aValue)
|
||||
{
|
||||
mCheckLastGroup = aValue;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void visitToken(DetailAST aAST)
|
||||
{
|
||||
final DetailAST nextGroup = (DetailAST) aAST.getNextSibling();
|
||||
if (nextGroup == null || nextGroup.getType() != TokenTypes.CASE_GROUP) {
|
||||
// last group we shouldn't check it
|
||||
final boolean isLastGroup =
|
||||
(nextGroup == null || nextGroup.getType() != TokenTypes.CASE_GROUP);
|
||||
if (isLastGroup && !mCheckLastGroup) {
|
||||
// we do not need to check last group
|
||||
return;
|
||||
}
|
||||
|
||||
final DetailAST slist = aAST.findFirstToken(TokenTypes.SLIST);
|
||||
|
||||
if (!isTerminated(slist, true, true)) {
|
||||
log(nextGroup, "fall.through");
|
||||
if (!isLastGroup) {
|
||||
log(nextGroup, "fall.through");
|
||||
}
|
||||
else {
|
||||
log(aAST, "fall.through.last");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ explicit.init=Variable ''{0}'' explicitly initialized to ''{1}'' (default value
|
|||
default.comes.last=Default should be last label in the switch.
|
||||
missing.ctor=Class should define a constructor.
|
||||
fall.through=Fall through from previous branch of the switch statement.
|
||||
fall.through.last=Fall through from the last branch of the switch statement.
|
||||
require.this.variable=Reference to instance variable ''{0}'' needs \"this.\".
|
||||
require.this.unfound.variable=Unable find where ''{0}'' is declared.
|
||||
require.this.method=Method call to ''{0}'' needs \"this.\".
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ public class InputFallThrough
|
|||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
case 24:
|
||||
switch (j) {
|
||||
case 1:
|
||||
|
|
@ -117,7 +117,7 @@ public class InputFallThrough
|
|||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
default: //fall through!!!
|
||||
// this is the last label
|
||||
i++;
|
||||
|
|
|
|||
|
|
@ -7,11 +7,33 @@ import java.io.File;
|
|||
|
||||
public class FallThroughCheckTest extends BaseCheckTestCase
|
||||
{
|
||||
public void testIt()
|
||||
throws Exception
|
||||
private DefaultConfiguration checkConfig;
|
||||
|
||||
public void setUp()
|
||||
{
|
||||
checkConfig = createCheckConfig(FallThroughCheck.class);
|
||||
}
|
||||
|
||||
public void testIt() throws Exception
|
||||
{
|
||||
checkConfig.addAttribute("checkLastCaseGroup", "true");
|
||||
final String[] expected = {
|
||||
"12:13: Fall through from previous branch of the switch statement.",
|
||||
"36:13: Fall through from previous branch of the switch statement.",
|
||||
"51:13: Fall through from previous branch of the switch statement.",
|
||||
"68:13: Fall through from previous branch of the switch statement.",
|
||||
"85:13: Fall through from previous branch of the switch statement.",
|
||||
"103:13: Fall through from previous branch of the switch statement.",
|
||||
"121:13: Fall through from previous branch of the switch statement.",
|
||||
"121:13: Fall through from the last branch of the switch statement.",
|
||||
};
|
||||
verify(checkConfig,
|
||||
getPath("coding" + File.separator + "InputFallThrough.java"),
|
||||
expected);
|
||||
}
|
||||
|
||||
public void testDefault() throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(FallThroughCheck.class);
|
||||
final String[] expected = {
|
||||
"12:13: Fall through from previous branch of the switch statement.",
|
||||
"36:13: Fall through from previous branch of the switch statement.",
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ public class ParenPadCheckTest
|
|||
};
|
||||
verify(checkConfig, getPath("InputWhitespace.java"), expected);
|
||||
}
|
||||
|
||||
|
||||
public void testDefaultForIterator()
|
||||
throws Exception
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1863,6 +1863,25 @@ if ("something".equals(x))
|
|||
</p>
|
||||
</subsection>
|
||||
|
||||
<subsection name="Properties">
|
||||
<table>
|
||||
<tr>
|
||||
<th>name</th>
|
||||
<th>description</th>
|
||||
<th>type</th>
|
||||
<th>default value</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>checkLastCaseGroup</td>
|
||||
<td>
|
||||
Whether we need to check last case group or not.
|
||||
</td>
|
||||
<td><a href="property_types.html#boolean">Boolean</a></td>
|
||||
<td><span class="default">false</span></td>
|
||||
</tr>
|
||||
</table>
|
||||
</subsection>
|
||||
|
||||
<subsection name="Examples">
|
||||
<p>
|
||||
To configure the check:
|
||||
|
|
|
|||
|
|
@ -36,6 +36,10 @@
|
|||
classloader of this class can be used to load the correct
|
||||
resource bundle. Patch 1309516 from Ralf (rakus).
|
||||
</li>
|
||||
<li>
|
||||
FallThrough check now can check last case group (property
|
||||
checkLastCaseGroup, bug 1245942).
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Fixed Bugs:</p>
|
||||
|
|
|
|||
Loading…
Reference in New Issue