Skip to content

[clang-tidy] HeaderFilterRegex in .clang-tidy is not applied consistently and depends on working directory #118009

@PWVEC

Description

@PWVEC

I've encountered a strange behavior regarding "HeaderFilterRegex" (other options might be affected as well...).
I've tested this with clang-tidy version 19.1.2.
With older versions (e.g. clang-tidy 18.1.2) HeaderFilterRegex works as expected, so this seems to be a new issue.

Given the following setup in a project:

.
└── src
    ├── .clang-tidy
    ├── external.h
    ├── internal.h
    └── main.cpp

With the following file contents:

$ cat src/.clang-tidy
Checks: "-*,modernize-use-trailing-return-type"
HeaderFilterRegex: '.*internal.*'

$ cat src/internal.h
int GetSomeValue();

$ cat src/external.h
int GetSomeLibValue();

$ cat src/main.cpp
#include "internal.h"
#include "external.h"

int GetSomeValue() { return 42; }

int main() {
  return GetSomeValue() + GetSomeLibValue();
}

The expectation is that when clang-tidy is run on "main.cpp", it will report the diagnostics for "internal.h" but not for "external.h".
But this does not work consistently. It seems relevant in which workding directory clang-tidy is invoked.

$ clang-tidy src/main.cpp 2>/dev/null
PATH/TO/PROJ/src/main.cpp:4:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
    4 | int GetSomeValue() { return 42; }
      | ~~~ ^
      | auto               -> int
PATH/TO/PROJ/src/main.cpp:6:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
    6 | int main() {
      | ~~~ ^
      | auto       -> int

No diagnostic is applied, i.e. the "HeaderFilterRegex" is essentially set to ''.
If I run clang-tidy in "src", then this works as expected (internal.h is reported, external.h is not):

cd src/ && clang-tidy main.cpp 2>/dev/null
PATH/TO/PROJ/src/internal.h:1:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
    1 | int GetSomeValue();
      | ~~~ ^
      | auto               -> int
PATH/TO/PROJ/src/main.cpp:4:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
    4 | int GetSomeValue() { return 42; }
      | ~~~ ^
      | auto               -> int
PATH/TO/PROJ/src/main.cpp:6:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
    6 | int main() {
      | ~~~ ^
      | auto       -> int

Notably, if I dump the configuration with "--dump-config" the resulting configuration is identical in both cases and it clearly sets "HeaderFilterRegex" to ".*internal.*".

Metadata

Metadata

Assignees

Type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions