Skip to content

Remove IWYU False Positives From Other Namespaces #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

geoffviola
Copy link
Contributor

This PR removes the false positives from _HEADERS_CONTAINING_TEMPLATES that come from non std namespaces. Users of boost or other namespaces will no longer get false positives.

// Warn
set<int> blah1;
std::set<int> blah2;
::std::set<int> blah2;
  set<int> blah1;
  std::set<int> blah2;
  ::std::set<int> blah2;

//  NO WARN
foo.set<int>();
foo->set<int>();

boost::container::set<int> blah3;
blah::std::set<int> blah3;
my_std::set<int> blah3;
my_set<int> blah4;
coolset<int> blah5;
std::blah<int> blah6;
::set<int> blah7;

@geoffviola
Copy link
Contributor Author

Note that there is a regression for the example from #62. This might be OK, because an unqualified map should probably be flagged since it could come from std. Other workarounds include

  1. Only warning if std:: is present. This goes against a previous decision to warn on unqualified names.
  2. Only flag if a comma is present in the template for templates that require multiple template types. The implementation would be a little bit more complicated. If the type name is long, it might go to the next line.
  3. Warn on unqualified map. Users would could use map qualified from other namespaces.

Copy link
Member

@aaronliu0130 aaronliu0130 left a comment

Choose a reason for hiding this comment

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

We shouldn't introduce a regression. Locally defined classes should not be IWYU'd.

@geoffviola
Copy link
Contributor Author

We shouldn't introduce a regression. Locally defined classes should not be IWYU'd.

Is there a preference on how we want to handle this? I'd like to have a simple check that is consistent. Right now the checks for set and map are customized. It would be great if they didn't need to be. For example, it's weird that those each have special rules, but unordered_set and unordered_map don't.

The current check for map prevents false positives from other map definitions by using a fully qualified name(i.e. std::map). This check goes against the comment from before.

The problem specifically with the name map is that it can mean applying a function to a functor. The provided example, could be used to initialize a functor, Foo.

template <typename T>
struct Foo {
    T t;
};
template <typename T>
Foo<T> map(T t) {
    return Foo<T>{ t };
}
struct Bar {
};
auto res = map<Bar>();

That example, only has one template; but it would be reasonable to have multiple ones. One could be the functor. The other could be a function.

If we want to go down the route of applying the check against 2+ template arguments, there are a couple of options.

  1. Check for at least one comma in the same line. This check would be sensitive to formatting and nested types.
  2. Check for at least one comma across multiple lines. This check would be robust to formatting, but not nested types.
  3. Parse the angle brackets across multiple lines at the appropriate level. This would be robust to formatting and nested types.

Another set of options would be to look for a declaration with the same name in the same file. If the declaration is present, the check would be ignored. That might be difficult due to the most vexing parse. Also, it would miss declarations in other files.

It seems like all the options have trade offs, but it would be good to have some clarity.

@geoffviola geoffviola force-pushed the remove_iwyu_false_positive_from_other_namespaces branch 2 times, most recently from ba02750 to 0ad09f5 Compare May 22, 2024 16:52
@geoffviola geoffviola force-pushed the remove_iwyu_false_positive_from_other_namespaces branch from 0ad09f5 to 6440ff3 Compare May 22, 2024 16:53
@geoffviola
Copy link
Contributor Author

I made a change such that map still requires that the name to be fully qualified. The comment looked wrong so I updated it. There isn't a regression, but auto foo = map<int, int>(); won't warn.

@geoffviola geoffviola requested a review from aaronliu0130 May 22, 2024 16:56
Copy link
Member

@aaronliu0130 aaronliu0130 left a comment

Choose a reason for hiding this comment

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

Looks great!

@aaronliu0130 aaronliu0130 merged commit 8d8e5fc into cpplint:develop May 22, 2024
@aaronliu0130
Copy link
Member

oops, forgot the changelog part...

@aaronliu0130
Copy link
Member

Anyways, you don't need to force push here. We always squash on merge.

aaronliu0130 added a commit to norab0130/sockpuppetry that referenced this pull request May 23, 2024
Add changelog for cpplint#273, standardize the changelog's name to all-caps, stop vi favortism, and add some PR guidance to CONTRIBUTING.
aaronliu0130 added a commit that referenced this pull request May 23, 2024
Add changelog for #273, standardize the changelog's name to all-caps,
stop vi favortism, and add some PR guidance to CONTRIBUTING.

Co-authored-by: Aaron Liu <aaronliu0130@gmail.com>
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.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy