Skip to content

Fix edge case in InconsistentCapitalization #4801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Expand Up @@ -16,11 +16,12 @@

package com.google.errorprone.bugpatterns;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.util.ASTHelpers.isStatic;

import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
Expand All @@ -39,6 +40,8 @@
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol;

import java.util.List;
import java.util.Map;
import javax.lang.model.element.ElementKind;

Expand All @@ -58,11 +61,11 @@ public Description matchClass(ClassTree tree, VisitorState state) {
return Description.NO_MATCH;
}

ImmutableMap<String, Symbol> fieldNamesMap =
ImmutableListMultimap<String, Symbol> fieldNamesMap =
fields.stream()
.collect(
toImmutableMap(
symbol -> Ascii.toLowerCase(symbol.toString()), x -> x, (x, y) -> x));
toImmutableListMultimap(
symbol -> Ascii.toLowerCase(symbol.toString()), x -> x));
ImmutableMap<TreePath, Symbol> matchedParameters =
MatchingParametersScanner.findMatchingParameters(fieldNamesMap, state.getPath());

Expand Down Expand Up @@ -180,17 +183,17 @@ public Void visitVariable(VariableTree tree, Void unused) {
private static class MatchingParametersScanner extends TreePathScanner<Void, Void> {

static ImmutableMap<TreePath, Symbol> findMatchingParameters(
ImmutableMap<String, Symbol> fieldNamesMap, TreePath path) {
ImmutableListMultimap<String, Symbol> fieldNamesMap, TreePath path) {
ImmutableMap.Builder<TreePath, Symbol> matchedParametersBuilder = ImmutableMap.builder();
new MatchingParametersScanner(fieldNamesMap, matchedParametersBuilder).scan(path, null);
return matchedParametersBuilder.buildOrThrow();
}

private final ImmutableMap<String, Symbol> fields;
private final ImmutableListMultimap<String, Symbol> fields;
private final ImmutableMap.Builder<TreePath, Symbol> matchedParameters;

private MatchingParametersScanner(
ImmutableMap<String, Symbol> fields,
ImmutableListMultimap<String, Symbol> fields,
ImmutableMap.Builder<TreePath, Symbol> matchedParameters) {
this.fields = fields;
this.matchedParameters = matchedParameters;
Expand All @@ -212,12 +215,10 @@ public Void visitVariable(VariableTree tree, Void unused) {
return super.visitVariable(tree, null);
}
String variableName = symbol.toString();
Symbol matchedField = fields.get(Ascii.toLowerCase(variableName));
if (matchedField != null) {
String fieldName = matchedField.toString();
if (!variableName.equals(fieldName)) {
matchedParameters.put(getCurrentPath(), matchedField);
}
List<Symbol> matchedFields = fields.get(Ascii.toLowerCase(variableName));
if (!matchedFields.isEmpty()
&& matchedFields.stream().map(Symbol::toString).noneMatch(variableName::equals)) {
matchedParameters.put(getCurrentPath(), matchedFields.getFirst());
Comment on lines +218 to +221
Copy link
Author

Choose a reason for hiding this comment

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

I chose to use a List because using matchedFields.getFirst() is the simplest way to choose a matching field in a manner that is obviously deterministic. It would have to be a very weird class for the performance of iterating over the list to be a concern. However, I'd happily change the ListMultimap to something like a Map<String, Map<String, Symbol>> if requested.

}
return super.visitVariable(tree, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ static class Child extends Parent {
}
}
}

static class HandlesFieldsWithInconsistentCapitalization {

private int abc;
private int ABC;

void foo(int ABC) {

}
}
}\
""")
.doTest();
Expand Down