1// RUN: %check_clang_tidy %s readability-string-compare %t -- -config='{CheckOptions: {readability-string-compare.StringLikeClasses: "CustomStringTemplateBase;CustomStringNonTemplateBase"}}' -- -isystem %clang_tidy_headers
2#include <string>
3
4struct CustomStringNonTemplateBase {
5 int compare(const CustomStringNonTemplateBase& Other) const {
6 return 123; // value is not important for check
7 }
8};
9
10template <typename T>
11struct CustomStringTemplateBase {
12 int compare(const CustomStringTemplateBase& Other) const {
13 return 123;
14 }
15};
16
17struct CustomString1 : CustomStringNonTemplateBase {};
18struct CustomString2 : CustomStringTemplateBase<char> {};
19
20void CustomStringClasses() {
21 std::string_view sv1("a");
22 std::string_view sv2("b");
23 if (sv1.compare(str: sv2)) { // No warning - if a std class is not listed in StringLikeClasses, it won't be checked.
24 }
25
26 CustomString1 custom1;
27 if (custom1.compare(Other: custom1)) {
28 }
29 // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare]
30
31 CustomString2 custom2;
32 if (custom2.compare(Other: custom2)) {
33 }
34 // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare]
35}
36

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang-tools-extra/test/clang-tidy/checkers/readability/string-compare-custom-string-classes.cpp