Skip to content

Commit 8583fcf

Browse files
authored
avoid more cases of returning non-const pointers from const objects (#4821)
1 parent d65cc69 commit 8583fcf

7 files changed

Lines changed: 22 additions & 15 deletions

File tree

gui/mainwindow.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,13 @@ void MainWindow::loadSettings()
335335
mSettings->value(SETTINGS_WINDOW_HEIGHT, 600).toInt());
336336
}
337337

338-
ShowTypes *types = mUI->mResults->getShowTypes();
339-
mUI->mActionShowStyle->setChecked(types->isShown(ShowTypes::ShowStyle));
340-
mUI->mActionShowErrors->setChecked(types->isShown(ShowTypes::ShowErrors));
341-
mUI->mActionShowWarnings->setChecked(types->isShown(ShowTypes::ShowWarnings));
342-
mUI->mActionShowPortability->setChecked(types->isShown(ShowTypes::ShowPortability));
343-
mUI->mActionShowPerformance->setChecked(types->isShown(ShowTypes::ShowPerformance));
344-
mUI->mActionShowInformation->setChecked(types->isShown(ShowTypes::ShowInformation));
338+
const ShowTypes &types = mUI->mResults->getShowTypes();
339+
mUI->mActionShowStyle->setChecked(types.isShown(ShowTypes::ShowStyle));
340+
mUI->mActionShowErrors->setChecked(types.isShown(ShowTypes::ShowErrors));
341+
mUI->mActionShowWarnings->setChecked(types.isShown(ShowTypes::ShowWarnings));
342+
mUI->mActionShowPortability->setChecked(types.isShown(ShowTypes::ShowPortability));
343+
mUI->mActionShowPerformance->setChecked(types.isShown(ShowTypes::ShowPerformance));
344+
mUI->mActionShowInformation->setChecked(types.isShown(ShowTypes::ShowInformation));
345345
mUI->mActionShowCppcheck->setChecked(true);
346346
mUI->mActionShowClang->setChecked(true);
347347

gui/resultsview.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ void ResultsView::clearRecheckFile(const QString &filename)
113113
mUI->mTree->clearRecheckFile(filename);
114114
}
115115

116-
ShowTypes * ResultsView::getShowTypes() const
116+
const ShowTypes & ResultsView::getShowTypes() const
117117
{
118-
return &mUI->mTree->mShowSeverities;
118+
return mUI->mTree->mShowSeverities;
119119
}
120120

121121
void ResultsView::progress(int value, const QString& description)

gui/resultsview.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ class ResultsView : public QWidget {
189189
* @brief Return checking statistics.
190190
* @return Pointer to checking statistics.
191191
*/
192-
CheckStatistics *getStatistics() const {
192+
const CheckStatistics *getStatistics() const {
193193
return mStatistics;
194194
}
195195

196196
/**
197197
* @brief Return Showtypes.
198198
* @return Pointer to Showtypes.
199199
*/
200-
ShowTypes * getShowTypes() const;
200+
const ShowTypes & getShowTypes() const;
201201

202202
signals:
203203

lib/symboldatabase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,12 +1399,12 @@ class CPPCHECKLIB SymbolDatabase {
13991399
const Scope *findScopeByName(const std::string& name) const;
14001400

14011401
const Type* findType(const Token *startTok, const Scope *startScope, bool lookOutside = false) const;
1402-
Type* findType(const Token *startTok, Scope *startScope, bool lookOutside = false) const {
1402+
Type* findType(const Token *startTok, Scope *startScope, bool lookOutside = false) {
14031403
return const_cast<Type*>(this->findType(startTok, const_cast<const Scope *>(startScope), lookOutside));
14041404
}
14051405

14061406
const Scope *findScope(const Token *tok, const Scope *startScope) const;
1407-
Scope *findScope(const Token *tok, Scope *startScope) const {
1407+
Scope *findScope(const Token *tok, Scope *startScope) {
14081408
return const_cast<Scope *>(this->findScope(tok, const_cast<const Scope *>(startScope)));
14091409
}
14101410

lib/templatesimplifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class CPPCHECKLIB TemplateSimplifier {
156156
mNameToken == rhs.mNameToken && mParamEnd == rhs.mParamEnd && mFlags == rhs.mFlags;
157157
}
158158

159+
// TODO: do not return non-const pointer from const object
159160
Token * token() const {
160161
return mToken;
161162
}

lib/tokenize.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ static Token *splitDefinitionFromTypedef(Token *tok, nonneg int *unnamedCount)
510510
* code that generated it deals in some way with functions, then this
511511
* function will probably need to be extended to handle a new function
512512
* related pattern */
513-
Token *Tokenizer::processFunc(Token *tok2, bool inOperator) const
513+
const Token *Tokenizer::processFunc(const Token *tok2, bool inOperator) const
514514
{
515515
if (tok2->next() && tok2->next()->str() != ")" &&
516516
tok2->next()->str() != ",") {
@@ -570,6 +570,11 @@ Token *Tokenizer::processFunc(Token *tok2, bool inOperator) const
570570
return tok2;
571571
}
572572

573+
Token *Tokenizer::processFunc(Token *tok2, bool inOperator)
574+
{
575+
return const_cast<Token*>(processFunc(const_cast<const Token*>(tok2), inOperator));
576+
}
577+
573578
void Tokenizer::simplifyUsingToTypedef()
574579
{
575580
if (!isCPP() || mSettings->standards.cpp < Standards::CPP11)

lib/tokenize.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ class CPPCHECKLIB Tokenizer {
693693
Tokenizer &operator=(const Tokenizer &) = delete;
694694

695695
private:
696-
Token *processFunc(Token *tok2, bool inOperator) const;
696+
const Token *processFunc(const Token *tok2, bool inOperator) const;
697+
Token *processFunc(Token *tok2, bool inOperator);
697698

698699
/**
699700
* Get new variable id.

0 commit comments

Comments
 (0)