Questions tagged [clang-tidy]

For questions about clang-tidy as a static analyzer and code modernization tool. For more generic questions such as compiler diagnostic messages use the clang tag.

404 questions
4
votes
1 answer

Which clang-tidy checks provide automated fixes?

I'd like to find out which of the clang-tidy checks are possible to run with the -fix option, i.e. automatically generate fixed code. I know all the modernize-* checks can do this and some other checks can too (like google-readability-casting) but…
pablo285
  • 2,460
  • 4
  • 14
  • 38
3
votes
2 answers

What is the problem with assigning magic numbers to named structure members?

Consider the following code: const double Motor_Kv = 1.1e5; // Motor constant, [rpm/V] const double Motor_R = 2e-2; // Winding resistance, [Ohm] const double Motor_J = 3e-6; // Rotor angular mass, [kg*m2] This has been refactored to use a…
Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53
3
votes
2 answers

Is clang-tidy (version 14) right with `bugprone-use-after-move` error on this code?

struct test { test& foo(std::vector const& v) { assert(v.size() == 1); return *this; } void bar(std::vector v) {} }; void do_test() { std::vector v = { 42 }; return test{}.foo(v).bar(std::move(v)); //…
vladon
  • 8,158
  • 2
  • 47
  • 91
3
votes
1 answer

clang-tidy-10 and compile_commands.json does not support relative paths

There are various posts about this - but I can't seem to find a definitive answer. Some people are saying it works and others not, and yet others saying its tools / IDEs that are the issue. So I made as small an example as I could. Here are the two…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
3
votes
0 answers

Why can't clang tidy detect dead code in lambda function?

Clang tidy can't detect dead code when I use a lambda function; but, if I replace the lambda with a normal function, Clang tidy pin-points the error like the message below: warning GC1A2668F: Value stored to 'ret' is never read…
Gary Kim
  • 31
  • 3
3
votes
0 answers

Why clang-tidy 13 do not ignores system header as 12 was?

I have a C++ code for which I use clang-tidy static analyzer. This code has its own headers and uses external libraries, and is usually compiled thanks to cmake. To make sure clang-tidy check my headers but not the ones from external libraries, I…
R. N
  • 707
  • 11
  • 31
3
votes
2 answers

Potential memory leak if a tuple of a unique pointer is captured in lambda

clang-tidy and scan-build warn about a potential memory leak in this code: #include #include int main() { auto lambda = [tuple = std::make_tuple(std::make_unique(42))] {}; } $ clang-tidy main.cpp -checks="clang*" 1…
Kane
  • 5,595
  • 1
  • 18
  • 27
3
votes
0 answers

Prevent structs from being uninitialized in C++

Here's three files in which the first has a bug: // main.cpp #include #include "lib.h" int main() { Deposit d; // Bug! Needs to be Deposit d = {}; d.dollars = 3; // std::cout << reconfigure_params.version; // We do get an…
theicfire
  • 2,719
  • 2
  • 26
  • 29
3
votes
2 answers

Specifying compile_commands.json file in a .clang-tidy configuration file

I am trying to use clang-tidy for my project, using a .clang-tidy config file. IDE integration tool I used for clang-tidy is telling me that it can't find compile_commands.json file. I figured this is because I use out-of-source builds and I need to…
meguli
  • 1,426
  • 1
  • 18
  • 35
3
votes
0 answers

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…
Wojciech
  • 56
  • 3
3
votes
1 answer

"Parameter is passed by value and only copied once; consider moving it" - avoid this for reference types

I'm writing C++ code in CLion 2021.3, which uses clang-tidy checks. In my code, I have a lightweight reference class; let's say it looks like this: struct resource_t { uint8_t kind; int id; } Now, when I pass a resource_t object around, I…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
1 answer

Warn against missing std:: prefixes due to ADL

It is possible to omit the std:: namespace for s when the argument types are in that namespace, which is usually the case. Is there any warning or clang-tidy rule that finds such omissions? #include #include…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
3
votes
1 answer

How to run `clang-apply-replacements` on change set generated by `clang-tidy --export-fixes`?

Command clang-tidy --export-fixes=fixes.yml generates a file fixes.yml with stances like --- Diagnostics: - DiagnosticName: modernize-loop-convert DiagnosticMessage: Message: use range-based for loop instead …
Joachim W
  • 7,290
  • 5
  • 31
  • 59
3
votes
1 answer

clang-tidy reporting many error: unknown argument

I am running an embedded C project based on a tricore-gcc compiler. I made a compile_commands.json file that works well. However, there are some arguments that clang-tidy cannot recognize. For example I get the following error: unknown argument:…
Barzi2001
  • 989
  • 8
  • 24
3
votes
2 answers

Clang-Tidy: Initializing non-local variable with non-const expression depending on uninitialized non-local variable, C pointer reference

We have a very simple bit of code which is giving a warning but seems like it should be fine, and runs fine. We understand that in general there may be issues with order of initialisation between compilation units, but in this case we are…
jugglingcats
  • 698
  • 9
  • 29