From cf4071cf5de83c006b08bf8f62e1450d17a9ce07 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Wed, 22 Feb 2017 12:02:39 -0500 Subject: [PATCH 1/2] Teach the explicit constructor check about constexpr. If a constructor is marked constexpr it evades the explicit constructor check right now, since the check only knows about the inline keyword. Teach it that constexpr can be used also. --- cpplint/cpplint.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpplint/cpplint.py b/cpplint/cpplint.py index 3366b3990..fafc24362 100755 --- a/cpplint/cpplint.py +++ b/cpplint/cpplint.py @@ -2779,7 +2779,8 @@ def CheckForNonStandardConstructs(filename, clean_lines, linenum, # Look for single-argument constructors that aren't marked explicit. # Technically a valid construct, but against style. explicit_constructor_match = Match( - r'\s+(?:inline\s+)?(explicit\s+)?(?:inline\s+)?%s\s*' + r'\s+(?:(?:inline|constexpr)\s+)*(explicit\s+)?' + r'(?:(?:inline|constexpr)\s+)*%s\s*' r'\(((?:[^()]|\([^()]*\))*)\)' % re.escape(base_classname), line) From 2db65fed51cf28fb6c888b0661acffb97c50dcac Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Thu, 2 Mar 2017 13:19:58 -0500 Subject: [PATCH 2/2] Add unit tests for constexpr constructors This also adds unit tests for inline+explicit constructors being okay, with and without constexpr. --- cpplint/cpplint_unittest.py | 74 +++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/cpplint/cpplint_unittest.py b/cpplint/cpplint_unittest.py index b8b831940..308626910 100755 --- a/cpplint/cpplint_unittest.py +++ b/cpplint/cpplint_unittest.py @@ -1316,6 +1316,80 @@ class Foo { };""", 'Single-parameter constructors should be marked explicit.' ' [runtime/explicit] [5]') + # missing explicit for constexpr constructors is bad as well + self.TestMultiLineLint( + """ + class Foo { + constexpr Foo(int f); + };""", + 'Single-parameter constructors should be marked explicit.' + ' [runtime/explicit] [5]') + # missing explicit for constexpr+inline constructors is bad as well + self.TestMultiLineLint( + """ + class Foo { + constexpr inline Foo(int f); + };""", + 'Single-parameter constructors should be marked explicit.' + ' [runtime/explicit] [5]') + self.TestMultiLineLint( + """ + class Foo { + inline constexpr Foo(int f); + };""", + 'Single-parameter constructors should be marked explicit.' + ' [runtime/explicit] [5]') + # explicit with inline is accepted + self.TestMultiLineLint( + """ + class Foo { + inline explicit Foo(int f); + };""", + '') + self.TestMultiLineLint( + """ + class Foo { + explicit inline Foo(int f); + };""", + '') + # explicit with constexpr is accepted + self.TestMultiLineLint( + """ + class Foo { + constexpr explicit Foo(int f); + };""", + '') + self.TestMultiLineLint( + """ + class Foo { + explicit constexpr Foo(int f); + };""", + '') + # explicit with constexpr+inline is accepted + self.TestMultiLineLint( + """ + class Foo { + inline constexpr explicit Foo(int f); + };""", + '') + self.TestMultiLineLint( + """ + class Foo { + explicit inline constexpr Foo(int f); + };""", + '') + self.TestMultiLineLint( + """ + class Foo { + constexpr inline explicit Foo(int f); + };""", + '') + self.TestMultiLineLint( + """ + class Foo { + explicit constexpr inline Foo(int f); + };""", + '') # structs are caught as well. self.TestMultiLineLint( """