From 07c4d31cfdcedf5bb9fb8c0044f09940cc22d483 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Thu, 14 Apr 2022 15:31:37 +0200 Subject: Improve consistency of QQmlJSUtils::didYouMean() We have to either sort the input candidate list or ensure that we always get same-ordered candidates at the call sites. The latter seems rather problematic since we usually use QHash::keys() as candidates so blarantly sort the candidates instead Complexity-wise, this adds an extra O(n * log(n)) procedure (where n is the size of the candidates list) which might or might not be amortized by the main logic (depends on average input/candidate string lengths) Change-Id: Ib528d453e2f4b2d6646f1451f5aefa559a0bdc10 Reviewed-by: Fabian Kosmale --- src/qmlcompiler/qqmljsutils.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/qmlcompiler/qqmljsutils.cpp') diff --git a/src/qmlcompiler/qqmljsutils.cpp b/src/qmlcompiler/qqmljsutils.cpp index 9feb102298..b593b02b9a 100644 --- a/src/qmlcompiler/qqmljsutils.cpp +++ b/src/qmlcompiler/qqmljsutils.cpp @@ -31,11 +31,22 @@ #include std::optional QQmlJSUtils::didYouMean(const QString &userInput, - const QStringList &candidates, + QStringList candidates, QQmlJS::SourceLocation location) { QString shortestDistanceWord; int shortestDistance = userInput.length(); + + // Most of the time the candidates are keys() from QHash, which means that + // running this function in the seemingly same setup might yield different + // best cadidate (e.g. imagine a typo 'thing' with candidates 'thingA' vs + // 'thingB'). This is especially flaky in e.g. test environment where the + // results may differ (even when the global hash seed is fixed!) when + // running one test vs the whole test suite (recall platform-dependent + // QSKIPs). There could be user-visible side effects as well, so just sort + // the candidates to guarantee consistent results + std::sort(candidates.begin(), candidates.end()); + for (const QString &candidate : candidates) { /* * Calculate the distance between the userInput and candidate using Damerau–Levenshtein -- cgit v1.2.3