Skip to content

clangd -Wunneeded-internal-declaration false positive diagnostic, doesn't show in clang++ #117000

Open
@syyyr

Description

Hi,
I have this code (sorry it has Qt, I'm not able to reproduce it without it):

#include <QObject>
#include <iostream>

namespace {
void bar()
{
    std::cerr << "Hello from bar\n";
}
}

void foo()
{
    auto x = new QObject;
    QObject::connect(x, &QObject::destroyed, x, [] (const auto& ptr) {
        bar();
    });
    delete x;
}

int main()
{
    foo();
}

This code results in bar() being called and "Hello from bar" being printed. However, when I open this file in my editor, where I use clangd, I get a warning that says:

clang: Function 'bar' is not needed and will not be emitted (-Wunneeded-internal-declaration)

This is wrong, it is needed, because it is called.

  • I do not get this warning, when I run the compilation manually (via clang++).
  • The clangd warning disappears if I remove the unused lambda argument (clang++ does warn about it).

The command I use to compile this program is:

clang++ -Wunneeded-internal-declaration -std=c++20 main.cpp $(pkg-config  --cflags --libs Qt6Core)

My clang version:

$ clang++ --version
clang version 18.1.8
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

$ clangd --version
clangd version 18.1.8
Features: linux
Platform: x86_64-pc-linux-gnu

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions