8000 Issue #15792: resolved false-negatives for CATCH · checkstyle/checkstyle@b24e789 · GitHub
[go: up one dir, main page]

Skip to content

Commit b24e789

Browse files
Anmol202005nrmancuso
authored andcommitted
Issue #15792: resolved false-negatives for CATCH
1 parent 3e5c4a8 commit b24e789

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

src/it/resources/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/InputTryCatchIfElse.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,34 @@ void foo() throws Exception {
3434
try (MyResource r = new MyResource()) {}
3535

3636
try (MyResource r = new MyResource()) {} catch (Exception expected) {}
37+
// 2 violations above:
38+
// 'WhitespaceAround: '{' is not followed by whitespace.'
39+
// ''}' at column 74 should be alone on a line.'
3740

3841
try (MyResource r = new MyResource()) {} catch (Exception expected) { }
39-
// violation above 'Empty blocks should have no spaces.'
42+
// 3 violations above:
43+
// 'Empty blocks should have no spaces.'
44+
// 'WhitespaceAround: '{' is not followed by whitespace.'
45+
// ''}' at column 75 should be alone on a line.'
4046

4147
try (MyResource r = new MyResource()) {
4248
} catch (Exception expected) {}
49+
// violation above ''}' at column 35 should be alone on a line.'
4350

4451
try (MyResource r = new MyResource()) {
45-
// violation below 'Empty blocks should have no spaces.'
4652
} catch (Exception expected) { }
53+
// 2 violations above:
54+
// 'Empty blocks should have no spaces.'
55+
// ''}' at column 36 should be alone on a line.'
4756

4857
try (MyResource r = new MyResource()) { ; }
4958
// violation above ''{' at column 43 should have line break after.'
5059

60+
try {
61+
/* foo */
62+
// violation below ''}' at column 35 should be alone on a line.'
63+
} catch (Exception expected) {}
64+
5165
try {
5266
/* foo */
5367
// violation below ''}' at column 5 should be on the same line as .* multi-block statement'

src/it/resources/com/google/checkstyle/test/chapter4formatting/rule413emptyblocks/InputEmptyBlocksAndCatchBlocks.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,29 @@ void foo() throws Exception {
5050
try (MyResource r = new MyResource()) { }
5151
// violation above 'Empty blocks should have no spaces. .* may only be represented as {}'
5252
try (MyResource r = new MyResource()) {}
53+
5354
try (MyResource r = new MyResource()) {} catch (Exception expected) {}
54-
// false-negative above until #15792
55+
// 2 violations above:
56+
// 'WhitespaceAround: '{' is not followed by whitespace.'
57+
// ''}' at column 74 should be alone on a line.'
58+
5559
try (MyResource r = new MyResource()) {} catch (Exception expected) { }
56-
// violation above 'Empty blocks should have no spaces. .* may only be represented as {}'
60+
// 3 violations above:
61+
// 'Empty blocks should have no spaces.'
62+
// 'WhitespaceAround: '{' is not followed by whitespace.'
63+
// ''}' at column 75 should be alone on a line.'
64+
5765
try (MyResource r = new MyResource()) {
5866

59-
} catch (Exception expected) {} // false-negative until #15792
67+
} catch (Exception expected) {}
68+
// violation above ''}' at column 35 should be alone on a line.'
6069
try (MyResource r = new MyResource()) {
6170

6271
} catch (Exception expected) { }
63-
// violation above 'Empty blocks should have no spaces. .* may only be represented as {}'
72+
// 2 violations above:
73+
// 'Empty blocks should have no spaces.'
74+
// ''}' at column 36 should be alone on a line.'
75+
6476
try (MyResource r = new MyResource()) {;}
6577
// 3 violations above:
6678
// 'WhitespaceAround: '{' is not followed by whitespace.'

src/main/resources/google_checks.xml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,32 @@
105105
<module name="RightCurly">
106106
<property name="id" value="RightCurlySame"/>
107107
<property name="tokens"
108-
value="LITERAL_TRY, LITERAL_CATCH, LITERAL_IF, LITERAL_ELSE,
108+
value="LITERAL_TRY, LITERAL_CATCH, LITERAL_IF, LITERAL_ELSE,
109109
LITERAL_DO"/>
110110
</module>
111+
<module name="SuppressionXpathSingleFilter">
112+
<property name="id" value="RightCurlySame"/>
113+
<property name="query" value="//RCURLY[parent::SLIST[parent::LITERAL_CATCH
114+
and not(parent::LITERAL_CATCH/following-sibling::*)]]"/>
115+
</module>
111116
<module name="RightCurly">
112117
<property name="id" value="RightCurlyAlone"/>
113118
<property name="option" value="alone"/>
114119
<property name="tokens"
115120
value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, STATIC_INIT,
116121
INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF, INTERFACE_DEF, RECORD_DEF,
117-
COMPACT_CTOR_DEF, LITERAL_SWITCH, LITERAL_CASE, LITERAL_FINALLY"/>
122+
COMPACT_CTOR_DEF, LITERAL_SWITCH, LITERAL_CASE, LITERAL_FINALLY,
123+
LITERAL_CATCH"/>
118124
</module>
119125
<module name="SuppressionXpathSingleFilter">
120126
<!-- suppression is required till https://github.com/checkstyle/checkstyle/issues/7541 -->
121127
<property name="id" value="RightCurlyAlone"/>
122-
<property name="query" value="//RCURLY[parent::SLIST[count(./*)=1]
123-
or preceding-sibling::*[last()][self::LCURLY]]"/>
128+
<property name="query" value="//RCURLY[parent::SLIST[count(./*)=1
129+
and not(parent::LITERAL_CATCH)]
130+
or (preceding-sibling::*[last()][self::LCURLY]
131+
and not(parent::SLIST/parent::LITERAL_CATCH))
132+
or (parent::SLIST/parent::LITERAL_CATCH
133+
and parent::SLIST/parent::LITERAL_CATCH/following-sibling::*)]"/>
124134
</module>
125135
<module name="WhitespaceAfter">
126136
<property name="tokens"
@@ -154,10 +164,14 @@
154164
</module>
155165
<module name="SuppressionXpathSingleFilter">
156166
<property name="checks" value="WhitespaceAround"/>
157-
<property name="query" value="//*[self::LITERAL_IF or self::LITERAL_ELSE or self::STATIC_INIT
158-
or self::LITERAL_TRY or self::LITERAL_CATCH]/SLIST[count(./*)=1]
167+
<property name="query" value="//*[self::LITERAL_IF or self::LITERAL_ELSE or
168+
self::STATIC_INIT]/SLIST[count(./*)=1]
159169
| //*[self::STATIC_INIT or self::LITERAL_TRY or self::LITERAL_IF]
160-
//*[self::RCURLY][parent::SLIST[count(./*)=1]]"/>
170+
//*[self::RCURLY][parent::SLIST[count(./*)=1]]
171+
| //SLIST[count(./*)=1][parent::LITERAL_TRY and
172+
not(following-sibling::*)]
173+
| //SLIST[count(./*)=1][parent::LITERAL_CATCH and
174+
not(parent::LITERAL_CATCH/following-sibling::*)]"/>
161175
</module>
162176
<module name="RegexpSinglelineJava">
163177
<property name="format" value="\{[ ]+\}"/>

0 commit comments

Comments
 (0)
0