FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(IWYU): force std:: or <> for max, min, copy by aaronliu0130 · Pull Request #411 · cpplint/cpplint · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 10 additions & 4 deletions cpplint.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6837,9 +6837,6 @@ def ExpectingFunctionArgs(clean_lines, linenum):
(
"<algorithm>",
(
"copy",
"max",
"min",
"min_element",
"sort",
"transform",
Expand Down Expand Up @@ -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<type>(...)', but not 'map<type>(...)''
_re_pattern_headers_maybe_templates.append(
(re.compile(r"(std\b::\bmap\s*\<)|(^(std\b::\b)map\b\(\s*\<)"), "map<>", "<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,
"<algorithm>",
)
for _template in ("copy", "max", "min")
)

# Other scripts may reach in and modify this pattern.
_re_pattern_templates: list[tuple[re.Pattern, str, str]] = []
Expand Down
1 change: 1 addition & 0 deletions cpplint_unittest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ def testIncludeWhatYouUse(self):
""",
"Add #include <algorithm> for min [build/include_what_you_use] [4]",
)
self.TestIncludeWhatYouUse("int max(0), copy(max), min();", "")

Copilot AI Nov 27, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The test case verifies that false positives are suppressed, but there's no corresponding test to verify that legitimate uses of std::max(), std::min(), or std::copy() still trigger the include warning. Consider adding test cases like:

self.TestIncludeWhatYouUse("int x = std::max(a, b);", 
    "Add #include <algorithm> for max  [build/include_what_you_use] [4]")
self.TestIncludeWhatYouUse("std::copy(src.begin(), src.end(), dst.begin());",
    "Add #include <algorithm> for copy  [build/include_what_you_use] [4]")

This ensures the fix doesn't break detection of actual usage.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

How was this resolved? Do those tests already exist or are they not worth adding?

aaronliu0130 Nov 27, 2025
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Indeed, I didn't think they were worth adding—the max and copy regexes are the same as the already-tested one for min, and just like how regexes for max and copy weren't already there, throughout the unit tests each part of the relevant regex pattern is only tested once. Sorry for resolving without leaving this comment.

self.TestIncludeWhatYouUse(
'cout << "hello world" << endl;',
"Add #include <iostream> for cout [build/include_what_you_use] [4]",
Expand Down

Back | FazBrowse Home | New Git URL