Patch from Oleg Sukhodolsky to add MissingSwitchDefaultCheck to check that a

switch statement has a default clause (request 564199).
This commit is contained in:
Oliver Burn 2003-04-18 11:14:48 +00:00
parent f4fb9cf0fc
commit cd6e8a32d2
10 changed files with 159 additions and 3 deletions

View File

@ -99,5 +99,6 @@
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>
<module name="FinalClass"/>
<module name="MissingSwitchDefault"/>
</module>
</module>

View File

@ -40,6 +40,9 @@
<li>
<a href="#NewlineAtEndOfFile">NewlineAtEndOfFile</a>
</li>
<li>
<a href="#MissingSwitchDefault">MissingSwitchDefault</a>
</li>
<li>
<a href="#SimplifyBooleanExpression">SimplifyBooleanExpression</a>
</li>
@ -290,6 +293,33 @@
&lt;module name=&quot;InnerAssignment&quot;&gt;
&lt;property name=&quot;tokens&quot; value=&quot;ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN&quot;/&gt;
&lt;/module&gt;
</pre>
<h4>Package</h4>
<p class="body">
com.puppycrawl.tools.checkstyle.checks
</p>
<h4>Parent Module</h4>
<p class="body">
<a href="config.html#treewalker">TreeWalker</a>
</p>
<!-- --> <a name="MissingSwitchDefault"></a> <h2>MissingSwitchDefault</h2> <h4>Description</h4>
<p class="body">
Checks that switch statement has &quot;default&quot; clause.
</p>
<p class="body">
Rationale: It's usually a good idea to introduce a default case
in every switch statement. Even if the developer is sure that
all currently possible cases are covered, this should be
expressed in the default branch, e.g. by using an
assertion. This way the code is protected aginst later changes,
e.g. introduction of new types in an enumeration type.
</p>
<h4>Examples</h4>
<p class="body">
To configure the check:
</p>
<pre class="body">
&lt;module name=&quot;MissingSwitchDefault&quot;/&gt;
</pre>
<h4>Package</h4>
<p class="body">

View File

@ -104,6 +104,10 @@
check that classes are declared final if they only contain private
constructors (request 696290).</li>
<li class="body">Patch from Oleg Sukhodolsky to add
MissingSwitchDefaultCheck to check that a switch statement has a
default clause (request 564199).</li>
</ul>
<p class="body">

View File

@ -115,6 +115,9 @@
<!-- declare classes as final if they only have private constructors -->
<module name="FinalClass"/>
<!-- check for default case in switch statement -->
<module name="MissingSwitchDefault"/>
</module>
<!-- enforce package documentation -->

View File

@ -82,7 +82,7 @@ public class IllegalInstantiationCheck
TokenTypes.PACKAGE_DEF
};
}
/**
* Prevent user from changing tokens in the configuration.
* @see com.puppycrawl.tools.checkstyle.api.Check
@ -91,7 +91,7 @@ public class IllegalInstantiationCheck
{
return new int[] {};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getRequiredTokens()
{
@ -101,7 +101,7 @@ public class IllegalInstantiationCheck
TokenTypes.PACKAGE_DEF
};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void beginTree(DetailAST aRootAST)
{

View File

@ -0,0 +1,69 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* <p>
* Checks that switch statement has &quot;default&quot; clause.
* </p>
* <p>
* Rationale: It's usually a good idea to introduce a
* default case in every switch statement. Even if
* the developer is sure that all currently possible
* cases are covered, this should be expressed in the
* default branch, e.g. by using an assertion. This way
* the code is protected aginst later changes, e.g.
* introduction of new types in an enumeration type.
* </p>
* <p>
* An example of how to configure the check is:
* </p>
* <pre>
* &lt;module name="MissingSwitchDefault"/&gt;
* </pre>
* @author o_sukhodolsky
*/
public class MissingSwitchDefaultCheck
extends Check
{
/** @see Check */
public int[] getDefaultTokens()
{
return new int[]{TokenTypes.LITERAL_SWITCH};
}
/** @see Check */
public void visitToken(DetailAST aAst)
{
DetailAST child = aAst.findFirstToken(TokenTypes.CASE_GROUP);
while (child != null) {
if ((child.getType() == TokenTypes.CASE_GROUP)
&& child.branchContains(TokenTypes.LITERAL_DEFAULT))
{
return;
}
child = (DetailAST) child.getNextSibling();
}
log(aAst.getLineNo(), "missing.switch.default");
}
}

View File

@ -79,3 +79,4 @@ doublechecked.locking.avoid=The double-checked locking idiom is broken and shoul
design.forInheritance=Method ''{0}'' is not designed for inheritance - needs to be abstract, final or empty.
final.class=Class {0} should be declared as final
missing.switch.default=switch without \"default\" clause

View File

@ -0,0 +1,20 @@
public class test {
public void foo() {
int i = 1;
switch (i) {
case 1: i++; break;
case 2: i--; break;
default: return;
}
}
}
class bad_test {
public void foo() {
int i = 1;
switch (i) {
case 1: i++; break;
case 2: i--; break;
}
}
}

View File

@ -29,6 +29,7 @@ import com.puppycrawl.tools.checkstyle.checks.LocalVariableNameCheckTest;
import com.puppycrawl.tools.checkstyle.checks.MemberNameCheckTest;
import com.puppycrawl.tools.checkstyle.checks.MethodLengthCheckTest;
import com.puppycrawl.tools.checkstyle.checks.MethodNameCheckTest;
import com.puppycrawl.tools.checkstyle.checks.MissingSwitchDefaultCheckTest;
import com.puppycrawl.tools.checkstyle.checks.ModifierOrderCheckTest;
import com.puppycrawl.tools.checkstyle.checks.NeedBracesCheckTest;
import com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheckTest;
@ -135,6 +136,7 @@ public class AllTests {
suite.addTest(new TestSuite(WhitespaceAroundTest.class));
suite.addTest(new TestSuite(XMLLoggerTest.class));
suite.addTest(new TestSuite(FinalClassCheckTest.class));
suite.addTest(new TestSuite(MissingSwitchDefaultCheckTest.class));
//$JUnit-END$
return suite;
}

View File

@ -0,0 +1,26 @@
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
public class MissingSwitchDefaultCheckTest
extends BaseCheckTestCase
{
private DefaultConfiguration mCheckConfig;
public void setUp()
{
mCheckConfig = createCheckConfig(MissingSwitchDefaultCheck.class);
}
public void testMissingSwitchDefault() throws Exception
{
final String[] expected = {
"15: switch without \"default\" clause",
};
verify(
mCheckConfig,
getPath("InputMissingSwitchDefault.java"),
expected);
}
}