View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_ASSIGN;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_EXPR;
25  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_IDENT;
26  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_LAMBDA;
27  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_LITERAL;
28  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_RETURN;
29  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_STRING;
30  
31  import org.junit.jupiter.api.Test;
32  
33  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
35  
36  /**
37   * Test fixture for the {@link UnnecessaryParenthesesCheck}.
38   *
39   */
40  public class UnnecessaryParenthesesCheckTest extends AbstractModuleTestSupport {
41  
42      @Override
43      public String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/checks/coding/unnecessaryparentheses";
45      }
46  
47      @Test
48      public void testDefault() throws Exception {
49  
50          final String[] expected = {
51              "18:22: " + getCheckMessage(MSG_ASSIGN),
52              "18:29: " + getCheckMessage(MSG_EXPR),
53              "18:31: " + getCheckMessage(MSG_IDENT, "i"),
54              "18:46: " + getCheckMessage(MSG_ASSIGN),
55              "19:15: " + getCheckMessage(MSG_ASSIGN),
56              "20:14: " + getCheckMessage(MSG_IDENT, "x"),
57              "20:17: " + getCheckMessage(MSG_ASSIGN),
58              "21:15: " + getCheckMessage(MSG_ASSIGN),
59              "22:14: " + getCheckMessage(MSG_IDENT, "x"),
60              "22:17: " + getCheckMessage(MSG_ASSIGN),
61              "25:22: " + getCheckMessage(MSG_ASSIGN),
62              "25:30: " + getCheckMessage(MSG_IDENT, "i"),
63              "25:46: " + getCheckMessage(MSG_ASSIGN),
64              "29:17: " + getCheckMessage(MSG_LITERAL, "0"),
65              "39:11: " + getCheckMessage(MSG_ASSIGN),
66              "43:11: " + getCheckMessage(MSG_ASSIGN),
67              "45:11: " + getCheckMessage(MSG_ASSIGN),
68              "47:11: " + getCheckMessage(MSG_ASSIGN),
69              "48:16: " + getCheckMessage(MSG_IDENT, "a"),
70              "49:14: " + getCheckMessage(MSG_IDENT, "a"),
71              "49:20: " + getCheckMessage(MSG_IDENT, "b"),
72              "49:26: " + getCheckMessage(MSG_LITERAL, "600"),
73              "49:40: " + getCheckMessage(MSG_LITERAL, "12.5f"),
74              "49:56: " + getCheckMessage(MSG_IDENT, "arg2"),
75              "50:14: " + getCheckMessage(MSG_STRING, "\"this\""),
76              "50:25: " + getCheckMessage(MSG_STRING, "\"that\""),
77              "51:11: " + getCheckMessage(MSG_ASSIGN),
78              "51:14: " + getCheckMessage(MSG_STRING, "\"this is a really, really...\""),
79              "53:16: " + getCheckMessage(MSG_RETURN),
80              "57:21: " + getCheckMessage(MSG_LITERAL, "1"),
81              "57:26: " + getCheckMessage(MSG_LITERAL, "13.5"),
82              "58:22: " + getCheckMessage(MSG_LITERAL, "true"),
83              "59:17: " + getCheckMessage(MSG_IDENT, "b"),
84              "63:17: " + getCheckMessage(MSG_ASSIGN),
85              "65:11: " + getCheckMessage(MSG_ASSIGN),
86              "67:16: " + getCheckMessage(MSG_RETURN),
87              "77:13: " + getCheckMessage(MSG_EXPR),
88              "81:16: " + getCheckMessage(MSG_EXPR),
89              "86:19: " + getCheckMessage(MSG_EXPR),
90              "87:23: " + getCheckMessage(MSG_LITERAL, "4000"),
91              "92:19: " + getCheckMessage(MSG_ASSIGN),
92              "94:11: " + getCheckMessage(MSG_ASSIGN),
93              "94:16: " + getCheckMessage(MSG_LITERAL, "3"),
94              "95:26: " + getCheckMessage(MSG_ASSIGN),
95              "106:11: " + getCheckMessage(MSG_ASSIGN),
96              "106:14: " + getCheckMessage(MSG_STRING, "\"12345678901234567890123\""),
97          };
98  
99          verifyWithInlineConfigParser(
100                 getPath("InputUnnecessaryParenthesesOperatorsAndCasts.java"), expected);
101     }
102 
103     @Test
104     public void test15Extensions() throws Exception {
105         final String[] expected = {
106             "28:23: " + getCheckMessage(MSG_EXPR),
107             "28:51: " + getCheckMessage(MSG_LITERAL, "1"),
108             "59:20: " + getCheckMessage(MSG_ASSIGN),
109         };
110         verifyWithInlineConfigParser(
111                 getPath("InputUnnecessaryParentheses15Extensions.java"), expected);
112     }
113 
114     @Test
115     public void testLambdas() throws Exception {
116         final String[] expected = {
117             "17:35: " + getCheckMessage(MSG_LAMBDA),
118             "18:35: " + getCheckMessage(MSG_LAMBDA),
119             "25:18: " + getCheckMessage(MSG_LAMBDA),
120             "28:25: " + getCheckMessage(MSG_LAMBDA),
121             "47:25: " + getCheckMessage(MSG_LAMBDA),
122             "47:33: " + getCheckMessage(MSG_LAMBDA),
123             "50:25: " + getCheckMessage(MSG_LAMBDA),
124             "53:31: " + getCheckMessage(MSG_LAMBDA),
125         };
126         verifyWithInlineConfigParser(
127                 getPath("InputUnnecessaryParenthesesLambdas.java"), expected);
128     }
129 
130     @Test
131     public void testReturn() throws Exception {
132         final String[] expected = {
133             "21:33: " + getCheckMessage(MSG_RETURN),
134             "22:16: " + getCheckMessage(MSG_RETURN),
135             "25:16: " + getCheckMessage(MSG_RETURN),
136             "28:16: " + getCheckMessage(MSG_RETURN),
137             "31:16: " + getCheckMessage(MSG_RETURN),
138             "36:16: " + getCheckMessage(MSG_RETURN),
139         };
140         verifyWithInlineConfigParser(
141                 getPath("InputUnnecessaryParenthesesReturnValue.java"), expected);
142     }
143 
144     @Test
145     public void testUnnecessaryParenthesesSwitchExpression() throws Exception {
146         final String[] expected = {
147             "21:31: " + getCheckMessage(MSG_ASSIGN),
148             "24:13: " + getCheckMessage(MSG_LITERAL, 2),
149             "25:39: " + getCheckMessage(MSG_ASSIGN),
150             "30:18: " + getCheckMessage(MSG_RETURN),
151             "32:16: " + getCheckMessage(MSG_IDENT, "g"),
152             "36:18: " + getCheckMessage(MSG_RETURN),
153             "46:31: " + getCheckMessage(MSG_ASSIGN),
154             "48:13: " + getCheckMessage(MSG_LITERAL, 2),
155             "49:39: " + getCheckMessage(MSG_ASSIGN),
156             "53:18: " + getCheckMessage(MSG_RETURN),
157             "58:18: " + getCheckMessage(MSG_RETURN),
158         };
159         verifyWithInlineConfigParser(
160                 getPath(
161                         "InputUnnecessaryParenthesesCheckSwitchExpression.java"),
162                 expected);
163     }
164 
165     @Test
166     public void testUnnecessaryParenthesesTextBlocks() throws Exception {
167         final String[] expected = {
168             "19:23: " + getCheckMessage(MSG_STRING, "\"this\""),
169             "19:34: " + getCheckMessage(MSG_STRING, "\"that\""),
170             "19:45: " + getCheckMessage(MSG_STRING, "\"other\""),
171             "20:23: " + getCheckMessage(MSG_STRING, "\"\\n     "
172                 + "   this\""),
173             "22:12: " + getCheckMessage(MSG_STRING, "\"\\n     "
174                 + "   that\""),
175             "24:12: " + getCheckMessage(MSG_STRING, "\"\\n     "
176                 + "   other\""),
177             "27:23: " + getCheckMessage(MSG_STRING, "\"\\n        this is a test...\""),
178             "28:32: " + getCheckMessage(MSG_STRING, "\"\\n        and another li...\""),
179         };
180         verifyWithInlineConfigParser(
181                 getPath(
182                 "InputUnnecessaryParenthesesCheckTextBlocks.java"),
183             expected);
184     }
185 
186     @Test
187     public void testUnnecessaryParenthesesPatterns() throws Exception {
188         final String[] expected = {
189             "24:22: " + getCheckMessage(MSG_ASSIGN),
190             "27:21: " + getCheckMessage(MSG_ASSIGN),
191             "31:13: " + getCheckMessage(MSG_EXPR),
192         };
193         verifyWithInlineConfigParser(
194                 getNonCompilablePath(
195                 "InputUnnecessaryParenthesesCheckPatterns.java"),
196             expected);
197     }
198 
199     @Test
200     public void testTokensNotNull() {
201         final UnnecessaryParenthesesCheck check = new UnnecessaryParenthesesCheck();
202         assertWithMessage("Acceptable tokens should not be null")
203             .that(check.getAcceptableTokens())
204             .isNotNull();
205         assertWithMessage("Default tokens should not be null")
206             .that(check.getDefaultTokens())
207             .isNotNull();
208         assertWithMessage("Required tokens should not be null")
209             .that(check.getRequiredTokens())
210             .isNotNull();
211     }
212 
213     @Test
214     public void testIfStatement() throws Exception {
215 
216         final String[] expected = {
217             "20:20: " + getCheckMessage(MSG_EXPR),
218             "34:13: " + getCheckMessage(MSG_EXPR),
219             "35:20: " + getCheckMessage(MSG_EXPR),
220             "39:13: " + getCheckMessage(MSG_EXPR),
221             "39:14: " + getCheckMessage(MSG_EXPR),
222             "40:20: " + getCheckMessage(MSG_EXPR),
223             "45:20: " + getCheckMessage(MSG_EXPR),
224             "49:13: " + getCheckMessage(MSG_EXPR),
225             "50:20: " + getCheckMessage(MSG_EXPR),
226             "54:13: " + getCheckMessage(MSG_EXPR),
227             "55:17: " + getCheckMessage(MSG_EXPR),
228             "56:28: " + getCheckMessage(MSG_EXPR),
229             "61:13: " + getCheckMessage(MSG_EXPR),
230             "66:14: " + getCheckMessage(MSG_EXPR),
231             "67:24: " + getCheckMessage(MSG_EXPR),
232             "70:13: " + getCheckMessage(MSG_EXPR),
233             "71:21: " + getCheckMessage(MSG_EXPR),
234             "72:21: " + getCheckMessage(MSG_EXPR),
235             "78:12: " + getCheckMessage(MSG_EXPR),
236             "79:20: " + getCheckMessage(MSG_EXPR),
237             "86:20: " + getCheckMessage(MSG_EXPR),
238             "103:13: " + getCheckMessage(MSG_EXPR),
239             "106:13: " + getCheckMessage(MSG_EXPR),
240             "107:21: " + getCheckMessage(MSG_EXPR),
241             "110:13: " + getCheckMessage(MSG_EXPR),
242         };
243 
244         verifyWithInlineConfigParser(
245                 getPath("InputUnnecessaryParenthesesIfStatement.java"), expected);
246     }
247 
248     @Test
249     public void testIfStatement2() throws Exception {
250         final String[] expected = {
251             "28:17: " + getCheckMessage(MSG_EXPR),
252             "39:17: " + getCheckMessage(MSG_EXPR),
253             "51:25: " + getCheckMessage(MSG_EXPR),
254             "57:13: " + getCheckMessage(MSG_EXPR),
255             "59:28: " + getCheckMessage(MSG_EXPR),
256             "60:28: " + getCheckMessage(MSG_EXPR),
257             "61:20: " + getCheckMessage(MSG_EXPR),
258             "63:20: " + getCheckMessage(MSG_EXPR),
259             "74:20: " + getCheckMessage(MSG_EXPR),
260         };
261         verifyWithInlineConfigParser(
262                 getPath("InputUnnecessaryParenthesesIfStatement2.java"), expected);
263     }
264 
265     @Test
266     public void testIdentifier() throws Exception {
267         final String[] expected = {
268             "22:17: " + getCheckMessage(MSG_IDENT, "test"),
269             "31:18: " + getCheckMessage(MSG_ASSIGN),
270             "32:13: " + getCheckMessage(MSG_IDENT, "square"),
271             "46:22: " + getCheckMessage(MSG_IDENT, "clazz"),
272             "56:18: " + getCheckMessage(MSG_IDENT, "test"),
273             "57:22: " + getCheckMessage(MSG_IDENT, "clazz"),
274             "75:18: " + getCheckMessage(MSG_EXPR),
275             "76:17: " + getCheckMessage(MSG_EXPR),
276             "77:25: " + getCheckMessage(MSG_EXPR),
277             "82:48: " + getCheckMessage(MSG_IDENT, "get"),
278             "98:33: " + getCheckMessage(MSG_EXPR),
279             "101:34: " + getCheckMessage(MSG_IDENT, "isComment"),
280 
281         };
282         verifyWithInlineConfigParser(
283                 getPath("InputUnnecessaryParenthesesIdentifier.java"), expected);
284     }
285 
286     @Test
287     public void testOperator1() throws Exception {
288         final String[] expected = {
289             "20:17: " + getCheckMessage(MSG_EXPR),
290             "22:17: " + getCheckMessage(MSG_EXPR),
291             "24:17: " + getCheckMessage(MSG_EXPR),
292             "26:17: " + getCheckMessage(MSG_EXPR),
293             "28:17: " + getCheckMessage(MSG_EXPR),
294             "30:17: " + getCheckMessage(MSG_EXPR),
295             "32:17: " + getCheckMessage(MSG_EXPR),
296             "34:17: " + getCheckMessage(MSG_EXPR),
297             "36:17: " + getCheckMessage(MSG_EXPR),
298             "38:17: " + getCheckMessage(MSG_EXPR),
299             "40:17: " + getCheckMessage(MSG_EXPR),
300             "42:17: " + getCheckMessage(MSG_EXPR),
301             "47:19: " + getCheckMessage(MSG_EXPR),
302             "49:18: " + getCheckMessage(MSG_EXPR),
303             "51:18: " + getCheckMessage(MSG_EXPR),
304             "53:17: " + getCheckMessage(MSG_EXPR),
305             "55:18: " + getCheckMessage(MSG_EXPR),
306             "57:19: " + getCheckMessage(MSG_EXPR),
307             "59:18: " + getCheckMessage(MSG_EXPR),
308             "61:19: " + getCheckMessage(MSG_EXPR),
309             "63:18: " + getCheckMessage(MSG_EXPR),
310             "65:18: " + getCheckMessage(MSG_EXPR),
311             "67:19: " + getCheckMessage(MSG_EXPR),
312             "69:18: " + getCheckMessage(MSG_EXPR),
313             "85:20: " + getCheckMessage(MSG_EXPR),
314             "102:14: " + getCheckMessage(MSG_EXPR),
315             "106:13: " + getCheckMessage(MSG_EXPR),
316         };
317         verifyWithInlineConfigParser(
318                 getPath("InputUnnecessaryParenthesesOperator.java"), expected);
319     }
320 
321     @Test
322     public void testOperator2() throws Exception {
323         final String[] expected = {
324             "66:18: " + getCheckMessage(MSG_EXPR),
325             "67:17: " + getCheckMessage(MSG_EXPR),
326             "68:25: " + getCheckMessage(MSG_EXPR),
327             "82:14: " + getCheckMessage(MSG_EXPR),
328             "83:19: " + getCheckMessage(MSG_EXPR),
329             "92:21: " + getCheckMessage(MSG_EXPR),
330             "95:19: " + getCheckMessage(MSG_EXPR),
331             "98:20: " + getCheckMessage(MSG_EXPR),
332             "101:21: " + getCheckMessage(MSG_EXPR),
333             "107:20: " + getCheckMessage(MSG_EXPR),
334             "110:21: " + getCheckMessage(MSG_EXPR),
335         };
336         verifyWithInlineConfigParser(
337                 getPath("InputUnnecessaryParenthesesOperator2.java"), expected);
338     }
339 
340     @Test
341     public void testOperator3() throws Exception {
342         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
343         verifyWithInlineConfigParser(
344                 getPath("InputUnnecessaryParenthesesOperator3.java"), expected);
345     }
346 
347     @Test
348     public void testWhenExpressions() throws Exception {
349         final String[] expected = {
350             "22:33: " + getCheckMessage(MSG_EXPR),
351             "24:32: " + getCheckMessage(MSG_EXPR),
352             "28:26: " + getCheckMessage(MSG_EXPR),
353             "31:31: " + getCheckMessage(MSG_EXPR),
354             "31:32: " + getCheckMessage(MSG_EXPR),
355             "37:44: " + getCheckMessage(MSG_EXPR),
356             "40:44: " + getCheckMessage(MSG_EXPR),
357         };
358         verifyWithInlineConfigParser(
359                 getPath("InputUnnecessaryParenthesesWhenExpressions.java"), expected);
360     }
361 
362     @Test
363     public void testUnnecessaryParenthesesConditionalExpression() throws Exception {
364         final String[] expected = {
365             "19:17: " + getCheckMessage(MSG_EXPR),
366             "19:29: " + getCheckMessage(MSG_LITERAL, "3"),
367             "19:35: " + getCheckMessage(MSG_LITERAL, "4"),
368             "25:18: " + getCheckMessage(MSG_EXPR),
369             "28:18: " + getCheckMessage(MSG_EXPR),
370             "28:33: " + getCheckMessage(MSG_EXPR),
371             "35:26: " + getCheckMessage(MSG_EXPR),
372             "36:17: " + getCheckMessage(MSG_EXPR),
373             "36:41: " + getCheckMessage(MSG_EXPR),
374         };
375         verifyWithInlineConfigParser(
376                 getPath("InputUnnecessaryParenthesesConditionalExpression.java"), expected);
377 
378     }
379 
380     @Test
381     public void testFieldAndMethodAccess() throws Exception {
382         final String[] expected = {
383             "33:15: " + getCheckMessage(MSG_EXPR),
384             "36:15: " + getCheckMessage(MSG_EXPR),
385             "39:15: " + getCheckMessage(MSG_EXPR),
386             "42:15: " + getCheckMessage(MSG_EXPR),
387             "44:40: " + getCheckMessage(MSG_EXPR),
388             "57:13: " + getCheckMessage(MSG_EXPR),
389             "60:13: " + getCheckMessage(MSG_EXPR),
390             "64:13: " + getCheckMessage(MSG_EXPR),
391             "67:13: " + getCheckMessage(MSG_EXPR),
392             "76:14: " + getCheckMessage(MSG_EXPR),
393             "80:31: " + getCheckMessage(MSG_EXPR),
394             "84:27: " + getCheckMessage(MSG_EXPR),
395             "88:41: " + getCheckMessage(MSG_EXPR),
396         };
397         verifyWithInlineConfigParser(
398             getPath("InputUnnecessaryParenthesesFieldMethodAccess.java"), expected);
399     }
400 
401     @Test
402     public void testConstructor() throws Exception {
403         final String[] expected = new String[0];
404         verifyWithInlineConfigParser(
405             getPath("InputUnnecessaryParenthesesConstructor.java"), expected);
406     }
407 
408     @Test
409     public void testUnnecessaryParenthesesCasts1() throws Exception {
410         final String[] expected = {
411             "22:17: " + getCheckMessage(MSG_EXPR),
412             "22:42: " + getCheckMessage(MSG_EXPR),
413             "30:11: " + getCheckMessage(MSG_ASSIGN),
414             "35:11: " + getCheckMessage(MSG_ASSIGN),
415             "35:21: " + getCheckMessage(MSG_EXPR),
416             "48:18: " + getCheckMessage(MSG_EXPR),
417             "65:14: " + getCheckMessage(MSG_EXPR),
418             "74:30: " + getCheckMessage(MSG_EXPR),
419             "83:39: " + getCheckMessage(MSG_EXPR),
420             "92:19: " + getCheckMessage(MSG_EXPR),
421             "96:21: " + getCheckMessage(MSG_EXPR),
422             "100:29: " + getCheckMessage(MSG_EXPR),
423             "104:37: " + getCheckMessage(MSG_EXPR),
424             "108:51: " + getCheckMessage(MSG_EXPR),
425             "113:17: " + getCheckMessage(MSG_EXPR),
426         };
427 
428         verifyWithInlineConfigParser(
429                 getPath("InputUnnecessaryParenthesesCasts1.java"), expected);
430     }
431 
432     @Test
433     public void testUnnecessaryParenthesesCasts2() throws Exception {
434         final String[] expected = {
435             "32:27: " + getCheckMessage(MSG_EXPR),
436             "44:14: " + getCheckMessage(MSG_EXPR),
437             "55:31: " + getCheckMessage(MSG_EXPR),
438             "62:30: " + getCheckMessage(MSG_EXPR),
439             "72:13: " + getCheckMessage(MSG_EXPR),
440             "85:22: " + getCheckMessage(MSG_EXPR),
441             "90:29: " + getCheckMessage(MSG_EXPR),
442             "90:44: " + getCheckMessage(MSG_EXPR),
443             "93:52: " + getCheckMessage(MSG_EXPR),
444             "96:22: " + getCheckMessage(MSG_EXPR),
445             "99:27: " + getCheckMessage(MSG_EXPR),
446             "104:37: " + getCheckMessage(MSG_EXPR),
447         };
448 
449         verifyWithInlineConfigParser(
450                 getPath("InputUnnecessaryParenthesesCasts2.java"), expected);
451     }
452 
453     @Test
454     public void testUnnecessaryParenthesesCasts3() throws Exception {
455         final String[] expected = {
456             "31:48: " + getCheckMessage(MSG_EXPR),
457             "42:17: " + getCheckMessage(MSG_EXPR),
458             "47:33: " + getCheckMessage(MSG_EXPR),
459             "51:25: " + getCheckMessage(MSG_EXPR),
460             "56:49: " + getCheckMessage(MSG_EXPR),
461             "62:28: " + getCheckMessage(MSG_EXPR),
462             "67:13: " + getCheckMessage(MSG_ASSIGN),
463         };
464 
465         verifyWithInlineConfigParser(
466                 getPath("InputUnnecessaryParenthesesCasts3.java"), expected);
467     }
468 
469 }