mirror of
https://github.com/Kitware/CMake.git
synced 2026-03-17 09:11:36 -05:00
clang-tidy module: add test for cmStrLen() check
This commit is contained in:
@@ -9,3 +9,5 @@ function(add_run_clang_tidy_test check_name)
|
||||
-P "${CMAKE_CURRENT_SOURCE_DIR}/RunClangTidy.cmake"
|
||||
)
|
||||
endfunction()
|
||||
|
||||
add_run_clang_tidy_test(cmake-use-cmstrlen)
|
||||
|
||||
37
Utilities/ClangTidyModule/Tests/cmake-use-cmstrlen-fixit.cxx
Normal file
37
Utilities/ClangTidyModule/Tests/cmake-use-cmstrlen-fixit.cxx
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <cstring>
|
||||
|
||||
template <size_t N>
|
||||
constexpr size_t cmStrLen(const char (&/*str*/)[N])
|
||||
{
|
||||
return N - 1;
|
||||
}
|
||||
|
||||
namespace ns1 {
|
||||
using std::strlen;
|
||||
}
|
||||
|
||||
namespace ns2 {
|
||||
std::size_t strlen(const char* str)
|
||||
{
|
||||
return std::strlen(str);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// String variable used for calling strlen() on a variable
|
||||
auto s0 = "howdy";
|
||||
|
||||
// Correction needed
|
||||
(void)cmStrLen("Hello");
|
||||
(void)cmStrLen("Goodbye");
|
||||
(void)cmStrLen("Hola");
|
||||
(void)cmStrLen("Bonjour");
|
||||
|
||||
// No correction needed
|
||||
(void)ns2::strlen("Salve");
|
||||
(void)cmStrLen("Konnichiwa");
|
||||
(void)strlen(s0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
4 warnings generated.
|
||||
clang-tidy applied 4 of 4 suggested fixes.
|
||||
@@ -0,0 +1,20 @@
|
||||
cmake-use-cmstrlen.cxx:26:9: warning: use cmStrLen() for string literals [cmake-use-cmstrlen]
|
||||
(void)strlen("Hello");
|
||||
^~~~~~
|
||||
cmStrLen
|
||||
cmake-use-cmstrlen.cxx:26:9: note: FIX-IT applied suggested code changes
|
||||
cmake-use-cmstrlen.cxx:27:9: warning: use cmStrLen() for string literals [cmake-use-cmstrlen]
|
||||
(void)::strlen("Goodbye");
|
||||
^~~~~~~~
|
||||
cmStrLen
|
||||
cmake-use-cmstrlen.cxx:27:9: note: FIX-IT applied suggested code changes
|
||||
cmake-use-cmstrlen.cxx:28:9: warning: use cmStrLen() for string literals [cmake-use-cmstrlen]
|
||||
(void)std::strlen("Hola");
|
||||
^~~~~~~~~~~
|
||||
cmStrLen
|
||||
cmake-use-cmstrlen.cxx:28:9: note: FIX-IT applied suggested code changes
|
||||
cmake-use-cmstrlen.cxx:29:9: warning: use cmStrLen() for string literals [cmake-use-cmstrlen]
|
||||
(void)ns1::strlen("Bonjour");
|
||||
^~~~~~~~~~~
|
||||
cmStrLen
|
||||
cmake-use-cmstrlen.cxx:29:9: note: FIX-IT applied suggested code changes
|
||||
37
Utilities/ClangTidyModule/Tests/cmake-use-cmstrlen.cxx
Normal file
37
Utilities/ClangTidyModule/Tests/cmake-use-cmstrlen.cxx
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <cstring>
|
||||
|
||||
template <size_t N>
|
||||
constexpr size_t cmStrLen(const char (&/*str*/)[N])
|
||||
{
|
||||
return N - 1;
|
||||
}
|
||||
|
||||
namespace ns1 {
|
||||
using std::strlen;
|
||||
}
|
||||
|
||||
namespace ns2 {
|
||||
std::size_t strlen(const char* str)
|
||||
{
|
||||
return std::strlen(str);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// String variable used for calling strlen() on a variable
|
||||
auto s0 = "howdy";
|
||||
|
||||
// Correction needed
|
||||
(void)strlen("Hello");
|
||||
(void)::strlen("Goodbye");
|
||||
(void)std::strlen("Hola");
|
||||
(void)ns1::strlen("Bonjour");
|
||||
|
||||
// No correction needed
|
||||
(void)ns2::strlen("Salve");
|
||||
(void)cmStrLen("Konnichiwa");
|
||||
(void)strlen(s0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user