llvm / llvm/llvm-project

[clang-tidy] misc-include-cleaner warnings are not displayed in header file unless explicity checked

Open
#165,551 4 comments 1 reaction 0 assignees View on GitHub
clang-include-cleaner
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Tested with clang-tidy-21

Given this project : https://github.com/blabdouze/misc-include-cleaner-not-reported , clang-tidy doesn't report misc-include-cleaner warnings if clang-tidy is not called directly on the header file (but others warnings works fine).

Other warnings appears when checking main.cpp with header filter set to .* :

```
test> clang-tidy-21 -header-filter=.* -p=build main.cpp
136 warnings generated.
/workspace/build/test/A.hpp:6:26: warning: function 'AFunc' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]
6 | std::vector AFunc() { return std::vector(); }
| ^
/workspace/build/test/A.hpp:6:26: note: mark the definition as 'inline'
6 | std::vector AFunc() { return std::vector(); }
| ^
| inline
/workspace/build/test/B.hpp:5:26: warning: function 'BFunc' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]
5 | std::vector BFunc() { return std::vector(); }
| ^
/workspace/build/test/B.hpp:5:26: note: mark the definition as 'inline'
5 | std::vector BFunc() { return std::vector(); }
| ^
| inline
Suppressed 134 warnings (134 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
```

If run against the header directly, everything works fine :

```
test> clang-tidy-21 -header-filter=.* -p=build B.hpp
139 warnings generated.
/workspace/build/test/A.hpp:6:26: warning: function 'AFunc' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]
6 | std::vector AFunc() { return std::vector(); }
| ^
/workspace/build/test/A.hpp:6:26: note: mark the definition as 'inline'
6 | std::vector AFunc() { return std::vector(); }
| ^
| inline
/workspace/build/test/B.hpp:3:1: warning: included header A.hpp is not used directly [misc-include-cleaner]
3 | #include "A.hpp"
| ^~~~~~~~~~~~~~~~
4 |
/workspace/build/test/B.hpp:5:6: warning: no header providing "std::vector" is directly included [misc-include-cleaner]
3 | #include "A.hpp"
4 |
5 | std::vector BFunc() { return std::vector(); }
| ^
/workspace/build/test/B.hpp:5:18: warning: no header providing "std::string" is directly included [misc-include-cleaner]
3 | #include "A.hpp"
4 |
5 | std::vector BFunc() { return std::vector(); }
| ^
/workspace/build/test/B.hpp:5:26: warning: function 'BFunc' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]
5 | std::vector BFunc() { return std::vector(); }
| ^
/workspace/build/test/B.hpp:5:26: note: mark the definition as 'inline'
5 | std::vector BFunc() { return std::vector(); }
| ^
| inline
Suppressed 134 warnings (134 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
```

Same result with run-clang-tidy-21 :

```
test# run-clang-tidy-21 -header-filter=.* -p=build .
Enabled checks:
misc-confusable-identifiers
misc-const-correctness
misc-coroutine-hostile-raii
misc-definitions-in-headers
misc-header-include-cycle
misc-include-cleaner
misc-misleading-bidirectional
misc-misleading-identifier
misc-misplaced-const
misc-new-delete-overloads
misc-no-recursion
misc-non-copyable-objects
misc-non-private-member-variables-in-classes
misc-redundant-expression
misc-static-assert
misc-throw-by-value-catch-by-reference
misc-unconventional-assign-operator
misc-uniqueptr-reset-release
misc-unused-alias-decls
misc-unused-parameters
misc-unused-using-decls
misc-use-anonymous-namespace
misc-use-internal-linkage

Running clang-tidy for 1 files out of 1 in compilation database ...
[1/1][0.3s] clang-tidy-21 -header-filter=.* -p=build /workspace/build/test/main.cpp
/workspace/build/test/A.hpp:6:26: warning: function 'AFunc' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]
6 | std::vector AFunc() { return std::vector(); }
| ^
/workspace/build/test/A.hpp:6:26: note: mark the definition as 'inline'
6 | std::vector AFunc() { return std::vector(); }
| ^
| inline
/workspace/build/test/B.hpp:5:26: warning: function 'BFunc' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]
5 | std::vector BFunc() { return std::vector(); }
| ^
/workspace/build/test/B.hpp:5:26: note: mark the definition as 'inline'
5 | std::vector BFunc() { return std::vector(); }
| ^
| inline
136 warnings generated.
Suppressed 134 warnings (134 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the report with the linked misc-include-cleaner-not-reported project, comparing clang-tidy-21 on main.cpp with direct checks of A.hpp and B.hpp, and also run the shown run-clang-tidy-21 command. Trace why misc-include-cleaner warnings are absent through the main.cpp path; done means those warnings are displayed when the header is reached through a source file, with the existing header behavior preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.