Home > Net >  clang-tidy and boost::intrusive_ptr
clang-tidy and boost::intrusive_ptr

Time:11-06

I use boost::intrusive_ptr in my project and have such code:

void foo(boost::intrusive_ptr<MyObject> obj) {
    // do something with obj
}

And I have clang-tidy diagnostic:

Clang-Tidy: The parameter 'obj' is copied for each invocation but only used as a const reference; consider making it a const reference

But boost::intrusive_ptr copying on function invocation is intended usage of it, because it's wrapping a pointer and usually we don't want to add one more level of indirection. There are no such diagnostic for std::shared_ptr which have similar usage.

How to add boost::intrusive_ptr to the list of clang-tidy exceptions for this diagnostic rule to avoid false warnings?

CodePudding user response:

Clang-tidy has a configuration file (.clang-tidy) in which you can specify options for checks. According to https://clang.llvm.org/extra/clang-tidy/checks/performance/unnecessary-value-param.html the offending check has an option to ignore specified types. I think it should be something like

CheckOptions:
    - key: performance-unnecessary-value-param.AllowedTypes
      value: ^boost::intrusive_ptr<.*>$

in the .clang-tidy file. The file path can also be given explicitly with --config-file or the configuration can be given as a string in the --config command line argument. See the documentation for details.

The value field is a semicolon-separated list of regex to match against.

CodePudding user response:

IfSince you don't take shared-ownership and the pointer isn't expected to be null, just pass MyObject& or MyObject const&. It's simpler, more expressive and less tightly coupled. (Now you can use the function e.g. in a unit test without needing the unrelated intrusive_ptr for the occasion).

If it can be null, you can pass intrusive_ptr<MyObject> const&.

  • Related