From d91670a6c0b6147c473fef6df3c1cdad427c8cfc Mon Sep 17 00:00:00 2001 From: Aaron Liu Date: Wed, 26 Nov 2025 21:01:40 -0500 Subject: [PATCH] fix(IWYU): force std:: or <> for max, min, copy suppress false positive on direct initialization (`int max(0);`) --- cpplint.py | 14 ++++++++++---- cpplint_unittest.py | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cpplint.py b/cpplint.py index 146e798..50c8ab3 100755 --- a/cpplint.py +++ b/cpplint.py @@ -6837,9 +6837,6 @@ def ExpectingFunctionArgs(clean_lines, linenum): ( "", ( - "copy", - "max", - "min", "min_element", "sort", "transform", @@ -6917,11 +6914,20 @@ def ExpectingFunctionArgs(clean_lines, linenum): for _template in _templates ) -# Map is often overloaded. Only check, if it is fully qualified. +# Often overloaded, only check if fully qualified. # Match 'std::map(...)', but not 'map(...)'' _re_pattern_headers_maybe_templates.append( (re.compile(r"(std\b::\bmap\s*\<)|(^(std\b::\b)map\b\(\s*\<)"), "map<>", "") ) +# Otherwise, causes false positives with direct initialization. ('int max(0);') +_re_pattern_headers_maybe_templates.extend( + ( + re.compile(rf"std\b::\b{_template}\s*\([^\)]|\b{_template}\s*<.*?>\([^\)]"), + _template, + "", + ) + for _template in ("copy", "max", "min") +) # Other scripts may reach in and modify this pattern. _re_pattern_templates: list[tuple[re.Pattern, str, str]] = [] diff --git a/cpplint_unittest.py b/cpplint_unittest.py index 574e5fd..a8e3f38 100755 --- a/cpplint_unittest.py +++ b/cpplint_unittest.py @@ -1227,6 +1227,7 @@ def testIncludeWhatYouUse(self): """, "Add #include for min [build/include_what_you_use] [4]", ) + self.TestIncludeWhatYouUse("int max(0), copy(max), min();", "") self.TestIncludeWhatYouUse( 'cout << "hello world" << endl;', "Add #include for cout [build/include_what_you_use] [4]",