diff --git a/src/plugins/languageclient/languageclientformatter.cpp b/src/plugins/languageclient/languageclientformatter.cpp index 774b0899dee..f67302aa0e7 100644 --- a/src/plugins/languageclient/languageclientformatter.cpp +++ b/src/plugins/languageclient/languageclientformatter.cpp @@ -49,6 +49,7 @@ static const FormattingOptions formattingOptions(const TextEditor::TabSettings & QFutureWatcher *LanguageClientFormatter::format( const QTextCursor &cursor, const TextEditor::TabSettings &tabSettings) { + QTC_ASSERT(m_client, return nullptr); cancelCurrentRequest(); m_progress = QFutureInterface(); @@ -101,7 +102,7 @@ QFutureWatcher *LanguageClientFormatter::format( void LanguageClientFormatter::cancelCurrentRequest() { - if (m_currentRequest.has_value()) { + if (QTC_GUARD(m_client) && m_currentRequest.has_value()) { m_progress.reportCanceled(); m_progress.reportFinished(); m_client->cancelRequest(*m_currentRequest); @@ -113,8 +114,8 @@ void LanguageClientFormatter::cancelCurrentRequest() void LanguageClientFormatter::handleResponse(const DocumentRangeFormattingRequest::Response &response) { m_currentRequest = std::nullopt; - if (const std::optional &error = response - .error()) + const std::optional &error = response.error(); + if (QTC_GUARD(m_client) && error) m_client->log(*error); ChangeSet changeSet; if (std::optional> result = response.result()) { diff --git a/src/plugins/languageclient/languageclientformatter.h b/src/plugins/languageclient/languageclientformatter.h index e61e749d27b..1ddee2892ba 100644 --- a/src/plugins/languageclient/languageclientformatter.h +++ b/src/plugins/languageclient/languageclientformatter.h @@ -7,6 +7,8 @@ #include +#include + namespace TextEditor { class TextDocument; } namespace LanguageClient { @@ -26,7 +28,7 @@ private: void handleResponse( const LanguageServerProtocol::DocumentRangeFormattingRequest::Response &response); - Client *m_client = nullptr; // not owned + QPointer m_client = nullptr; // not owned QMetaObject::Connection m_cancelConnection; TextEditor::TextDocument *m_document; // not owned bool m_ignoreCancel = false;