[PATCH] D140968: [clang-tidy] Add check for passing the result of `std::string::c_str` to `strlen`
Carlos Galvez via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 4 07:33:15 PST 2023
carlosgalvezp added inline comments.
================
Comment at: clang-tools-extra/test/clang-tidy/checkers/readability/strlen-string-cstr.cpp:1
+// RUN: %check_clang_tidy -check-suffix=0 %s readability-strlen-string-cstr %t -- -config="{CheckOptions: [{key: readability-strlen-string-cstr.EnableForDataMethod, value: false}]}"
+// RUN: %check_clang_tidy -check-suffix=1 %s readability-strlen-string-cstr %t -- -config="{CheckOptions: [{key: readability-strlen-string-cstr.EnableForDataMethod, value: true}]}"
----------------
The default case typically doesn't need a suffix.
================
Comment at: clang-tools-extra/test/clang-tidy/checkers/readability/strlen-string-cstr.cpp:2
+// RUN: %check_clang_tidy -check-suffix=0 %s readability-strlen-string-cstr %t -- -config="{CheckOptions: [{key: readability-strlen-string-cstr.EnableForDataMethod, value: false}]}"
+// RUN: %check_clang_tidy -check-suffix=1 %s readability-strlen-string-cstr %t -- -config="{CheckOptions: [{key: readability-strlen-string-cstr.EnableForDataMethod, value: true}]}"
+int strlen(const char *);
----------------
Please give a more descriptive name
================
Comment at: clang-tools-extra/test/clang-tidy/checkers/readability/strlen-string-cstr.cpp:35
+ int length = strlen(str1.c_str());
+ // CHECK-MESSAGES-0: [[@LINE-1]]:16: warning: redundant call to 'strlen' {{.*}}
+ // CHECK-FIXES-0: {{^ }}int length = str1.size();{{$}}
----------------
Tip: you don't need to write duplicate checks, you can simply skip the suffix and it will apply to both RUN lines:
// CHECK-MESSAGES: ...
// CHECK-FIXES: ...
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D140968/new/
https://reviews.llvm.org/D140968
More information about the cfe-commits
mailing list