Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7aa7114

Browse filesBrowse files
authored
Fix #14796: FP assertWithSideEffect for member functions without definition (#8607)
1 parent f87363d commit 7aa7114
Copy full SHA for 7aa7114

3 files changed

+5-5Lines changed: 5 additions & 5 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/checkassert.cpp‎

Copy file name to clipboardExpand all lines: lib/checkassert.cpp
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void CheckAssertImpl::assertWithSideEffects()
8383
if (!scope) {
8484
// guess that const method doesn't have side effects
8585
if (f->nestedIn->isClassOrStruct() && !f->isConst() && !f->isStatic())
86-
sideEffectInAssertError(tmp, f->name()); // Non-const member function called, assume it has side effects
86+
sideEffectInAssertError(tmp, f->name(), " If there are no side effects, consider declaring the method const."); // Non-const member function called, assume it has side effects
8787
continue;
8888
}
8989

@@ -117,12 +117,12 @@ void CheckAssertImpl::assertWithSideEffects()
117117
//---------------------------------------------------------------------------
118118

119119

120-
void CheckAssertImpl::sideEffectInAssertError(const Token *tok, const std::string& functionName)
120+
void CheckAssertImpl::sideEffectInAssertError(const Token *tok, const std::string& functionName, const std::string &extra)
121121
{
122122
reportError(tok, Severity::warning,
123123
"assertWithSideEffect",
124124
"$symbol:" + functionName + "\n"
125-
"Assert statement calls a function which may have desired side effects: '$symbol'.\n"
125+
"Assert statement calls a function which may have desired side effects: '$symbol'." + extra + "\n"
126126
"Non-pure function: '$symbol' is called inside assert statement. "
127127
"Assert statements are removed from release builds so the code inside "
128128
"assert statement is not executed. If the code is needed also in release "
Collapse file

‎lib/checkassert.h‎

Copy file name to clipboardExpand all lines: lib/checkassert.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CPPCHECKLIB CheckAssertImpl : public CheckImpl {
6565
void checkVariableAssignment(const Token* assignTok, const Scope *assertionScope);
6666
static bool inSameScope(const Token* returnTok, const Token* assignTok);
6767

68-
void sideEffectInAssertError(const Token *tok, const std::string& functionName);
68+
void sideEffectInAssertError(const Token *tok, const std::string& functionName, const std::string &extra = "");
6969
void assignmentInAssertError(const Token *tok, const std::string &varname);
7070
};
7171
/// @}
Collapse file

‎test/testassert.cpp‎

Copy file name to clipboardExpand all lines: test/testassert.cpp
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class TestAssert : public TestFixture {
156156
"void foo(SquarePack s) {\n"
157157
" assert( s.Foo() );\n"
158158
"}");
159-
ASSERT_EQUALS("[test.cpp:5:14]: (warning) Assert statement calls a function which may have desired side effects: 'Foo'. [assertWithSideEffect]\n", errout_str());
159+
ASSERT_EQUALS("[test.cpp:5:14]: (warning) Assert statement calls a function which may have desired side effects: 'Foo'. If there are no side effects, consider declaring the method const. [assertWithSideEffect]\n", errout_str());
160160

161161
check("struct SquarePack {\n"
162162
" int Foo() const;\n"

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.