From 3ae81f191137d131b82901a6f697173054405e81 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Mon, 11 Jul 2016 19:00:34 -0700 Subject: [PATCH] Make build/include_what_you_use more consistent `std::string` has some logic to omit the suggestion if it is used in non-STL namespaces, so the same check should be OK to use for other types. Closes #157. --- cpplint/cpplint.py | 9 +++++++-- cpplint/cpplint_unittest.py | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cpplint/cpplint.py b/cpplint/cpplint.py index f4b1113..6201b30 100755 --- a/cpplint/cpplint.py +++ b/cpplint/cpplint.py @@ -5433,8 +5433,13 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, continue for pattern, template, header in _re_pattern_templates: - if pattern.search(line): - required[header] = (linenum, template) + matched = pattern.search(line) + if matched: + # Don't warn about IWYU in non-STL namespaces: + # (We check only the first match per line; good enough.) + prefix = line[:matched.start()] + if prefix.endswith('std::') or not prefix.endswith('::'): + required[header] = (linenum, template) # The policy is that if you #include something in foo.h you don't need to # include it again in foo.cc. Here, we will look at possible includes. diff --git a/cpplint/cpplint_unittest.py b/cpplint/cpplint_unittest.py index 07a443f..fad9013 100755 --- a/cpplint/cpplint_unittest.py +++ b/cpplint/cpplint_unittest.py @@ -949,6 +949,11 @@ class CpplintTest(CpplintTestBase): """, 'Add #include for hash_map<>' ' [build/include_what_you_use] [4]') + self.TestIncludeWhatYouUse( + """#include "base/containers/hash_tables.h" + base::hash_map foobar; + """, + '') self.TestIncludeWhatYouUse( """#include "base/foobar.h" bool foobar = std::less(0,1);