Fix NullPointerException when visitListItem encounters a non-paragraph first child.#1411
Merged
Conversation
747bf1b to
4586194
Compare
…graph first child. `visitListItem` in `MarkdownPositions` previously used a pattern-matching `switch` on `listItem.getFirstChild()`. If a Markdown `ListItem` starts with a non-paragraph child (such as when an empty or unfinished bullet item `/// -` has `null` as its first child), the `switch` statement throws a `NullPointerException` because it does not explicitly handle `null`. This change replaces the `switch` with `if (listItem.getFirstChild() instanceof Paragraph paragraph)` to safely return `false` on `null` or non-`Paragraph` children without throwing an exception. It also updates `LIST_ITEM_START_PATTERN` to allow list markers without trailing whitespace at the end of the input (`$`). Tested: Added `markdownEmptyListItem` in `JavadocFormattingTest.java` and ran `blaze test //third_party/java_src/google_java_format/...`. PiperOrigin-RevId: 945823851
4586194 to
adf9252
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.
Fix
NullPointerExceptionwhenvisitListItemencounters a non-paragraph first child.visitListIteminMarkdownPositionspreviously used a pattern-matchingswitchonlistItem.getFirstChild(). If a MarkdownListItemstarts with a non-paragraph child (such as when an empty or unfinished bullet item/// -hasnullas its first child), theswitchstatement throws aNullPointerExceptionbecause it does not explicitly handlenull.This change replaces the
switchwithif (listItem.getFirstChild() instanceof Paragraph paragraph)to safely returnfalseonnullor non-Paragraphchildren without throwing an exception. It also updatesLIST_ITEM_START_PATTERNto allow list markers without trailing whitespace at the end of the input ($).Tested:
Added
markdownEmptyListIteminJavadocFormattingTest.javaand ranblaze test //third_party/java_src/google_java_format/....