Skip to content

Commit 5e66d8d

Browse files
committed
Handle null getLineMap()
1 parent 0e9919e commit 5e66d8d

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

mug-errorprone/src/main/java/com/google/mu/errorprone/ParametersMustMatchByNameCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ private void checkParameters(
7373
List<String> argSources,
7474
VisitorState state)
7575
throws ErrorReport {
76-
if (!isEffectivelyAnnotated(method, state)) {
77-
return;
76+
if (argSources.isEmpty() || !isEffectivelyAnnotated(method, state)) {
77+
return;
7878
}
7979
boolean methodAnnotated = ASTHelpers.hasAnnotation(method, ANNOTATION_NAME, state);
8080
ClassTree classTree = state.findEnclosing(ClassTree.class);

mug-errorprone/src/main/java/com/google/mu/errorprone/SourceUtils.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,37 @@ static String normalizeForComparison(String code) {
5656
}
5757

5858
static ImmutableList<String> argsAsTexts(
59-
ExpressionTree invocationStart, List<? extends ExpressionTree> args, VisitorState state) {
60-
int position = state.getEndPosition(invocationStart);
59+
ExpressionTree methodName, List<? extends ExpressionTree> args, VisitorState state) {
60+
int position = state.getEndPosition(methodName);
6161
if (position < 0) {
6262
return ImmutableList.of();
6363
}
6464
LineMap lineMap = state.getPath().getCompilationUnit().getLineMap();
65+
if (lineMap == null) {
66+
return ImmutableList.of();
67+
}
6568
long startingLine = lineMap.getLineNumber(position);
66-
ImmutableList<Long> argLineNumbers =
69+
ImmutableList<Long> argLines =
6770
args.stream()
6871
.map(arg -> lineMap.getLineNumber(getStartPosition(arg)))
6972
.collect(toImmutableList());
70-
boolean isMultiline = argLineNumbers.stream().anyMatch(line -> line > startingLine);
73+
boolean isMultiline = argLines.stream().anyMatch(line -> line > startingLine);
7174
ImmutableList.Builder<String> builder = ImmutableList.builder();
7275
for (int i = 0; i < args.size(); i++) {
7376
ExpressionTree arg = args.get(i);
7477
int next = state.getEndPosition(arg);
7578
if (next < 0) {
7679
return ImmutableList.of();
7780
}
78-
boolean lastArgOfLine =
79-
i == args.size() - 1 || argLineNumbers.get(i) < argLineNumbers.get(i + 1);
80-
int end = isMultiline && lastArgOfLine ? locateLineEnd(state, next) : next;
81+
boolean lastArgOfLine = i == args.size() - 1 || argLines.get(i) < argLines.get(i + 1);
82+
int end = isMultiline && lastArgOfLine ? locateLineEnd(state.getSourceCode(), next) : next;
8183
builder.add(state.getSourceCode().subSequence(position, end).toString());
8284
position = end;
8385
}
8486
return builder.build();
8587
}
8688

87-
private static int locateLineEnd(VisitorState state, int pos) {
88-
CharSequence source = state.getSourceCode();
89+
private static int locateLineEnd(CharSequence source, int pos) {
8990
int end = pos;
9091
while (end < source.length() && source.charAt(end) != '\n') {
9192
end++;

mug-errorprone/src/main/java/com/google/mu/errorprone/StringFormatArgsCheck.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ private void checkTemplateFormatArgs(
241241
List<String> argSources,
242242
VisitorState state)
243243
throws ErrorReport {
244+
if (argSources.isEmpty()) {
245+
return;
246+
}
244247
int templateStringIndex =
245248
BiStream.zip(indexesFrom(0), params.stream())
246249
.filterValues(

0 commit comments

Comments
 (0)