ClangCodeModel: Request clangd quickfix for -Wswitch warning

This way, a warning about missing switch cases will be accompanied by a
matching code action, which allows the user to fix it right away by
clicking on the lightbulb.
Requires clangd >= 15; see https://reviews.llvm.org/D118976.

Change-Id: I11e82264c41e4154f979d28a5e44e72c8158595b
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-02-04 10:59:11 +01:00
parent 531e24892b
commit 15280d7127
3 changed files with 37 additions and 6 deletions

View File

@@ -1544,8 +1544,18 @@ void ClangdClient::handleDiagnostics(const PublishDiagnosticsParams &params)
return;
for (const Diagnostic &diagnostic : params.diagnostics()) {
const ClangdDiagnostic clangdDiagnostic(diagnostic);
for (const CodeAction &action : clangdDiagnostic.codeActions().value_or(QList<CodeAction>{}))
LanguageClient::updateCodeActionRefactoringMarker(this, action, uri);
const auto codeActions = clangdDiagnostic.codeActions();
if (codeActions && !codeActions->isEmpty()) {
for (const CodeAction &action : *codeActions)
LanguageClient::updateCodeActionRefactoringMarker(this, action, uri);
} else {
// We know that there's only one kind of diagnostic for which clangd has
// a quickfix tweak, so let's not be wasteful.
const Diagnostic::Code code = diagnostic.code().value_or(Diagnostic::Code());
const QString * const codeString = Utils::get_if<QString>(&code);
if (codeString && *codeString == "-Wswitch")
requestCodeActions(uri, diagnostic);
}
}
}