Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Apr 8, 2024
1 parent b953c05 commit d8b59dd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/checkclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CPPCHECKLIB CheckClass : public Check {
checkClass.checkCopyCtorAndEqOperator();
checkClass.checkOverride();
checkClass.checkUselessOverride();
checkClass.checkReturnReference();
checkClass.checkReturnByReference();
checkClass.checkThisUseAfterFree();
checkClass.checkUnsafeClassRefMember();
}
Expand Down Expand Up @@ -159,7 +159,7 @@ class CPPCHECKLIB CheckClass : public Check {
void checkUselessOverride();

/** @brief Check that large members are returned by reference from getter function */
void checkReturnReference();
void checkReturnByReference();

/** @brief When "self pointer" is destroyed, 'this' might become invalid. */
void checkThisUseAfterFree();
Expand Down
32 changes: 32 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class TestClass : public TestFixture {
TEST_CASE(ctuOneDefinitionRule);

TEST_CASE(testGetFileInfo);

TEST_CASE(returnByReference);
}

#define checkCopyCtorAndEqOperator(code) checkCopyCtorAndEqOperator_(code, __FILE__, __LINE__)
Expand Down Expand Up @@ -8959,6 +8961,36 @@ class TestClass : public TestFixture {
getFileInfo("struct sometype { sometype(); }; sometype::sometype() = delete;"); // don't crash
}

#define checkReturnByReference(...) checkReturnByReference_(__FILE__, __LINE__, __VA_ARGS__)
void checkReturnByReference_(const char* file, int line, const char code[]) {
const Settings settings = settingsBuilder().severity(Severity::performance).library("std.cfg").build();

std::vector<std::string> files(1, "test.cpp");
Tokenizer tokenizer(settings, this);
PreprocessorHelper::preprocess(code, files, tokenizer);

ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Check..
CheckClass checkClass(&tokenizer, &settings, this);
(checkClass.checkReturnByReference)();
}

void returnByReference() {
checkReturnByReference("struct T { int a[10]; };" // #12546
"struct S {"
" T t;"
" int i;"
" std::string s;"
" T getT() const { return t; }"
" int getI() const { return i; }"
" std::string getS() const { return s; }"
"};");
ASSERT_EQUALS("[test.cpp:1]: (style) Function 'getT()' should return member 't' by const reference.\n"
"[test.cpp:1]: (style) Function 'getS()' should return member 's' by const reference.\n",
errout_str());
}

};

REGISTER_TEST(TestClass)

0 comments on commit d8b59dd

Please sign in to comment.