Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Benoit Lacelle - SOLVEN
* Copyright 2023-2025 Benoit Lacelle - SOLVEN
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,19 +17,20 @@

import java.util.Set;

import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.LambdaExpr;
import com.google.common.collect.ImmutableSet;

import eu.solven.cleanthat.engine.java.IJdkVersionConstants;
import eu.solven.cleanthat.engine.java.refactorer.AJavaparserNodeMutator;
import eu.solven.cleanthat.engine.java.refactorer.AJavaparserExprMutator;
import eu.solven.cleanthat.engine.java.refactorer.NodeAndSymbolSolver;

/**
* Turns '.stream((s) -> s.subString(0, 2))' into '.stream(s -> s.subString(0, 2))'
*
* @author Benoit Lacelle
*/
public class UnnecessaryLambdaEnclosingParameters extends AJavaparserNodeMutator {
public class UnnecessaryLambdaEnclosingParameters extends AJavaparserExprMutator {

@Override
public String minimalJavaVersion() {
Expand All @@ -48,24 +49,18 @@ public boolean isDraft() {
}

@Override
protected boolean processNotRecursively(NodeAndSymbolSolver<?> node) {
protected boolean processExpression(NodeAndSymbolSolver<Expression> node) {
if (!(node.getNode() instanceof LambdaExpr)) {
return false;
}

var lambdaExpr = (LambdaExpr) node.getNode();
if (lambdaExpr.getParameters().size() != 1 || !lambdaExpr.isEnclosingParameters()) {
return false;
}

var param = lambdaExpr.getParameters().get(0);
if (param.getType() != null) {
return false;
}

var newLambdaExpr = lambdaExpr.clone();
newLambdaExpr.setEnclosingParameters(false);
return tryReplace(lambdaExpr, newLambdaExpr);
if (lambdaExpr.getParameters().size() != 1 || lambdaExpr.isExplicitlyTyped()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you craft a unit-test catching what's brought by this PR?

Copy link
Copy Markdown
Contributor Author

@gbq6 gbq6 Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually any test with @CompareMethods would have revealed this issue, but as the the JavaParser seems to ignore setEnclosingParameters(false) these tests are ignored. I'll add a comment regarding that if you don't mind though.

Edit: 24f869e

return false;
}

}
var newLambdaExpr = lambdaExpr.clone();
newLambdaExpr.setEnclosingParameters(false);
return tryReplace(lambdaExpr, newLambdaExpr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Runnable pre() {
}
}

@Ignore("TODO")
@Ignore("JavaParser seems to ignore `setEnclosingParameters(false)`")
@CompareMethods
public static class CaseFunction {
public Function<Integer, Integer> pre() {
Expand Down