diff --git a/src/plugins/texteditor/semantichighlighter.cpp b/src/plugins/texteditor/semantichighlighter.cpp
index 0e541e8f41c93ea934ab78b8ad36c55ae5f9518a..ba36dc9c1a84d5992f5bf88bdba2e358ccab6842 100644
--- a/src/plugins/texteditor/semantichighlighter.cpp
+++ b/src/plugins/texteditor/semantichighlighter.cpp
@@ -125,8 +125,13 @@ void TextEditor::SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
     }
 
     QTextDocument *doc = highlighter->document();
-    QTC_ASSERT(lastBlockNumber + 1 < doc->blockCount(), return);
-    QTextBlock b = doc->findBlockByNumber(lastBlockNumber + 1);
+
+    const int firstBlockToClear = lastBlockNumber + 1;
+    if (firstBlockToClear == doc->blockCount())
+        return;
+    QTC_ASSERT(firstBlockToClear < doc->blockCount(), return);
+
+    QTextBlock b = doc->findBlockByNumber(firstBlockToClear);
 
     while (b.isValid()) {
         highlighter->setExtraAdditionalFormats(b, QList<QTextLayout::FormatRange>());