Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wangsun300
p3c
提交
42ce06fd
P
p3c
项目概览
wangsun300
/
p3c
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
p3c
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
42ce06fd
编写于
3月 15, 2018
作者:
曾
曾候
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
bugfix: default中包含return, throw时漏报
上级
10f38486
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
139 addition
and
134 deletion
+139
-134
p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/SwitchStatementRule.java
...c/pmd/lang/java/rule/flowcontrol/SwitchStatementRule.java
+15
-11
p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/xml/SwitchStatementRule.xml
...md/lang/java/rule/flowcontrol/xml/SwitchStatementRule.xml
+124
-123
未找到文件。
p3c-pmd/src/main/java/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/SwitchStatementRule.java
浏览文件 @
42ce06fd
...
...
@@ -46,7 +46,7 @@ public class SwitchStatementRule extends AbstractAliRule {
* @param data
*/
private
void
checkDefault
(
ASTSwitchStatement
node
,
Object
data
)
{
final
String
switchCheckXpath
=
"SwitchLabel[@Default
=
'true']"
;
final
String
switchCheckXpath
=
"SwitchLabel[@Default
=
'true']"
;
if
(!
node
.
hasDescendantMatchingXPath
(
switchCheckXpath
))
{
addViolationWithMessage
(
data
,
node
,
MESSAGE_KEY_PREFIX
+
".nodefault"
);
}
...
...
@@ -60,16 +60,20 @@ public class SwitchStatementRule extends AbstractAliRule {
*/
private
void
checkFallThrough
(
ASTSwitchStatement
node
,
Object
data
)
{
// refer the rule MissingBreakInSwitch of PMD
final
String
xpath
=
"../SwitchStatement[(count(.//BreakStatement)"
+
" + count(BlockStatement//Statement/ReturnStatement)"
+
" + count(BlockStatement//Statement/ThrowStatement)"
+
" + count(BlockStatement//Statement/IfStatement[@Else='true' and "
+
"Statement[2][ReturnStatement|ThrowStatement]]"
+
"/Statement[1][ReturnStatement|ThrowStatement])"
+
" + count(SwitchLabel[name(following-sibling::node()) = 'SwitchLabel'])"
+
" + count(SwitchLabel[count(following-sibling::node()) = 0])"
+
" < count (SwitchLabel[@Default != 'true']))]"
;
final
String
xpath
=
"../SwitchStatement[(count(.//BreakStatement)"
+
" + count(BlockStatement//Statement/ReturnStatement)"
+
" + count(BlockStatement//Statement/ContinueStatement)"
+
" + count(BlockStatement//Statement/ThrowStatement)"
+
" + count(BlockStatement//Statement/IfStatement[@Else='true'"
+
" and Statement[2][ReturnStatement|ContinueStatement|ThrowStatement]]"
+
"/Statement[1][ReturnStatement|ContinueStatement|ThrowStatement])"
+
" + count(SwitchLabel[name(following-sibling::node()) = 'SwitchLabel'])"
+
" + count(SwitchLabel[count(following-sibling::node()) = 0])"
+
" < count (SwitchLabel[@Default != 'true'])"
+
" + count(SwitchLabel[@Default = 'true']/following-sibling::BlockStatement//Statement/ReturnStatement)"
+
" + count(SwitchLabel[@Default = 'true']/following-sibling::BlockStatement//Statement/ContinueStatement)"
+
" + count(SwitchLabel[@Default = 'true']/following-sibling::BlockStatement//Statement/ThrowStatement)"
+
")]"
;
if
(
node
.
hasDescendantMatchingXPath
(
xpath
))
{
addViolationWithMessage
(
data
,
node
,
MESSAGE_KEY_PREFIX
+
".notermination"
);
...
...
p3c-pmd/src/test/resources/com/alibaba/p3c/pmd/lang/java/rule/flowcontrol/xml/SwitchStatementRule.xml
浏览文件 @
42ce06fd
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<code-fragment
id=
"switch-no-default-1"
>
<![CDATA[
public class Example {
public void fn() {
int i;
switch (i) {
case 0:
break;
case 1:
int j;
switch (j) {
case 0:
break;
default:
// nested switch has default blocks
return;
}
break;
// missing default statement
}
}
}
]]>
</code-fragment>
<test-code>
<description>
switch statement in outer class has no default block
</description>
<expected-problems>
1
</expected-problems>
<expected-linenumbers>
4
</expected-linenumbers>
<code-ref
id=
"switch-no-default-1"
/>
</test-code>
<code-fragment
id=
"switch-no-default-1"
>
<![CDATA[
public class Example {
public void fn() {
int i;
switch (i) {
case 0:
break;
case 1:
int j;
switch (j) {
case 0:
break;
default:
// nested switch has default blocks
return;
}
break;
// missing default statement
}
}
}
]]>
</code-fragment>
<test-code>
<description>
switch statement in outer class has no default block
</description>
<expected-problems>
1
</expected-problems>
<expected-linenumbers>
4
</expected-linenumbers>
<code-ref
id=
"switch-no-default-1"
/>
</test-code>
<!-- ====================================================================== -->
<code-fragment
id=
"switch-no-default-2"
>
<![CDATA[
public class Foo {
public void bar() {
int i;
switch (i) {
case 0:
break;
case 1:
int j;
switch (j) {
case 0:
break;
// nested switch has no default block
}
break;
default:
return;
}
}
}
]]>
</code-fragment>
<test-code>
<description>
nested switch has no default block
</description>
<expected-problems>
1
</expected-problems>
<expected-linenumbers>
9
</expected-linenumbers>
<code-ref
id=
"switch-no-default-2"
/>
</test-code>
<code-fragment
id=
"switch-no-default-2"
>
<![CDATA[
public class Foo {
public void bar() {
int i;
switch (i) {
case 0:
break;
case 1:
int j;
switch (j) {
case 0:
break;
// nested switch has no default block
}
break;
default:
return;
}
}
}
]]>
</code-fragment>
<test-code>
<description>
nested switch has no default block
</description>
<expected-problems>
1
</expected-problems>
<expected-linenumbers>
9
</expected-linenumbers>
<code-ref
id=
"switch-no-default-2"
/>
</test-code>
<!-- ====================================================================== -->
<code-fragment
id=
"switch-case-no-break"
>
<![CDATA[
public class Foo {
public void bar() {
int i;
switch (i) {
case 0:
int j =1;
// missing break,return and continue
case 1:
break;
case 2:
continue;
case 3: // OK
default:
return;
}
}
}
]]>
</code-fragment>
<test-code>
<description>
case statement without break
</description>
<expected-problems>
1
</expected-problems>
<expected-linenumbers>
4
</expected-linenumbers>
<code-ref
id=
"switch-case-no-break"
/>
</test-code>
<code-fragment
id=
"switch-case-no-break"
>
<![CDATA[
public class Foo {
public void bar() {
int i;
switch (i) {
case 0:
int j =1;
// missing break,return and continue
case 1:
break;
case 2:
continue;
case 3: // OK
default:
int k = 1;
return;
}
}
}
]]>
</code-fragment>
<test-code>
<description>
case statement without break
</description>
<expected-problems>
1
</expected-problems>
<expected-linenumbers>
4
</expected-linenumbers>
<code-ref
id=
"switch-case-no-break"
/>
</test-code>
<!-- ====================================================================== -->
<code-fragment
id=
"default-no-break"
>
<![CDATA[
public class Foo {
public void bar() {
int i;
switch (i) {
case 0:
int j =1;
break;
case 1:
case 2:
continue;
default: // OK
}
}
}
]]>
</code-fragment>
<test-code>
<description>
default statement has no break
</description>
<expected-problems>
0
</expected-problems>
<code-ref
id=
"default-no-break"
/>
</test-code>
<code-fragment
id=
"default-no-break"
>
<![CDATA[
public class Foo {
public void bar() {
int i;
switch (i) {
case 0:
int j =1;
break;
case 1:
case 2:
continue;
default: // OK
}
}
}
]]>
</code-fragment>
<test-code>
<description>
default statement has no break
</description>
<expected-problems>
0
</expected-problems>
<code-ref
id=
"default-no-break"
/>
</test-code>
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<code-fragment
id=
"multi-case-no-statement"
>
<![CDATA[
public class Foo {
public void bar() {
int i;
switch (i) {
case 0:
case 1:
case 2:
// do something
break;
}
}
}
]]>
public class Foo {
public void bar() {
int i;
switch (i) {
case 0:
case 1:
case 2:
// do something
break;
}
}
}
]]>
</code-fragment>
<test-code>
<description>
multiple continuous blank case
</description>
<expected-problems>
1
</expected-problems>
<expected-linenumbers>
4
</expected-linenumbers>
<expected-linenumbers>
4
</expected-linenumbers>
<code-ref
id=
"multi-case-no-statement"
/>
</test-code>
</test-data>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录