-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Description
Consider the following example:
lib.hpp:
#pragma once
#include <string>
namespace tidy
{
class base
{
public:
virtual ~base() = default;
base();
base(const base& other) = default;
base(base&& other) = default;
base& operator=(const base& other) = default;
base& operator=(base&& other) = default;
protected:
void on_construct();
virtual void foo() = 0;
private:
std::string* str = nullptr;
};
}lib.cpp:
#include "lib.hpp"
#include <iostream>
namespace tidy
{
base::base()
{
on_construct();
}
void base::on_construct()
{
// the following check is introduced intentionally, to show that
// clang-tidy actually examines file, finds one issue and skips other
if (str = nullptr)
{
std::cout << "null\n";
}
foo();
}
}test.cpp:
#include "lib.cpp"If I run clang-tidy lib.cpp --header-filter=.*, it finds two issues:
an assignment within an 'if' conditionCall to pure virtual method 'base::foo' during construction has undefined behavior
However if I run clang-tidy test.cpp --header-filter=.*, it finds only one:
an assignment within an 'if' condition
Any hints are highly appreciated.
It does not depend on presence of compilation database (I intentionally skipped it), but for those who care, I add simple cmake file:
CMakeLists.txt:
cmake_minimum_required(VERSION 4.0)
project(tidy)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
#add_library(tidy STATIC lib.cpp)
add_library(tidy STATIC test.cpp)
target_include_directories(tidy PUBLIC ${CMAKE_CURRENT_LIST_DIR})To generate database, use
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -S . -B build
and then append -p build to clang-tidy cmd:
clang-tidy test.cpp --header-filter=.* -p build
I also attach the archive with sample project tidy.zip
clang-tidy version: LLVM version 21.1.4
Best regards.