cppclean thinks forward declaration is not used, but it is.
cppclean master reports:
cppclean_test2.h:4: 'Foo::Bar' not used
for the following code snippet. I think it may be getting tricked by the using namespace declaration into thinking that the Bar forward declaration is not required, when in fact it is.
// Forward declare Foo::Bar
namespace Foo
{
class Bar;
}
// So we can reference Bar without qualification below.
using namespace Foo;
class A
{
// The namespaced forward declaration is required for this line of code.
void f(Bar & bar);
};
This should be fixed now :)
More complex test cases will still fail because I didn't implement the full namespace lookup rules, but at least cppclean should no longer produce incorrect warning for this kind of code.
Thanks, I tried the latest cppclean version and confirmed that the false positive is gone in the simple test code.
However, when I tried the new cppclean on my real header flle, the spurious "forward declaration not used" warning still popped up. I'm not immediately able to determine what the difference between the two cases might be; if I can work out what that is and boil it down into a simple test code, I'll let you know.
Ah, the issue is that the using namespace declaration gets included from a separate file in the real application. So the following two-file test exhibits the same spurious warning:
namespace.h
// This line is required, or else you get "error: expected namespace name"
namespace Foo {}
using namespace Foo;
test.h
// This header file declares 'using namespace Foo' so we can reference
// Bar without qualification below.
#include "namespace.h"
// Forward declare Foo::Bar
namespace Foo
{
class Bar;
}
class A
{
// The namespaced forward declaration is required for this line of code.
void f(Bar & bar);
};
cppclean 0.7 reports:
cppclean_test3.h:3: 'namespace.h' does not need to be #included
cppclean_test3.h:8: 'Foo::Bar' not used
I guess this is what you were referring to with "full namespace lookup rules"...
No "full namespace lookup rules" refers to cases where the using namespace is inside a namespace not in global scope.
I didn't think of this pattern with the using namespace in another file. You really have this pattern in a project ? Using namespace inside headers are already considered bad practice but depending on using namespace from another header seems dangerous to me.
I reopen the issue to see if cppclean can handle this case but it will be hard.
I didn't think of this pattern with the using namespace in another file. You really have this pattern in a project ? Using namespace inside headers are already considered bad practice but depending on using namespace from another header seems dangerous to me.
I 100% agree with you that "using namespace" in header files is a bad idea. We do have it in our project, primarily because of laziness, and other historic reasons. So I wouldn't consider it a high priority to fix, especially if it is tricky to do so.