File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed
main/java/org/kohsuke/groovy/sandbox
test/groovy/org/kohsuke/groovy/sandbox Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change 2727import org .codehaus .groovy .ast .expr .ClosureExpression ;
2828import org .codehaus .groovy .ast .expr .ConstantExpression ;
2929import org .codehaus .groovy .ast .expr .DeclarationExpression ;
30+ import org .codehaus .groovy .ast .expr .EmptyExpression ;
3031import org .codehaus .groovy .ast .expr .Expression ;
3132import org .codehaus .groovy .ast .expr .ListExpression ;
3233import org .codehaus .groovy .ast .expr .MethodCallExpression ;
@@ -716,7 +717,9 @@ public void visitExpressionStatement(ExpressionStatement es) {
716717 DeclarationExpression de = (DeclarationExpression ) exp ;
717718 Expression leftExpression = de .getLeftExpression ();
718719 if (leftExpression instanceof VariableExpression ) {
719- if (mightBePositionalArgumentConstructor ((VariableExpression ) leftExpression )) {
720+ // Only cast and transform if the RHS is *not* an EmptyExpression, i.e., "String foo;" would not be cast/transformed.
721+ if (!(de .getRightExpression () instanceof EmptyExpression ) &&
722+ mightBePositionalArgumentConstructor ((VariableExpression ) leftExpression )) {
720723 CastExpression ce = new CastExpression (leftExpression .getType (), de .getRightExpression ());
721724 ce .setCoerce (true );
722725 es .setExpression (transform (new DeclarationExpression (leftExpression , de .getOperation (), ce )));
Original file line number Diff line number Diff line change @@ -840,4 +840,15 @@ return cnt''')
840840 '''
841841 )
842842 }
843+
844+ @Issue (" JENKINS-46191" )
845+ void testEmptyDeclaration () {
846+ assertIntercept([" " ],
847+ " abc" ,
848+ '''
849+ String a
850+ a = "abc"
851+ return a
852+ ''' )
853+ }
843854}
You can’t perform that action at this time.
0 commit comments