Skip to content

Commit

Permalink
Render zero sized spans in dump-syntax example
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Apr 24, 2023
1 parent 0bf7104 commit 8e02dd1
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions examples/dump-syntax/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ fn render_location(
let start = err.span().start();
let mut end = err.span().end();

if start.line == end.line && start.column == end.column {
return render_fallback(formatter, err);
}

let code_line = match code.lines().nth(start.line - 1) {
let code_line = match start.line.checked_sub(1).and_then(|n| code.lines().nth(n)) {
Some(line) => line,
None => return render_fallback(formatter, err),
};
Expand Down Expand Up @@ -138,7 +134,7 @@ fn render_location(
label = start.line.to_string().blue().bold(),
code = code_line.trim_end(),
offset = " ".repeat(start.column),
underline = "^".repeat(end.column - start.column).red().bold(),
underline = "^".repeat(end.column.saturating_sub(start.column).max(1)).red().bold(),
message = err.to_string().red(),
)
}
Expand Down

0 comments on commit 8e02dd1

Please sign in to comment.