================ @@ -265,4 +255,63 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss, s.compare(0, 1, "ab") == 0; s.rfind(suffix, 1) == s.size() - suffix.size(); + + #define STR(x) std::string(x) + 0 == STR(s).find("a"); + + #define STRING s + if (0 == STRING.find("ala")) { /* do something */} +} + +void test_substr() { + std::string str("hello world"); + std::string prefix = "hello"; + + // Basic pattern + str.substr(0, 5) == "hello"; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with] + // CHECK-FIXES: str.starts_with("hello"); + + // With string literal on left side + "hello" == str.substr(0, 5); + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with] + // CHECK-FIXES: str.starts_with("hello"); + + // Inequality comparison + str.substr(0, 5) != "world"; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() != [modernize-use-starts-ends-with] + // CHECK-FIXES: !str.starts_with("world"); + + // Ensure non-zero start position is not transformed + str.substr(1, 5) == "hello"; + str.substr(0, 4) == "hello"; // Length mismatch + + size_t len = 5; + str.substr(0, len) == "hello"; // Non-constant length + + // String literal with size calculation + str.substr(0, strlen("hello")) == "hello"; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with] + // CHECK-FIXES: str.starts_with("hello"); + + str.substr(0, prefix.size()) == prefix; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with] + // CHECK-FIXES: str.starts_with(prefix); + + str.substr(0, prefix.length()) == prefix; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with] + // CHECK-FIXES: str.starts_with(prefix); + + // Tests to verify macro behavior ---------------- 5chmidti wrote:
Please add ```c++ #define MSG "hello" str.substr(0, strlen(MSG)) == MSG; // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with] // CHECK-FIXES: str.starts_with(MSG); ``` https://github.com/llvm/llvm-project/pull/116033 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits