Skip to content

Conversation

@aaronliu0130
Copy link
Member

suppress false positive on direct initialization (int max(0);)

Fixes #296

suppress false positive on direct initialization (`int max(0);`)
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a false positive in the Include What You Use (IWYU) checker by preventing direct initialization syntax (like int max(0);) from being incorrectly flagged as requiring #include <algorithm>. The fix moves copy, max, and min from the general template detection pattern to a specialized pattern that only matches when these functions are:

  1. Explicitly qualified with std:: (e.g., std::max(a, b)), OR
  2. Used with template arguments (e.g., max<int>(a, b))

Key Changes

  • Removes copy, max, and min from the _HEADERS_MAYBE_TEMPLATES tuple to prevent the generic pattern from matching them
  • Adds a specialized regex pattern that only matches fully qualified or explicitly templated uses of these three functions
  • Adds a test case to verify that direct initialization syntax doesn't trigger false positives
  • Minor comment improvement for consistency

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
cpplint.py Removes copy, max, min from generic template list and adds specialized pattern with stricter matching rules to avoid false positives
cpplint_unittest.py Adds test case verifying that direct initialization syntax (int max(0)) doesn't trigger IWYU warnings

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

""",
"Add #include <algorithm> for min [build/include_what_you_use] [4]",
)
self.TestIncludeWhatYouUse("int max(0), copy(max), min();", "")
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

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

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
Member

Choose a reason for hiding this comment

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

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

Copy link
Member Author

@aaronliu0130 aaronliu0130 Nov 27, 2025

Choose a reason for hiding this comment

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

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.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

RSLGTM

@aaronliu0130 aaronliu0130 merged commit 9f4e9df into develop Nov 27, 2025
20 checks passed
@aaronliu0130
Copy link
Member Author

Thanks—had to search up what a RS is :)

Could you also check out #376? That's the one I really want to get over with.

@cclauss cclauss deleted the minmaxxing branch November 27, 2025 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive for min and max

3 participants