Skip to content

Commit 2ce995d

Browse files
author
thk123
committed
Adding check for throw formatting
Throw statements should not have the bracket and the first character of the error message should be lower case. Added a check to the linter to verify this. Added regression tests to check this.
1 parent ddea715 commit 2ce995d

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

CODING_STANDARD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ C++ features:
122122
- We allow to use 3rd-party libraries directly.
123123
No wrapper matching the coding rules is required.
124124
Allowed libraries are: STL.
125+
- When throwing, omit the brackets, i.e. `throw "error"`.
126+
- Error messages should start with a lower case letter.
125127
- Use the auto keyword if and only if one of the following
126128
- The type is explictly repeated on the RHS (e.g. a constructor call)
127129
- Adding the type will increase confusion (e.g. iterators, function pointers)

regression/cpp-linter/throw/main.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************\
2+
3+
Module: Lint Examples
4+
5+
Author: Thomas Kiley, thomas@diffblue.com
6+
7+
\*******************************************************************/
8+
9+
/*******************************************************************\
10+
11+
Function: fun
12+
13+
Inputs:
14+
15+
Outputs:
16+
17+
Purpose:
18+
19+
\*******************************************************************/
20+
21+
static void fun()
22+
{
23+
throw "a valid error";
24+
25+
throw("too bracketed");
26+
throw ("too bracketed");
27+
28+
throw "Too capitalised";
29+
throw("Too bracketed and capitalised");
30+
}

regression/cpp-linter/throw/test.desc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.cpp
3+
4+
main\.cpp:25: Do not include brackets when throwing an error \[readability/throw\] \[4\]
5+
main\.cpp:26: Extra space before ( in function call \[whitespace/parens\] \[4\]
6+
main\.cpp:26: Do not include brackets when throwing an error \[readability/throw\] \[4\]
7+
main\.cpp:28: First character of throw error message should be lower case \[readability/throw\] \[4\]
8+
main\.cpp:29: Do not include brackets when throwing an error \[readability/throw\] \[4\]
9+
main\.cpp:29: First character of throw error message should be lower case \[readability/throw\] \[4\]
10+
^Total errors found: 6$
11+
^SIGNAL=0$
12+
--

scripts/cpplint.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
'readability/nul',
224224
'readability/strings',
225225
'readability/todo',
226+
'readability/throw',
226227
'readability/utf8',
227228
'readability/function_comment'
228229
'runtime/arrays',
@@ -5162,6 +5163,20 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
51625163
' for more information.')
51635164

51645165

5166+
5167+
# Check that throw statements don't include the optional bracket
5168+
# We use raw lines as we want to check the contents of the string too
5169+
# We require the error message starts with a lower case character
5170+
raw_line = clean_lines.raw_lines[linenum]
5171+
if(Match(r'^\s*throw', raw_line)):
5172+
if(Match(r'^\s*throw\s*\(', raw_line)):
5173+
error(filename, linenum, 'readability/throw', 4,
5174+
'Do not include brackets when throwing an error')
5175+
if(Match(r'\s*throw\s*\(?"[A-Z]', raw_line)):
5176+
error(filename, linenum, 'readability/throw', 4,
5177+
'First character of throw error message should be lower case')
5178+
5179+
51655180
def CheckGlobalStatic(filename, clean_lines, linenum, error):
51665181
"""Check for unsafe global or static objects.
51675182

0 commit comments

Comments
 (0)