Skip to content

Commit

Permalink
Merge pull request solven-eu#849 from solven-eu/LambdaMethodRef/chain…
Browse files Browse the repository at this point in the history
…edMethods

methodRef is accepted only if the scope is a name or field
  • Loading branch information
blacelle authored Oct 9, 2024
2 parents 76ced1f + 88ab88f commit f064f10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Benoit Lacelle - SOLVEN
* Copyright 2023-2024 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 Down Expand Up @@ -223,6 +223,13 @@ private boolean onMethodCall(NodeAndSymbolSolver<?> context,
}
var scope = optScope.get();

if (!scope.isNameExpr() && !scope.isFieldAccessExpr()) {
// https://github.com/solven-eu/cleanthat/issues/847
// There is a risk of side-effect if the scope is not the lambda parameter (e.g. if the lambda chains
// multiple calls)
return false;
}

if (methodCallExpr.getArguments().size() == 1 && methodCallExpr.getArguments().get(0).isNameExpr()
&& methodCallExpr.getArguments().get(0).asNameExpr().getName().equals(singleParameter.getName())) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -249,6 +250,16 @@ public List<?> pre(Stream<? extends Class<?>> s) {
}
}

// https://github.com/solven-eu/cleanthat/issues/847
@UnmodifiedMethod
public static class CaseRootIsNotLambdaVariable {
// Must not be turned into
// `o.ifPresent(sb.append("/")::append);`
public void pre(Optional<?> o, StringBuilder sb) {
o.ifPresent(s -> sb.append("/").append(s));
}
}

// https://www.baeldung.com/java-method-references#examples-limitations
@UnmodifiedMethod
public static class refToExternalVariables {
Expand Down

0 comments on commit f064f10

Please sign in to comment.