Turn dangling documentation comments into block comments - #1224
Merged
Conversation
jkschneider
force-pushed
the
dangling-doc-comments
branch
from
August 24, 2026 12:57
f5c6bf5 to
82022d4
Compare
Java 22 added `-Xlint:dangling-doc-comments`, so a documentation comment that documents nothing now warns, and a build using `-Werror` that was fine before the version bump stops compiling. Changing the opening delimiter keeps the text and silences it. Telling the two apart needs no heuristic: a documentation comment attached to a declaration parses to a DocComment, so any comment still in a prefix as raw text is one the compiler will not associate with anything. The comment leading a file is left alone, since that is conventionally the license header.
jkschneider
force-pushed
the
dangling-doc-comments
branch
from
August 24, 2026 13:00
82022d4 to
d586169
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JDK 22 added
-Xlint:dangling-doc-comments, which warns about a documentation comment that does not precede a declaration. A project building with-Werrortherefore stops building purely because its Java version went up — its source is unchanged and was fine before.Real shape, from a project that had documented a field and later commented the field out:
/** The request. */documents nothing. Changing its opening delimiter to/*keeps the text and silences the warning, which is what a person would do.Telling the two apart needs no heuristic
This is the part that makes the recipe safe. A documentation comment attached to a declaration is parsed into
Javadoc.DocComment. One that is not stays in a prefixSpaceas a plain multilineTextCommentwhose text opens with the extra asterisk of/**. Confirmed against the parser across every position that matters:/** Doc. */before a fieldJavadoc.DocComment/** Doc. */before an annotated methodJavadoc.DocComment/** Doc. */before a commented-out field, with another doc comment followingTextComment/** Doc. */before animportTextComment/** Doc. */between@Deprecatedand the methodTextCommentSo real API documentation cannot be caught by this recipe — it is a different node type entirely. That was the failure mode worth designing against, since silently stripping Javadoc would be far worse than the warning.
Note the parser attaches the nearest preceding documentation comment, which matches javac: an earlier one only becomes dangling when another documentation comment follows it. A test case with a single documentation comment will not reproduce the problem.
The comment leading a file is left alone, since that is conventionally the license header.
Tests
Seven cases: the shape above, before an import, after an annotation, multiline, and three negative cases covering attached documentation, the file header, and ordinary block comments.
Found by compiling the output of
UpgradeToJava25across a set of open source repositories.