Skip to content

[clang-tidy] misc-include-header Not directly included & not directly used issue #107625

Open
@mnorfolk03

Description

@mnorfolk03

The misc-include-header check for clang tidy is not properly identifying which header provides a particular function.

I believe this likely due to the function having an overload defined in different files.

Reproducibility:

// clang_tidy_issue.cpp
#include "clang_tidy_issue.h"
#include <unistd.h>

int main() {
    time("hello", [] { sleep(1); });
}
// clang_tidy_issue.h
#pragma once
#include <chrono>
#include <functional>
#include <string>

inline size_t time(const std::string& prefix, std::function<void()> func) {
    auto thread_local timer_start = std::chrono::high_resolution_clock::now();
    func();
    auto thread_local timer_stop = std::chrono::high_resolution_clock::now();
    const size_t dur = std::chrono::duration_cast<std::chrono::nanoseconds>(timer_stop - timer_start).count();
    printf("[%s] took %ld nanos", prefix.c_str(), dur);
    return dur;
}
$ clang-tidy-18 --checks=misc-include-cleaner clang_tidy_issue.cpp
...
2 warnings generated.
/scratch/mnorfolk/Code/Clion/CppTesting/clang_tidy/clang_tidy_issue.cpp:5:1: warning: included header clang_tidy_issue.h is not used directly [misc-include-cleaner]
    5 | #include "clang_tidy_issue.h"
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    6 | #include <unistd.h>
/scratch/mnorfolk/Code/Clion/CppTesting/clang_tidy/clang_tidy_issue.cpp:9:5: warning: no header providing "time" is directly included [misc-include-cleaner]
    6 | #include <unistd.h>
    7 | 
    8 | int main() {
    9 |     time("hello", [] { sleep(1); });
      |     ^

In this example, I believe it is trying to find the time_t time( time_t *arg ); function defined in <time.h>
https://en.cppreference.com/w/c/chrono/time

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions