8000 Use static member functions from class instead of instances by aureleoules · Pull Request #25903 · bitcoin/bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use static member functions from class instead of instances #25903

New issue 8000

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

Conversation

aureleoules
Copy link
Contributor

I noticed while reviewing a PR that some static class member functions are accessed by instance instead of class. I believe this makes the code misleading.
This PR enables the clang-tidy check 'readability-static-accessed-through-instance'.

Checks for member expressions that access static members through instances, and replaces them with uses of the appropriate qualified-id.

https://clang.llvm.org/extra/clang-tidy/checks/readability/static-accessed-through-instance.html

I used the -fix option of clang-tidy to discover and fix these issues.

Copy link
Contributor
@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

Approach ACK

@@ -299,7 +299,7 @@ Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) {
unsigned char tmp[Num3072::BYTE_SIZE];

uint256 hashed_in{(HashWriter{} << in).GetSHA256()};
ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, Num3072::BYTE_SIZE);
ChaCha20(hashed_in.data(), uint256::size()).Keystream(tmp, Num3072::BYTE_SIZE);
Copy link
Member

Choose a reason for hiding this comment

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

I think the pattern is generally a.data(), a.size(). If you want to change this, it might be best to use spans instead of the changes here in this pull.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i am unfamiliar with spans, how would that work?

Copy link
Member

Choose a reason for hiding this comment

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

See std::span and our implementation of it Span

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, attempted in b8f7201.
Not sure if I should add this commit in a separate PR.

@DrahtBot
Copy link
Contributor
DrahtBot commented Aug 23, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #26153 (Reduce wasted pseudorandom bytes in ChaCha20 + various improvements by sipa)
  • #25722 (refactor: Use util::Result class for wallet loading by ryanofsky)
  • #15294 (refactor: Extract RipeMd160 by Empact)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@@ -69,7 +69,7 @@ std::string FormatScript(const CScript& script)
ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
break;
}
return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
return ret.substr(0, ret.empty() ? std::string::npos : ret.size() - 1);
Copy link
Member

Choose a reason for hiding this comment

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

personally I prefer ret.npos, or at least think that either is fine and we don't need a linter for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dropped e16f6a3 and rolled back to ret.npos instead of std::string::npos in ff9c66a

Copy link
Member

Choose a reason for hiding this comment

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

I only left a comment on this line, but I meant all other lines as well.

I really don't get the recent rush to enforce meaningless style decisions with clang-tidy, even happily introducing bugs along the way (#25695 (comment)).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I meant I updated it everywhere as well.

Well, it came from a readability issue and so I thought it would be better to enforce it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to drop ff9c66a and keep 6f8c291 if needed

@aureleoules aureleoules force-pushed the 2022-08-tidy-use-static-functions branch from b8f7201 to e9b1c81 Compare August 25, 2022 12:44
This simplifies the usage of various hasher Write functions as there is
no need to pass the buffer size.
@aureleoules aureleoules force-pushed the 2022-08-tidy-use-static-functions branch from e9b1c81 to 6f8c291 Compare August 25, 2022 12:44
@ajtowns
Copy link
Contributor
ajtowns commented Aug 26, 2022

+1 on switching to Span, -1 on switching from the instance to the class for accessing static methods though.

@luke-jr
Copy link
Member
luke-jr commented Aug 26, 2022

I noticed while reviewing a PR that some static class member functions are accessed by instance instead of class. I believe this makes the code misleading.

I don't agree. Sometimes it makes sense to access by instance instead of class.

If there's particular cases that are confusing, those can be fixed, but I don't think adopting this as a hard rule is a good idea.

@aureleoules
Copy link
Contributor Author

Closing because of Concept NACK.

@aureleoules aureleoules deleted the 2022-08-tidy-use-static-functions branch November 2, 2022 10:51
@bitcoin bitcoin locked and limited conversation to collaborators Nov 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0