diff --git a/src/plugins/cpptools/cppcompletionassist.cpp b/src/plugins/cpptools/cppcompletionassist.cpp
index 8dd911aac858a697e697eb3392fc778110b375b5..f3a6a08f9e31a5e3139aca24b9f7c0046403ac96 100644
--- a/src/plugins/cpptools/cppcompletionassist.cpp
+++ b/src/plugins/cpptools/cppcompletionassist.cpp
@@ -344,7 +344,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
     if (!inEditor.isEmpty()) {
         preserveLength = toInsert.length() - (editor->position() - basePosition);
         const int inEditorLength = inEditor.length();
-        while (preserveLength) {
+        while (preserveLength > 0) {
             if (inEditor.startsWith(toInsert.right(preserveLength))
                     && (inEditorLength == preserveLength
                         || (!inEditor.at(preserveLength).isLetterOrNumber()
diff --git a/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp b/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp
index 9a488dfaa6f88174d193ad331200678f0fada2c7..84657911c799c24e23954606219e06115889a7fe 100644
--- a/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp
+++ b/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp
@@ -271,22 +271,27 @@ bool BasicProposalItemListModel::keepPerfectMatch(AssistReason reason) const
 
 QString BasicProposalItemListModel::proposalPrefix() const
 {
-    if (m_currentItems.size() >= kMaxPrefixFilter)
+    if (m_currentItems.size() >= kMaxPrefixFilter || m_currentItems.isEmpty())
         return QString();
 
     // Compute common prefix
-    QString firstKey = m_currentItems.first()->text();
-    QString lastKey = m_currentItems.last()->text();
-    const int length = qMin(firstKey.length(), lastKey.length());
-    firstKey.truncate(length);
-    lastKey.truncate(length);
-
-    while (firstKey != lastKey) {
-        firstKey.chop(1);
-        lastKey.chop(1);
+    QString commonPrefix = m_currentItems.first()->text();
+    for (int i = 1, ei = m_currentItems.size(); i < ei; ++i) {
+        QString nextItem = m_currentItems.at(i)->text();
+        const int length = qMin(commonPrefix.length(), nextItem.length());
+        commonPrefix.truncate(length);
+        nextItem.truncate(length);
+
+        while (commonPrefix != nextItem) {
+            commonPrefix.chop(1);
+            nextItem.chop(1);
+        }
+
+        if (commonPrefix.isEmpty()) // there is no common prefix, so return.
+            return commonPrefix;
     }
 
-    return firstKey;
+    return commonPrefix;
 }
 
 IAssistProposalItem *BasicProposalItemListModel::proposalItem(int index) const