pixee/codemodder-java

Add RemoveUnusedLocalVariables transform to SQLParameterizerWithCleanup

andrecsilva opened this issue · 0 comments

The sql parameterizer codemod may leave a few unused variables after the transformation. This transformation already exists in ASTTransforms.removeUnusedLocalVariables but it currently fails due to a bug in JavaParser where children may appear out of order after adding a statement.

This can be seen by executing the following snippet:

   String code =
        """
    class A{
	    void f(){
		    int b = 2;
		    int c = 3;
	    }
    }
    """;
    CompilationUnit cu = StaticJavaParser.parse(code);
    System.out.println(cu);
    // var name = cu.findAll(NameExpr.class).get(1);
    var stmt = cu.findAll(Statement.class).get(1);
    var newStmt = StaticJavaParser.parseStatement("int a = 1;");
    var blockStmt = (BlockStmt) stmt.getParentNode().get();
    System.out.println("Children: " + blockStmt.getChildNodes());
    System.out.println("Statements: " + blockStmt.getStatements());
    blockStmt.addStatement(0, newStmt);
    System.out.println(cu);
    System.out.println(stmt);
    System.out.println("Children: " + blockStmt.getChildNodes());
    System.out.println("Statements: " + blockStmt.getStatements());

this will produce:

    class A {

        void f() {
            int b = 2;
            int c = 3;
        }
    }

    Children: [int b = 2;, int c = 3;]
    Statements: [int b = 2;, int c = 3;]
    class A {

        void f() {
            int a = 1;
            int b = 2;
            int c = 3;
        }
    }

    int b = 2;
    Children: [int b = 2;, int c = 3;, int a = 1;]
    Statements: [int a = 1;, int b = 2;, int c = 3;]

Notice that the children and statement orders differ after the addition.