Skip to content

Clang does not warn for unused variables in included files, but only main file #143190

Open
@kayhayen

Description

@kayhayen

Hello,

I have tried to get a warning about an unused variable in included code. I drilled it down to this, which was created with -E from an example:

# 1 "test.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 360 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "test.c" 2

static int x_top = 1;

# 1 "included.c" 1
static int x_middle = 1;
# 5 "test.c" 2

static int x_bottom = 1;

Compiling with clang-21 and older gives:

gcc -o test.o -c -I/usr/include/python3.11/ -I./nuitka/build/include/nuitka -I./nuitka/build/include/ -IAssertsTest.build -Wall -Wunused-variable -Werror -Wextra preprocessed.c

test.c:2:12: error: unused variable 'x_top' [-Werror,-Wunused-variable]
static int x_top = 1;
           ^
test.c:6:12: error: unused variable 'x_bottom' [-Werror,-Wunused-variable]
static int x_bottom = 1;

Which was very unexpected for me. Another C compiler gave warnings for all 3 variables, including x_middle.

Using a header file "included.h" doesn't change this. I just had a theory that maybe .h files are treated differently, it is applied for .c files as well, which is a bit of a problem due to how I use includes in my project Nuitka.

I didn't succeed in finding if this is expected behavior or not. I didn't succeed in any way with options like -isystem --Wsystem-header, not with warning mapping files, etc. it appears to me that warnings are only given for the main file. Is that itentional?

I have seen it with

clang --version
Ubuntu clang version 21.0.0 (++20250529012635+c474f8f2404d-1~exp1~20250529132841.938)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-21/bin
clang --version
Debian clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang-19 --version
Debian clang version 19.1.4 (1~deb12u1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-negative

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions