Skip to content

Adding check for throw formatting #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CODING_STANDARD
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ C++ features:
- We allow to use 3rd-party libraries directly.
No wrapper matching the coding rules is required.
Allowed libraries are: STL.
- When throwing, omit the brackets, i.e. `throw "error"`.
- Error messages should start with a lower case letter.
- Use the auto keyword if and only if one of the following
- The type is explictly repeated on the RHS (e.g. a constructor call)
- Adding the type will increase confusion (e.g. iterators, function pointers)
Expand Down
30 changes: 30 additions & 0 deletions regression/cpp-linter/throw/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************\

Module: Lint Examples

Author: Thomas Kiley, [email protected]

\*******************************************************************/

/*******************************************************************\

Function: fun

Inputs:

Outputs:

Purpose:

\*******************************************************************/

static void fun()
{
throw "a valid error";

throw("too bracketed");
throw ("too bracketed");

throw "Too capitalised";
throw("Too bracketed and capitalised");
}
12 changes: 12 additions & 0 deletions regression/cpp-linter/throw/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CORE
main.cpp

main\.cpp:25: Do not include brackets when throwing an error \[readability/throw\] \[4\]
main\.cpp:26: Extra space before ( in function call \[whitespace/parens\] \[4\]
main\.cpp:26: Do not include brackets when throwing an error \[readability/throw\] \[4\]
main\.cpp:28: First character of throw error message should be lower case \[readability/throw\] \[4\]
main\.cpp:29: Do not include brackets when throwing an error \[readability/throw\] \[4\]
main\.cpp:29: First character of throw error message should be lower case \[readability/throw\] \[4\]
^Total errors found: 6$
^SIGNAL=0$
--
15 changes: 15 additions & 0 deletions scripts/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
'readability/nul',
'readability/strings',
'readability/todo',
'readability/throw',
'readability/utf8',
'readability/function_comment'
'runtime/arrays',
Expand Down Expand Up @@ -5162,6 +5163,20 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
' for more information.')



# Check that throw statements don't include the optional bracket
# We use raw lines as we want to check the contents of the string too
# We require the error message starts with a lower case character
raw_line = clean_lines.raw_lines[linenum]
if(Match(r'^\s*throw', raw_line)):
if(Match(r'^\s*throw\s*\(', raw_line)):
error(filename, linenum, 'readability/throw', 4,
'Do not include brackets when throwing an error')
if(Match(r'\s*throw\s*\(?"[A-Z]', raw_line)):
error(filename, linenum, 'readability/throw', 4,
'First character of throw error message should be lower case')


def CheckGlobalStatic(filename, clean_lines, linenum, error):
"""Check for unsafe global or static objects.

Expand Down