1 year ago

#75845

test-img

Wojciech

How to avoid duplicate clang-tidy warnings coming from the same header?

Description

Recently I started using clang-tidy in a C++ project and I encountered an annoying problem. When a header contains error and is included in many .cpp files the error is reported multiple times by clang-tidy. Obviously the error is found in different .cpp files, but in fact the error is inside the header and I would like to see just one error message. Is it possible and how could it be done?

BTW: I am aware that a similar question has been asked here. However, the answer is not correct and the problem is not in invoking clang-tidy as a CMake target.

How to reproduce the problem

It is quite easy to reproduce the problem, however it requires some boilerplate code. I am using clang-tidy-12 but I am quite sure that this problem is still up-to-date in version 13 or 14. Consider these files in a single directory:

// A.hpp
#pragma once

void foo(const int);
// A.cpp
#include "A.hpp"

void foo(const int) {}
// main.cpp
#include "A.hpp"

int main() {
    foo(2);
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.13)

project(clang-tidy-test)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(${PROJECT_NAME} main.cpp A.cpp)

Now just build the project:

mkdir build
cd build
cmake ..
cmake --build .

Run clang-tidy inside the build dir: run-clang-tidy-12 -header-filter=.* -checks=-*,readability-avoid-const-params-in-decls.

You can see that the same warning in file A.hpp:3:10 is displayed twice.

c++

duplicates

clang-tidy

0 Answers

Your Answer

Accepted video resources