Skip to content
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

Fix cppcoreguidelines-narrowing-conversions warning reported by clang-tidy #1159

Conversation

MaximSmolskiy
Copy link
Contributor

@MaximSmolskiy MaximSmolskiy commented Dec 4, 2022

Fix #1092

@MaximSmolskiy MaximSmolskiy marked this pull request as draft December 4, 2022 21:57
Comment on lines 35 to 36
template <typename IntegerType>
std::string Integer(IntegerType data);
Copy link
Member

Choose a reason for hiding this comment

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

Actually you can implement this template function in the header directly to avoid explicit instantiations.

@MaximSmolskiy MaximSmolskiy marked this pull request as ready for review December 5, 2022 22:04
@MaximSmolskiy MaximSmolskiy requested review from torwig and PragmaTwice and removed request for PragmaTwice and torwig December 6, 2022 18:11
@@ -121,7 +121,7 @@ int StringMatch(const std::string &pattern, const std::string &in, int nocase) {
}

// Glob-style pattern matching.
int StringMatchLen(const char *pattern, int patternLen, const char *string, int stringLen, int nocase) {
int StringMatchLen(const char *pattern, size_t patternLen, const char *string, size_t stringLen, int nocase) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since I noticed it via your PR, you can change the camel case to the snake case here for function arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

src/config/config.h Show resolved Hide resolved
}
Status Set(const std::string &v) override {
int64_t n;
auto s = Util::DecimalStringToNum(v, &n, min_, max_);
Copy link
Member

@PragmaTwice PragmaTwice Dec 10, 2022

Choose a reason for hiding this comment

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

I think you can use ParseInt<uint32_t>(v, {min_, max_}) here.

std::string ToString() override { return std::to_string(*receiver_); }
Status ToNumber(int64_t *n) override {
*n = *receiver_;
return Status::OK();
}
Status Set(const std::string &v) override {
int64_t n;
auto s = Util::DecimalStringToNum(v, &n, min_, max_);
auto s = ParseInt(v, {min_, max_});
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto s = ParseInt(v, {min_, max_});
auto s = ParseInt<IntegerType>(v, {min_, max_});

Copy link
Contributor Author

@MaximSmolskiy MaximSmolskiy Dec 10, 2022

Choose a reason for hiding this comment

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

It can be deduced. Is it better to explicitly pass type?

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 it is better to pass type explicitly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

#ifdef ENABLE_OPENSSL
{"tls-port", true, new IntField(&tls_port, 0, 0, PORT_LIMIT)},
{"tls-port", true, new IntegerField(&tls_port, (uint32_t)0, (uint32_t)0, PORT_LIMIT)},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{"tls-port", true, new IntegerField(&tls_port, (uint32_t)0, (uint32_t)0, PORT_LIMIT)},
{"tls-port", true, new IntegerField<uint32_t>(&tls_port, 0, 0, PORT_LIMIT)},

I prefer we specify template types explicitly instead of deduced from arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In all places (and with int type) or only in few places with types uint32_t and uint64_t?

Copy link
Member

@PragmaTwice PragmaTwice Dec 10, 2022

Choose a reason for hiding this comment

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

I think you can define some aliases, e.g. using IntField = IntergerField<int>, using UInt32Field = IntergerField<uint32_t>.

Then you can use these alias in the config map, so most config kv will remain the original form (IntField).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@MaximSmolskiy MaximSmolskiy requested review from PragmaTwice and removed request for torwig December 10, 2022 16:12
Copy link
Contributor

@torwig torwig left a comment

Choose a reason for hiding this comment

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

LGTM

@git-hulk
Copy link
Member

Thanks all, merging...

@git-hulk git-hulk merged commit e6aa898 into apache:unstable Dec 12, 2022
@MaximSmolskiy MaximSmolskiy deleted the fix-cppcoreguidelines-narrowing-conversions-warning-reported-by-clang-tidy branch December 12, 2022 04:27
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.

Fix cppcoreguidelines-narrowing-conversions warning reported by clang-tidy
4 participants