Skip to content

Commit 2c0ad30

Browse files
cushoncpovirk
authored andcommitted
Only apply UnnecessaryLambda to FIs in core packages
e.g. java.util.function.Function, not arbitrary user-defined FI types, which may have more documentation value. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=308682538
1 parent b58a90a commit 2c0ad30

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryLambda.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static com.google.errorprone.util.ASTHelpers.getType;
2929
import static java.util.stream.Collectors.joining;
3030

31+
import com.google.common.collect.ImmutableSet;
3132
import com.google.common.collect.Streams;
3233
import com.google.errorprone.BugPattern;
3334
import com.google.errorprone.VisitorState;
@@ -161,13 +162,24 @@ public Void visitIdentifier(IdentifierTree node, Void unused) {
161162
return describeMatch(tree, fix.build());
162163
}
163164

165+
// Allowlist of core packages to emit fixes for functional interfaces in. User-defined functional
166+
// interfaces are slightly more likely to have documentation value.
167+
private static final ImmutableSet<String> PACKAGES_TO_FIX =
168+
ImmutableSet.of(
169+
"com.google.common.base",
170+
"com.google.errorprone.matchers",
171+
"java.util.function",
172+
"java.lang");
164173
/**
165174
* Check if the only methods invoked on the functional interface type are the descriptor method,
166175
* e.g. don't rewrite uses of {@link Predicate} in compilation units that call other methods like
167176
* {#link Predicate#add}.
168177
*/
169178
boolean canFix(Tree type, Symbol sym, VisitorState state) {
170179
Symbol descriptor = state.getTypes().findDescriptorSymbol(getType(type).asElement());
180+
if (!PACKAGES_TO_FIX.contains(descriptor.packge().getQualifiedName().toString())) {
181+
return false;
182+
}
171183
class Scanner extends TreePathScanner<Void, Void> {
172184

173185
boolean fixable = true;

0 commit comments

Comments
 (0)