Suggests replacing the call with std::string::size.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang-tools-extra/clang-tidy/readability/StrlenStringCStrCheck.cpp | ||
---|---|---|
2 | Please make it single string. | |
36 | What about std::basic_string_view? Or any class with c_str/data/length/size? | |
66 | Please fix. | |
clang-tools-extra/docs/ReleaseNotes.rst | ||
135 | Please use double back-ticks for language constructs. | |
clang-tools-extra/docs/clang-tidy/checks/readability/strlen-string-cstr.rst | ||
7 | Please make it same as statement in Release Notes. | |
23 | Please fix. | |
clang-tools-extra/test/clang-tidy/checkers/readability/strlen-string-cstr.cpp | ||
118 | Please fix. |
clang-tools-extra/clang-tidy/readability/StrlenStringCStrCheck.h | ||
---|---|---|
24 | The docs say this is "true" by default, but here you are setting it to false. | |
clang-tools-extra/docs/clang-tidy/checks/readability/strlen-string-cstr.rst | ||
25 | Is there a use case for wanting this option? (As opposed to unconditionally warning about data()) The problem is the same and the same fix applies? |
clang-tools-extra/test/clang-tidy/checkers/readability/strlen-string-cstr.cpp | ||
---|---|---|
2 | The default case typically doesn't need a suffix. | |
3 | Please give a more descriptive name | |
36 | 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: ... |
Generlised the check to cover string_view and "string-like" classes
Removed the option of ignoring data(), and generalised the check to suggest
fixes if the result of data() or c_str() method of any class with a public
length() or size() method is passed to strlen.
clang-tools-extra/docs/clang-tidy/checks/readability/strlen-string-cstr.rst | ||
---|---|---|
25 | @carlosgalvezp Removed the option. |
clang-tools-extra/clang-tidy/readability/StrlenStringCStrCheck.h | ||
---|---|---|
2 | Should be 80 characters long. See other checks as example. |
I don't see the appear of this check as its a situation that I doubt ever appears in code bases. If there are open source code bases where this is a known problem can you please provide links to them as well as running the run_clang_tidy script over them to verify the changes are good.
After a look at some projects, it seems like the pattern does arise, but often it's intended (eg a string buffer is set to a large size, a legacy function taking a char* writes to it, and then the string is resized to strlen(foo.c_str())). As such, I'll drop this patch, as it can introduce bugs in these cases.
Should be 80 characters long. See other checks as example.