3

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 want to pass it by value. There is no reason I would need to employ references to it, or move it, or what-not. However, clang-tidy doesn't like this. So it complains about my functions with take resource_t's by values, and about my passing them by-value to these functions. It always suggests to me to make the parameters const resource_t&'s, and in some cases to consider passing std::move(my_resource) if that's the only use of my_resource in the function.

Now, these warnings are fine for "heavy" classes - but not for this one. Is there any way - with CLion or for clang-tidy generally - to get clang-tidy to tell apart the "heavy" from the "lightweight" case? Or perhaps to whitelist some specific types?

17
  • 4
    you should provide also the context where you are passing the object. In addition we have no idea what kind_t is. Commented Dec 27, 2021 at 14:38
  • 2
    Maybe its a bit pedantic, but why not pass by const& regardles? It perfectly states the intended use, it won't cost you anything (except maybe a little typing), and you won't have different rules for "light-weight" and "heavy-weight" structs. Also if at a later time during refactoring a "light-weight" struct becomes a "heavy-weight" struct you don't have to go through all your code again. Commented Dec 27, 2021 at 14:48
  • 1
    @Jarod42 • Can the indirection by access be optimized away? Is it required? Is it an implementation detail? Commented Dec 27, 2021 at 14:55
  • 1
    @Jarod42 a reference is not required to be implemented as an indirection. I had filed this problem away as "let the compiler worry about it". Now I'm second guessing if I should be passing by value for small values. Commented Dec 27, 2021 at 15:00
  • 1
    So your class doesn't actually look like you describe. It has a move constructor. The question is misleading. Commented Dec 28, 2021 at 4:36

1 Answer 1

2

Clang tidy has dedicated syntax to silence specific warnings on specific lines. I imagine the warning you're referring to is performance-unnecessary-value-param. You can obviously globally disable that check, but it would leave your code exposed (assuming you trust the tool elsewhere but not in this specific case).

So to disable it locally, say a specific function declaration you have to decorate your code with directives to ignore the warning. For example you can silence checks ending with -performance-unnecessary-value-param for all lines between the BEGIN and END

// NOLINTBEGIN(*-performance-unnecessary-value-param) void foo(resource_t iKnowBetter); // NOLINTEND(*-performance-unnecessary-value-param) 

Source

EDIT: Drawing from the comments, I gather that specific types can also be whitelisted like so:

  1. Add a .clang-tidy configuration file to your project (I use this one for example).
  2. Whitelist your type like so:
     CheckOptions: - key: performance-unnecessary-value-param.AllowedTypes value: resource_t 

With all the above been said note one last thing regarding your specific warning:

The check is only applied to parameters of types that are expensive to copy which means they are not trivially copyable or have a non-trivial copy constructor or destructor.

which means the structure you posted shouldn't trigger this warning, unless there's more to kind_t. In any case tweaking the structure to make sure it adheres to the "trivially copyable" requirement might be the cleaner solution.

Sign up to request clarification or add additional context in comments.

6 Comments

But this doesn't happen in a specific function, it happens in hundreds, or thousands, of uses of resource_t.
The documentation mentions suppressing this warning for specific types (needs to be set up in clang-tidy options).
@n.1.8e9-where's-my-sharem. Correct, I've updated the answer
@einpoklum updated the answer. If you have thousands of uses of the type you might want to consider the warnings clang-tidy is emitting though. As mentioned a trivially copy-able type shouldn't give such a warning. In any case globally muting this warning for your type is in fact possible
@Enerccio The method to disable the warning for a specific type is also mentioned. Read the answer past the EDIT
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.