-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpf2_misconceptions.xml
More file actions
567 lines (503 loc) · 13.8 KB
/
pf2_misconceptions.xml
File metadata and controls
567 lines (503 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?xml version="1.0"?>
<ruleset name="PF2 misconceptions"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Common misconceptions found in the pf2 course, or in general in novice programmers.
</description>
<rule name="NoFloatLiterals"
language="java"
message="Use float literal instead of casting a literal to float"
class="net.sourceforge.pmd.lang.rule.XPathRule"
>
<description>
A float literal can be obtained by appending 'f' to the number. i.e. 1.2f
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//CastExpression[
./Type/PrimitiveType[@Image='float']
and
./PrimaryExpression/PrimaryPrefix/Literal[@FloatLiteral = false()]
]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
public void bar() {
float f = (float) 123;
f = (float) 23;
f = (float) (1.5);
}
}
]]>
</example>
</rule>
<rule name="NoLongLiterals"
language="java"
message="Use long literal instead of casting a literal to long"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
A long literal can be obtained by appending 'L' to the number. i.e. 10L
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//CastExpression[
./Type/PrimitiveType[@Image='long']
and
./PrimaryExpression/PrimaryPrefix/Literal[@IntLiteral = true()]
]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
public void bar() {
long l = (long) 123;
l = (long) 23;
l = (long) (12);
}
}
]]>
</example>
</rule>
<rule name="MapToBooleanWithConditionalOperator"
language="java"
message="The value from the ternary operator is redundant."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
The ternary operator is unnecessary use the result of a boolean expression instead use.
i.e.
boolean mybool = !a; //instead of mybool = !a ? true : false;
boolean mybool = b.bar(); //instead of mybool = b.bar() ? true : false;
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ConditionalExpression[
./Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
and
./PrimaryExpression[2]/PrimaryPrefix/Literal/BooleanLiteral
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
public void bar() {
boolean t1 = a ? true : false;
boolean t2 = a ? false : true;
boolean t3 = foo() ? false : true;
if(foo() ? false : true){
//do something
}
}
}
]]>
</example>
</rule>
<rule name="ComparisonWithBooleanLiteral"
language="java"
message="Comparisons with boolean literals are unnecessary."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
A comparison with a boolean literal such as 'bool == true' serves no
purpose (true == true is true, false == true is false), directly use the value of the variable instead.
i.e.
if(bool){
//do something
}
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Expression[
./EqualityExpression//Literal/BooleanLiteral
or
./ConditionalOrExpression//Literal/BooleanLiteral
or
./ConditionalAndExpression//Literal/BooleanLiteral
or
./ExclusiveOrExpression//Literal/BooleanLiteral
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
public void bar() {
boolean bool = true;
//unnecessary comparison with true
if(bool == true){
//do something
}
//unnecessary comparison with false
if(bool != false){
//do something
}
}
]]>
</example>
</rule>
<rule name="MapToBooleanWithIf"
language="java"
message="Directly utilise the condition to obtain the boolean value needed."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
For a return an if statement is not necessary if we already have a
boolean expression on which we decide what to return.
i.e.
public boolean bar(boolean bool){
return !bool || foo();
}
NOT:
public boolean bar(boolean bool){
if(!bool || foo())
return true;
else
return false;
}
there is also the case to select a value:
boolean b;
if (CONDITION) {
b = true;
} else {
b = false;
}
the same result could be obtained by using:
b = CONDITION;
</description>
<priority>4</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//IfStatement[count(./Statement) = 2][
./Statement[1][count(./Block/BlockStatement) = 1]/Block/BlockStatement/Statement/ReturnStatement[
.//AllocationExpression/Arguments[(@ArgumentCount = 1) = true()]/ArgumentList/Expression//BooleanLiteral
or
./Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
]
and
./Statement[2][count(./Block/BlockStatement) = 1]/Block/BlockStatement/Statement/ReturnStatement[
.//AllocationExpression/Arguments[(@ArgumentCount = 1) = true()]/ArgumentList/Expression//BooleanLiteral
or
./Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
]
]|
//IfStatement[count(./Statement) = 2][
./Statement[1]/Block[count(./BlockStatement) = 1]//StatementExpression[
./AssignmentOperator
and
./Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
]
and
./Statement[2]/Block[count(./BlockStatement) = 1]//StatementExpression[
./AssignmentOperator
and
./Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
]
]
]]>
</value>
</property>
<property name="version" value="1.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
public boolean bar() {
if(false)
return false;
if(false){
return true;
}
//...
}
public Foo foo(){
if(false)
return new Foo(false);
if(false){
return new Foo(true);
}
//...
}
public Foo foo2(boolean s){
boolean b;
if(!s){
b = false;
}
else{
b = true;
}
//...
}
}
]]>
</example>
</rule>
<rule name="StringLiteralNoObject"
language="java"
message="Directly utilise the String literal instead of passing it as an argument to a String constructor."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
A String can be constructed directly by assigning a String literal to a String reference just as if it were a primitive type (it isn't). i.e. String s = "Hello World";
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//AllocationExpression[
./ClassOrInterfaceType[@Image='String']
and
./Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral = true()]
]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
public boolean bar() {
String s = new String("Hello World");
s = new String("Hello Again");
new String("This too.");
}
}
]]>
</example>
</rule>
<rule name="CharNotNumeric"
language="java"
message="A char can be utilised as if it were a number."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
A char can be utilised as if it were a number, but to print a char the expression must be of type char.
i.e.
System.out.println( 'b' ); // b
System.out.println( 'a' + 1 ); // 98 (the expression type is int)
System.out.println( (char) ('a' + 1) ); // b (the expression type is char)
System.out.println( (char) ('b' - 1) ); // a (the expression type is char)
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//CastExpression[
./Type/PrimitiveType[@Image='int']
and
./PrimaryExpression/PrimaryPrefix//Literal[@CharLiteral = true()]
]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
public boolean bar() {
char c = (char) (( (int)'a' ) +2);
int i = (int) 'i';
}
}
]]>
</example>
</rule>
<!-- only with pmd:check, compilation fails otherwise-->
<rule name="LargeIntegerLong"
language="java"
message="A large integer literal bigger than 32bit is not a Long"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
A value bigger than the maximal value of int is not represented as long but overflows.
Use a literal of type long instead
i.e.
long l = 1234567876543245678L;
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix/Literal[@IntLiteral = true()][
@ValueAsDouble > 2147483647 or
@ValueAsDouble < -2147483648
]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
public boolean bar() {
long i = 1234567876543245678;
}
}
]]>
</example>
</rule>
<rule name="ReturnCall"
language="java"
message="The return statement does not need parenthesis as it is NOT a method."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
The return statement only requires an expression, no parenthesis are needed.
i.e.
return 3;
</description>
<priority>4</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ReturnStatement/Expression/PrimaryExpression/PrimaryPrefix/Expression
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
public boolean bar() {
return (true);
}
}
]]>
</example>
</rule>
<rule name="ThisExistsInStaticMethod"
language="java"
message=" this only exists in instance methods."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
"this" only exists in instance methods as it the refers to the object on which the
method is called.
</description>
<priority>2</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration[@Static = true()]//PrimaryPrefix[@ThisModifier = true()]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
int a;
public Foo void bar() {
this.a = 12;
return this;
}
}
]]>
</example>
</rule>
<rule name="DeferredReturn"
language="java"
message="A return statement immediately returns."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
When a return statement is reached it immediately return unless inside a try in a
try{}catch(){}finally{}.
</description>
<priority>2</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Block/BlockStatement[position() != last()]/Statement/ReturnStatement
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
int i = 0;
public int bar() {
return i;
i++;
}
}
]]>
</example>
</rule>
<rule name="ForEachVariableIsElement"
language="java"
message="The variable of a for-each statement is not an alias for the element, therefore a for-each cannot be used to initialise the elements of an array."
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
The variable of a for-each statement can be seen as a new variable to which it's
assigned the value currently in the collection, it's therefore a distinct variable
not an alias for the variable holding the element.
</description>
<priority>2</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ForStatement[
.//LocalVariableDeclaration//VariableDeclaratorId/@Image =
.//StatementExpression[ ./AssignmentOperator ]/PrimaryExpression[not(./PrimarySuffix)]//Name/@Image
and
not( .//LocalVariableDeclaration//VariableInitializer )
]
]]>
</value>
</property>
<property name="version" value="2.0"/>
</properties>
<example>
<![CDATA[
public class Foo {
int i = 0;
public int bar() {
Foo[] foos = new Foo[10];
for (Foo foo : foos) {
foo = new Foo(); //violation
}
int[] ages = new int[10];
for (int age : ages) {
age = 19; //violation
}
}
}
]]>
</example>
</rule>
</ruleset>