Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 352236

Summary: [checker] Checker for unintentional converting constructors in C++
Product: [Tools] CDT Reporter: Tomasz Wesolowski <kosashi>
Component: cdt-coreAssignee: Project Inbox <cdt-core-inbox>
Status: NEW --- QA Contact: Jonah Graham <jonah>
Severity: normal    
Priority: P3 CC: eclipse.sprigogin, elaskavaia.cdt, malaperle, yevshif
Version: 8.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
working checker (w.i.p.)
none
examples none

Description Tomasz Wesolowski CLA 2011-07-15 13:01:49 EDT
A little checker which I plan to do soon:

IIRC it's good style to mark one-parameter constructors as explicit unless the programmer intends them to be used as converting constructors.

Example:
struct X {
    X(int a) { } // warning: converting constructor
};
Proposed quick fixes:
- change to explicit X(int a)
- mark with comment: /* implicit */ X(int a) or something similar (same comment-based solution as with the case fall through checker).
Comment 1 Tomasz Wesolowski CLA 2011-07-20 06:07:23 EDT
Created attachment 199968 [details]
working checker (w.i.p.)

Stil todo:
- tests
- strings
- add a preference for comment pattern
- quick fixes
Comment 2 Tomasz Wesolowski CLA 2011-07-20 06:17:13 EDT
Created attachment 199969 [details]
examples

Attaching some code to discuss the behaviour on (i'm going to base the tests on that).

Issues:
1) `explicit` on a copy constructor - this has its purpose (block implicit copying via function calls etc), but is used rarely. Either a Codan info marker or no marker at all - which?

2) `explicit` on a move constructor (c++0x) - i'm not familiar with any use case for that, and it breaks compatibility with some STL (http://bit.ly/oCPJt2), I recommend a warning.

3) Categories. I've used Programming Style for most, and Programming Problems for `explicit` on a move constructor (see (2)). Is that OK? Also maybe it would make sense to introduce a new sub-category under Programming Style for those problems? There's a couple of them and they're fine-grained and related.

4) What should be the default magic comment to indicate that converting constructor is intentional? I've suggested "implicit" as opposed to the `explicit` keyword, that is one option.