Opened 3 years ago

Last modified 5 months ago

#10733 new defect

False positive passedByValue for function pointer with using-declaration

Reported by: ptomato Owned by: noone
Priority: Normal Milestone:
Component: False positive Version:
Keywords: passedByValue simplifyusing Cc:

Description

bug.cpp:

#include <vector>
struct S {
    std::vector<int> v;
    using F = void (*)();
    void func(F f);
};
void S::func(S::F f) {}

Output:

 cppcheck bug.cpp --enable=performance
Checking bug.cpp ...
bug.cpp:7:19: performance: Function parameter 'f' should be passed by const reference. [passedByValue]
void S::func(S::F f) {}
                  ^

F should not be marked as being passed by value, because it is a pointer.

Any of the following changes will make this error disappear:

  • Remove the v member or replace it by a member with a POD type such as int. (But std::deque<int> v still shows the error, for example)
  • Write out the function pointer type instead of a using-declaration.
  • Move the body of func inline in the class declaration.
  • Change the type alias F to a data pointer, such as void*.

Observed with cppcheck 2.6.

Change History (4)

comment:1 by chrchr, 3 years ago

There's also no warning with typedef void (*F)();.

comment:2 by chrchr, 3 years ago

Related: #10252

comment:3 by chrchr, 3 years ago

Keywords: passedByValue simplifyusing added

Variable f doesn't have the pointer flag.

Last edited 20 months ago by chrchr (previous) (diff)

comment:4 by kidkat, 5 months ago

This started with 2.4 and has been bisected to https://github.com/danmar/cppcheck/commit/3c6fae37e439c2b264d1712e5a6d45c6878be276.

It was also previously reported from 1.75 to 1.90.

Note: See TracTickets for help on using tickets.