Skip to content

Commit

Permalink
Fix: issue 4579 Switch expr and var incompatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jlerbsc committed Oct 10, 2024
1 parent 479ec4d commit 602f8ed
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/

package com.github.javaparser.ast.validator;

import static com.github.javaparser.ParseStart.STATEMENT;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.utils.TestUtils.assertNoProblems;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.ast.stmt.Statement;
import org.junit.jupiter.api.Test;

class VarValidatorTest {
public static final JavaParser javaParser =
new JavaParser(new ParserConfiguration().setLanguageLevel(LanguageLevel.BLEEDING_EDGE));

@Test
void typePatternExpr() {
ParseResult<Statement> result = javaParser.parse(
STATEMENT, provider("switch (shape) {\n" + " case Circle(var radius) -> { } \n" + "}"));

assertNoProblems(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.ArrayInitializerExpr;
import com.github.javaparser.ast.expr.LambdaExpr;
import com.github.javaparser.ast.expr.NullLiteralExpr;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.ForEachStmt;
import com.github.javaparser.ast.stmt.ForStmt;
Expand All @@ -46,6 +43,16 @@ public VarValidator(boolean varAllowedInLambdaParameters) {

@Override
public void accept(VarType node, ProblemReporter reporter) {
// Allowed in type pattern expression. For example
// record Name(String fName, String lName, String mName) {};
// Name host = new Name("William", "Michael", "Korando");
// String printName = switch(person){
// case Name(var fName, var lName, var mName) -> lName + ", " + fName + " " + mName;
// };
if (node.hasParentNode() && node.getParentNode().get() instanceof TypePatternExpr) {
return;
}

// All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something
// else.
Optional<VariableDeclarator> variableDeclarator = node.findAncestor(VariableDeclarator.class);
Expand Down

0 comments on commit 602f8ed

Please sign in to comment.