Affects PMD Version:
6.31.0
Rule:
UnusedAssignment
Property reportUnusedVariables=true
Description:
False positive when declared variables are initialized and referenced in multi-line code-blocks surrounded by curly braces.
Code Sample demonstrating the issue:
import java.io.IOException; import static org.objectweb.asm.Opcodes.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; public class Dumper { private static final String VERSION = "version"; private static final String CLASS = "Dumper"; public static byte[] dump() throws IOException { // ClassWriter is a class visitor that generates the code for the class ClassWriter cw = new ClassWriter(0); // Start creating the class. cw.visit(V11, ACC_PUBLIC + ACC_SUPER, CLASS, null, "java/lang/Object", null); FieldVisitor fv; // false positive MethodVisitor mv; // false positive { // version field fv = cw.visitField(ACC_PRIVATE, VERSION, "I", null, null); fv.visitEnd(); } { // Implementing the constructor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); } }
Second example:
int start; if (isOdd(firstPartitionSize)) start = firstPartitionSize; else start = firstPartitionSize + 1;
Third example
private boolean isPair(int number) { // Unlike SoftWorker's isPair(int) method, // this one contains a little bit more code not necessarily // related to pair number detection. It's here to detect if // a long method can be inlined String stringNumber = String.valueOf(number); int divRest; if (stringNumber.length() == 3) divRest = number % 10; else if (stringNumber.length() == 4) divRest = number % 100; else if (stringNumber.length() == 5) divRest = number % 1000; else if (stringNumber.length() == 6) divRest = number % 1000; else divRest = number % 10_000; if (divRest == 0) return true; return number % 2 == 0; }
Expected outcome:
No violation reported.
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4