Skip to content

maint(Clang-Tidy): readability-const-return #3254

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

Merged
merged 7 commits into from
Sep 10, 2021

Conversation

Skylion007
Copy link
Collaborator

@Skylion007 Skylion007 commented Sep 9, 2021

Description

  • Ensure we don't use const return types since they are useless and can prevent useful optimizations (const references are allowed to be returned though).
  • Fix some bad templating code that allowed
  • The annoying edge case to account for here is that our Eigen cast actually has a different cast depending on whether the Eigen array is const (it controls the writability flag). For that reason, we cannot ban const return types from the caster in case the user does similarly off spec things. I have however added the NOLINTS and ensured that all other errant const return types (such as const handles) have been removed.
  • Note: remove_cv_t will not strip the constness of references :)

Suggested changelog entry:

* Enable readability clang-tidy-const-return and remove useless consts

@Skylion007 Skylion007 requested a review from henryiii as a code owner September 9, 2021 15:18
@Skylion007 Skylion007 changed the title Clang tidy const return maint(Clang-Tidy):readability-const-return Sep 9, 2021
@Skylion007 Skylion007 changed the title maint(Clang-Tidy):readability-const-return maint(Clang-Tidy): readability-const-return Sep 9, 2021
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Clang-Format. I can revert.

@@ -244,6 +245,7 @@ TEST_SUBMODULE(eigen, m) {

// test_fixed, and various other tests
m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); });
// NOLINTNEXTLINE(readability-const-return-type)
m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); });
Copy link
Collaborator

Choose a reason for hiding this comment

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

This used to return a non-writable numpy array, now it returns a writable one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unchanged files with check annotations (Beta) - very nice to see!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How annoyingly offspec, Fixing this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a kind-of-handy and "cute" trick, but not at all "normal" C++!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is that trick explained somewhere? (I still don't know what it is.) A one-line comment here with a hint would be nice.

@@ -244,6 +245,7 @@ TEST_SUBMODULE(eigen, m) {

// test_fixed, and various other tests
m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); });
// NOLINTNEXTLINE(readability-const-return-type)
m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is that trick explained somewhere? (I still don't know what it is.) A one-line comment here with a hint would be nice.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

Thanks for the Eigen comment! That makes it totally clear.

@Skylion007 Skylion007 requested a review from henryiii September 9, 2021 18:06
@rwgk
Copy link
Collaborator

rwgk commented Sep 10, 2021

I'll merge this now, to then merge into to smart_holder branch, cleanup there, and then run the Google global testing overnight.

@rwgk rwgk merged commit ae07d4c into pybind:master Sep 10, 2021
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Sep 10, 2021
@Skylion007
Copy link
Collaborator Author

Oh darn, I didn't merge this yes because there was a useless a NOLINT I hadn't got around to deleting.

@rwgk
Copy link
Collaborator

rwgk commented Sep 10, 2021

Oops, sorry. Is that a one-line delete? Where? I could test it via the smart_holder testing, we could simply push directly to master.

@Skylion007
Copy link
Collaborator Author

cast.h 1187

@rwgk
Copy link
Collaborator

rwgk commented Sep 10, 2021

Thanks! I still have everything open (local clang-tidy testing). I'll take care of it.

@rwgk
Copy link
Collaborator

rwgk commented Sep 10, 2021

I'm running into a snag with my grand overnight testing plan. See below.
I'm too tired right now to debug. I'll work on this tomorrow. I'll hold off removing the NOLINT until I understand the error.

ERROR: /google/src/cloud/rwgk/pybind11_sh_update/google3/third_party/pybind11_protobuf/BUILD:9:15: Compiling third_party/pybind11_protobuf/proto_utils.cc failed: (Exit 1) wrapped_clang failed: error executing command third_party/crosstool/v18/stable/toolchain/bin/wrapped_clang '-frandom-seed=blaze-out/k8-asan-fastbuild/bin/third_party/pybind11_protobuf/_objs/proto_utils/proto_utils.pic.o' -DABSL_ALLOCATOR_NOTHROW ... (remaining 260 arguments skipped).  [forge_remote_host=ixdf7]
third_party/pybind11_protobuf/proto_utils.cc:520:16: error: non-const lvalue reference to type 'pair<...>' cannot bind to a temporary of type 'pair<...>'
    for (auto& item : values) SetItem(item.first, item.second);
               ^    ~
third_party/pybind11/include/pybind11/pytypes.h:1470:27: note: selected 'begin' function with iterator type 'detail::dict_iterator' (aka 'generic_iterator<iterator_policies::dict_readonly>')
    detail::dict_iterator begin() const { return {*this, 0}; }
                          ^
third_party/pybind11_protobuf/proto_utils.cc:1072:14: error: non-const lvalue reference to type 'pair<...>' cannot bind to a temporary of type 'pair<...>'
  for (auto& item : kwargs_in) {
             ^    ~
third_party/pybind11/include/pybind11/pytypes.h:1470:27: note: selected 'begin' function with iterator type 'detail::dict_iterator' (aka 'generic_iterator<iterator_policies::dict_readonly>')
    detail::dict_iterator begin() const { return {*this, 0}; }
                          ^
2 errors generated.

@rwgk
Copy link
Collaborator

rwgk commented Sep 10, 2021

Never mind, a quick guess worked: the proto_utils.cc code needs to be changed to const auto& in the two places pointed to in the error message.
Clearly your clang-tidy change is doing something good already for code health beyond pybind11 core :-)

@rwgk rwgk mentioned this pull request Sep 10, 2021
@rwgk
Copy link
Collaborator

rwgk commented Sep 10, 2021

I actually managed to run the Google global testing overnight. There is a pretty high number of failures (hundreds), with estimated 9 root causes. Unfortunately I cannot share the full list publicly, but one root cause points to pytorch. I'll work on this asap and will log my updates here.

rwgk added a commit to rwgk/pybind11 that referenced this pull request Sep 10, 2021
…sts reflective of user code that breaks when those `const` are removed.
rwgk added a commit that referenced this pull request Sep 13, 2021
* Restoring `const` removed from pytypes.h in PR #3254, adding tests reflective of user code that breaks when those `const` are removed.

* clang-tidy NOLINTs (and one collateral fix).

* Inserting PYBIND11_CONST_FOR_STRICT_PLATFORMS

* Trying `defined(__APPLE__)`

* Trying again: `auto it` for strict platforms.

* Adding NOLINTNEXTLINE(bugprone-macro-parentheses), expanding comments.

* Labeling all changes with `PR #3263`, for easy reference, and to make it easy to undo these changes if we decide to do so in the future.
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants