Skip to content

Commit

Permalink
Include the unicode character in the diagnostic message
Browse files Browse the repository at this point in the history
To help debug #3092

PiperOrigin-RevId: 442071506
  • Loading branch information
cushon authored and Error Prone Team committed Apr 15, 2022
1 parent ed35fda commit 726d179
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.util.ErrorProneTokens.getTokens;
import static java.lang.String.format;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableRangeSet;
Expand All @@ -31,10 +32,11 @@
import com.google.errorprone.fixes.FixedPosition;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ErrorProneToken;
import com.google.errorprone.util.SourceCodeEscapers;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.tools.javac.parser.Tokens.TokenKind;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;

/** Bans using non-ASCII Unicode characters outside string literals and comments. */
@BugPattern(
Expand All @@ -47,27 +49,36 @@ public final class UnicodeInCode extends BugChecker implements CompilationUnitTr
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
ImmutableRangeSet<Integer> commentsAndLiterals = commentsAndLiterals(state);

List<Integer> violatingLocations = new ArrayList<>();
Map<Integer, Character> violations = new LinkedHashMap<>();

CharSequence sourceCode = state.getSourceCode();

for (int i = 0; i < sourceCode.length(); ++i) {
char c = sourceCode.charAt(i);

if (!isAcceptableAscii(c) && !commentsAndLiterals.contains(i)) {
violatingLocations.add(i);
violations.put(i, c);
}
}

if (violatingLocations.isEmpty()) {
if (violations.isEmpty()) {
return NO_MATCH;
}

ImmutableRangeSet<Integer> suppressedRegions = suppressedRegions(state);

for (Integer violatingLocation : violatingLocations) {
for (var e : violations.entrySet()) {
int violatingLocation = e.getKey();
char c = e.getValue();
if (!suppressedRegions.contains(violatingLocation)) {
state.reportMatch(describeMatch(new FixedPosition(tree, violatingLocation)));
state.reportMatch(
buildDescription(new FixedPosition(tree, violatingLocation))
.setMessage(
format(
"Avoid using non-ASCII Unicode character (%s) outside of comments and"
+ " literals, as they can be confusing.",
SourceCodeEscapers.javaCharEscaper().escape(Character.toString(c))))
.build());
}
}
return NO_MATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void positive() {
.addSourceLines(
"Test.java", //
"class Test {",
" // BUG: Diagnostic contains:",
" // BUG: Diagnostic contains: Unicode character (\\u03c0)",
" static final double \u03C0 = 3;",
"}")
.doTest();
Expand Down

0 comments on commit 726d179

Please sign in to comment.