Bug 438027

Summary: copyable-polymorphic should not issue a warning when the base class copy ctor isn't public
Product: [Developer tools] clazy Reporter: David Faure <faure>
Component: generalAssignee: Unassigned bugs mailing-list <unassigned-bugs>
Status: RESOLVED FIXED    
Severity: normal CC: smartins
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description David Faure 2021-06-03 10:55:00 UTC
SUMMARY
copyable-polymorphic gives unfixable warnings when using a protected copy constructor in the base class, and clone() virtual methods in subclasses. Or final subclasses that can't be derived from. Copying is "safe" in such cases, except when slicing *this internally, but we can't do anything about that.

Well, that would be for a different check: detecting actual slicing at the time it happens... but for library authors, I guess this check still makes sense, if it only warns for *public* copy constructors.

STEPS TO REPRODUCE
1. cd training-material/addon/cpp11/misc/sol-modernize; mkdir build-clazy
2. cmake .. -DCMAKE_CXX_COMPILER=clazy
3. export CLAZY_CHECKS='copyable-polymorphic'
4. make

OBSERVED RESULT

/d/kdab/src/training-material/addon/cpp11/misc/sol-modernize/modernize.cpp:22:1: warning: Polymorphic class Person is copyable. Potential slicing. [-Wclazy-copyable-polymorphic]
class Person
^
/d/kdab/src/training-material/addon/cpp11/misc/sol-modernize/modernize.cpp:46:1: warning: Polymorphic class Employee is copyable. Potential slicing. [-Wclazy-copyable-polymorphic]
class Employee : public Person
^
2 warnings generated.

EXPECTED RESULT

No such warning when the base class' copy constructor is protected

SOFTWARE/OS VERSIONS
clazy git master from today
Comment 1 Sergio Martins 2021-06-03 21:50:21 UTC
Git commit 9ec85f78a18c031e49cdc62517175bfee7ee92ea by Sergio Martins.
Committed on 03/06/2021 at 21:45.
Pushed by smartins into branch 'master'.

copyable-polymorphic: Don't warn for some final classes

If the class is final and its base classes don't have public
copy-ctor/assign then it's ok
Related: bug 430672

M  +28   -4    src/checks/level2/copyable-polymorphic.cpp
M  +23   -0    tests/copyable-polymorphic/main.cpp
M  +1    -0    tests/copyable-polymorphic/main.cpp.expected

https://invent.kde.org/sdk/clazy/commit/9ec85f78a18c031e49cdc62517175bfee7ee92ea