-
Notifications
You must be signed in to change notification settings - Fork 1.5k
check-library warns on access to a function call operator via an auto reference #4834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jmarrec
wants to merge
1
commit into
danmar:main
Choose a base branch
from
jmarrec:checkFunctionCallOperatorOverrideWithAutoReference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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__) | ||
|
|
@@ -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" | ||
| " 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this fails. 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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.