Skip to content

[clang-tidy] readability-avoid-const-params-in-decls does not work in header files #133193

@tearfur

Description

@tearfur

As the title suggests, readability-avoid-const-params-in-decls does not work in header files.

It can be demonstrated using this CMake project:

readability-avoid-const-params-in-decls.zip

It contains 2 functions:

  • void print(int const): Declared in header, defined in source file.
  • void print2(int const): Declared and defined in source file.

If you try to build it, you'll notice that readability-avoid-const-params-in-decls only warns about print2(), but I expect it to warn about both print() and print2().

P.S. I wish I can demonstrate this on Godbolt, but seems like running clang-tidy with a CMake project is not possible there.

Here are my detailed reproduce steps

Only relevant command output are shown.

In host machine

$ docker run -it fedora:41 bash
$ docker cp readability-avoid-const-params-in-decls/ <CONTAINER>:/root

In Docker container

# cd
# dnf update
# dnf install @c-development cmake git ninja-build
# git clone https://github.com/llvm/llvm-project.git
# cd llvm-project/
# cmake -S llvm -B build -G Ninja -DLLVM_ENABLE_PROJECTS='clang;lld;clang-tools-extra' -DCMAKE_BUILD_TYPE=Release
# cd build/
# cmake --build .
# cmake --install .
# clang --version
clang version 21.0.0git (https://github.com/llvm/llvm-project.git a629b505757a1853e6083290e5d8d7b82f4f4d4a)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
# cd ~/readability-avoid-const-params-in-decls/
# CC=clang CXX=clang++ cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
-- The C compiler identification is Clang 21.0.0
-- The CXX compiler identification is Clang 21.0.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/local/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/local/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for clang-tidy
-- Looking for clang-tidy - found
'/usr/local/bin/clang-tidy' '--version'
LLVM (http://llvm.org/):
  LLVM version 21.0.0git
  Optimized build.
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /root/readability-avoid-const-params-in-decls/build
# cd build/
# cmake --build .
[2/3] Building CXX object CMakeFiles/readability_avoid_const_params_in_decls.dir/main.cpp.o
/root/readability-avoid-const-params-in-decls/main.cpp:5:17: warning: parameter 'i' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitio
ns [readability-avoid-const-params-in-decls]
    5 | void print2(int const i);
      |                 ^~~~~
[3/3] Linking CXX executable readability_avoid_const_params_in_decls

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-tidyquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions