Skip to content

Guard against panic in tokenize_markdown when removing final newline #176

@coderabbitai

Description

@coderabbitai

Issue Description

The tokenize_markdown function in src/tokenize.rs unconditionally calls out.pop() at line 150 to remove the final newline token. This could panic if the output vector is empty or incorrectly remove a non-newline token if the input doesn't end with a newline.

Location

  • File: src/tokenize.rs
  • Lines: 150-151

Problem

out.pop();
out

The unconditional pop() operation assumes:

  1. The vector is not empty
  2. The last token is always a newline

Suggested Fix

Add a guard to only remove the final token if it's actually a newline:

if matches!(out.last(), Some(Token::Newline)) {
    out.pop();
}
out

Context

Requested by

@leynos

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions