Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class TestFunctions : public TestFixture {
TEST_CASE(checkUseStandardLibrary12);
TEST_CASE(checkUseStandardLibrary13);
TEST_CASE(checkUseStandardLibrary14);

TEST_CASE(checkFunctionCallOperatorOverrideWithAutoReference);
}

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
Expand Down Expand Up @@ -2109,6 +2111,50 @@ class TestFunctions : public TestFixture {
"}}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::memset instead of loop.\n", errout.str());
}

void checkFunctionCallOperatorOverrideWithAutoReference() {
const auto settings_old = settings;
settings.checkLibrary = true;
check("template <typename T>\n"
"struct EPVector : public std::vector<T> {\n"
" using size_type = typename std::vector<T>::size_type;\n"
" [[nodiscard]] T& operator()(size_type n) { return (*this)[n - 1]; }\n"
" [[nodiscard]] const T& operator()(size_type n) const { return (*this)[n - 1]; }\n"
" void resize(size_type count) { std::vector<T>::resize(count); }\n"
"};\n"
"struct S { EPVector<double> vec; };\n"
"double f() {\n"
" EPVector<S> sVec;\n"
" sVec.resize(1);\n"
" sVec(1).vec.resize(2);\n"
" sVec(1).vec(1) = 1.0;\n"
" S& thisS = sVec(1);\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works when using the type explicitly.

" thisS.vec(2) = 2.0;\n"
" return sVec(1).vec(1) + thisS.vec(2);\n"
"}");
ASSERT_EQUALS("", errout.str());

check("template <typename T>\n"
"struct EPVector : public std::vector<T> {\n"
" using size_type = typename std::vector<T>::size_type;\n"
" [[nodiscard]] T& operator()(size_type n) { return (*this)[n - 1]; }\n"
" [[nodiscard]] const T& operator()(size_type n) const { return (*this)[n - 1]; }\n"
" void resize(size_type count) { std::vector<T>::resize(count); }\n"
"};\n"
"struct S { EPVector<double> vec; };\n"
"double f() {\n"
" EPVector<S> sVec;\n"
" sVec.resize(1);\n"
" sVec(1).vec.resize(2);\n"
" sVec(1).vec(1) = 1.0;\n"
" auto& thisS = sVec(1);\n" // NOTE: Using `auto&` instead of `S&`
" thisS.vec(2) = 2.0;\n" // TODO: --check-library: There is no matching configuration for function auto::vec()
" return sVec(1).vec(1) + thisS.vec(2);\n" // TODO: --check-library: There is no matching configuration for function auto::vec()
"}");
ASSERT_EQUALS("", errout.str());
Comment on lines +2137 to +2154
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fails.

cppcheck/test/testfunctions.cpp:2154(TestFunctions::checkFunctionCallOperatorOverrideWithAutoReference): Assertion failed.
Expected:

Actual:
[test.cpp:15]: (information) --check-library: There is no matching configuration for function auto::vec()\n
[test.cpp:16]: (information) --check-library: There is no matching configuration for function auto::vec()\n

Here's a compiler explorer link showing that the code itself is fine: https://compiler-explorer.com/z/o7d6PzK6b


settings = settings_old;
}
};

REGISTER_TEST(TestFunctions)