-
-
Notifications
You must be signed in to change notification settings - Fork 686
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
STYLE: Replace (const std::string)
casts with C++17 std::string_view
#4587
STYLE: Replace (const std::string)
casts with C++17 std::string_view
#4587
Conversation
Prevented potential static analysis warnings, like Clang-Tidy: > C-style casts are discouraged; use static_cast [google-readability-casting] And JetBrains ReSharper: > C-style cast is used instead of a C++ cast Note that `std::string_view` is more lightweight than `std::string`.
By the way, I noticed that GoogleTest has a few macro's specifically for string comparison, avoiding the need to manually convert the arguments to |
Make a competing PR using those? 😄 |
/azp run ITK.macOS |
Note that |
2a1265d
into
InsightSoftwareConsortium:master
ITK_TEST_EXPECT_EQUAL((const std::string) "QuadEdgeMeshEulerOperatorDeleteCenterVertexFunction", | ||
(const std::string)deleteCenterVertex->GetNameOfClass()); | ||
ITK_TEST_EXPECT_EQUAL(std::string_view("QuadEdgeMeshEulerOperatorDeleteCenterVertexFunction"), | ||
std::string_view(deleteCenterVertex->GetNameOfClass())); |
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.
Thanks for merging, @hjmjohnson! For the record, none of the recent versions of this test (pull request #4576, #4583, #4587) properly handle the possibility of deleteCenterVertex->GetNameOfClass()
accidentally returning a null. I was hoping that the std::string_view(const char*)
constructor would support a null char pointer as argument, but unfortunately it does not. Neither does the originally used std::string(const char*)
constructor. It's undefined behavior 🤷
If you just want to ensure that this GetNameOfClass()
override has code coverage, you might as well just checks that the returned pointer is not null. For example as follows:
ITK_TEST_EXPECT_TRUE(deleteCenterVertex->GetNameOfClass());
Instead of doing the string comparison check.
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.
Is GetNameOfClass()
ever expected to return a NULL
pointer? So that problem might be moot.
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.
Is
GetNameOfClass()
ever expected to return aNULL
pointer?
No, but unit tests allow us to express what we expect, and what we do not expect 😸
Obviously it's easy for anyone to write a malicious GetNameOfClass()
override that does return NULL. 😸 😸
Prevented potential static analysis warnings, like Clang-Tidy:
And JetBrains ReSharper:
Note that
std::string_view
is more lightweight thanstd::string
.ITK_TEST_EXPECT_EQUAL
to compare class names in QEMesh #4583