From 2ce995d73edaf2c60f7c3988aa38d53d421b7217 Mon Sep 17 00:00:00 2001 From: thk123 Date: Tue, 10 Jan 2017 11:49:29 +0000 Subject: [PATCH] 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. --- CODING_STANDARD | 2 ++ regression/cpp-linter/throw/main.cpp | 30 +++++++++++++++++++++++++++ regression/cpp-linter/throw/test.desc | 12 +++++++++++ scripts/cpplint.py | 15 ++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 regression/cpp-linter/throw/main.cpp create mode 100644 regression/cpp-linter/throw/test.desc diff --git a/CODING_STANDARD b/CODING_STANDARD index 41b9bdd8679..8078ef03d73 100644 --- a/CODING_STANDARD +++ b/CODING_STANDARD @@ -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) diff --git a/regression/cpp-linter/throw/main.cpp b/regression/cpp-linter/throw/main.cpp new file mode 100644 index 00000000000..bc0b301d3e5 --- /dev/null +++ b/regression/cpp-linter/throw/main.cpp @@ -0,0 +1,30 @@ +/*******************************************************************\ + +Module: Lint Examples + +Author: Thomas Kiley, thomas@diffblue.com + +\*******************************************************************/ + +/*******************************************************************\ + +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"); +} diff --git a/regression/cpp-linter/throw/test.desc b/regression/cpp-linter/throw/test.desc new file mode 100644 index 00000000000..ebd3926785e --- /dev/null +++ b/regression/cpp-linter/throw/test.desc @@ -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$ +-- diff --git a/scripts/cpplint.py b/scripts/cpplint.py index e4d7916f6e3..923a664b34d 100644 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -223,6 +223,7 @@ 'readability/nul', 'readability/strings', 'readability/todo', + 'readability/throw', 'readability/utf8', 'readability/function_comment' 'runtime/arrays', @@ -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.