Skip to content

Commit

Permalink
Complain about @param foo: blah.
Browse files Browse the repository at this point in the history
The colon winds up appearing in the generated docs in the summary.

PiperOrigin-RevId: 581238725
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 10, 2023
1 parent 5854742 commit 58aa646
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.DocTree.Kind;
Expand All @@ -47,6 +48,7 @@
import com.sun.source.util.DocTreePath;
import com.sun.source.util.DocTreePathScanner;
import com.sun.tools.javac.tree.DCTree.DCDocComment;
import com.sun.tools.javac.tree.DCTree.DCText;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -148,8 +150,22 @@ private ParamsChecker(
@Override
public Void visitParam(ParamTree paramTree, Void unused) {
ImmutableSet<String> paramNames = paramTree.isTypeParameter() ? typeParameters : parameters;
if (!paramTree.getDescription().isEmpty()) {
var firstDescription = paramTree.getDescription().get(0);
if (firstDescription instanceof DCText) {
if (((DCText) firstDescription).getBody().startsWith(":")) {
int colonLocation = Utils.getEndPosition(paramTree.getName(), state);
if (state.getSourceCode().charAt(colonLocation) == ':') {
state.reportMatch(
describeMatch(
diagnosticPosition(getCurrentPath(), state),
SuggestedFix.replace(colonLocation, colonLocation + 1, "")));
}
}
}
}
if (!paramNames.contains(paramTree.getName().toString())) {
ImmutableSet<String> documentedParamNames =
var documentedParamNames =
paramTree.isTypeParameter() ? documentedTypeParameters : documentedParameters;
Set<String> undocumentedParameters = Sets.difference(paramNames, documentedParamNames);
Optional<String> bestMatch =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.google.errorprone.bugpatterns.javadoc;

import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH;
import static org.junit.Assume.assumeTrue;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
Expand Down Expand Up @@ -52,6 +52,28 @@ public void badParameterName_positioning() {
.doTest();
}

@Test
public void badColon() {
refactoring
.addInputLines(
"Test.java",
"interface Test {",
" /**",
" * @param c: foo",
" */",
" <T> void foo(int c);",
"}")
.addOutputLines(
"Test.java",
"interface Test {",
" /**",
" * @param c foo",
" */",
" <T> void foo(int c);",
"}")
.doTest(TEXT_MATCH);
}

@Test
public void badParameterName() {
refactoring
Expand All @@ -75,7 +97,7 @@ public void badParameterName() {
" */",
" <T> void foo(int a, int b);",
"}")
.doTest(TestMode.TEXT_MATCH);
.doTest(TEXT_MATCH);
}

@Test
Expand All @@ -99,7 +121,7 @@ public void badTypeParameterName() {
" */",
" <T> void foo(int a);",
"}")
.doTest(TestMode.TEXT_MATCH);
.doTest(TEXT_MATCH);
}

@Test
Expand All @@ -121,7 +143,7 @@ public void verySimilarCodeParam() {
" */",
" void foo(int foobar);",
"}")
.doTest(TestMode.TEXT_MATCH);
.doTest(TEXT_MATCH);
}

@Test
Expand Down

0 comments on commit 58aa646

Please sign in to comment.