Skip to content

Commit 975b5a3

Browse files
author
martin
committed
Get the linter to check for assertions and give a deprecated warning.
1 parent 720e0f8 commit 975b5a3

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

CODING_STANDARD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ C++ features:
158158
- Use the auto keyword if and only if one of the following
159159
- The type is explictly repeated on the RHS (e.g. a constructor call)
160160
- Adding the type will increase confusion (e.g. iterators, function pointers)
161+
- Avoid assert, if the condition is an actual invariant, use INVARIANT,
162+
PRECONDITION, POSTCONDITION, CHECK_RETURN, UNREACHABLE or DATA_INVARIANT.
163+
If there are possible reasons why it might fail, throw an exception.
161164

162165
Architecture-specific code:
163166
- Avoid if possible.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Author: Martin Brain, martin.brain@diffblue.com
2+
3+
#include <cassert>
4+
#include <assert.h>
5+
6+
int main(int argc, char **argv)
7+
{
8+
assert(0);
9+
return 0;
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CORE
2+
main.cpp
3+
4+
^main\.cpp:8: assert is deprecated, use INVARIANT instead \[build/deprecated\] \[4\]
5+
^Total errors found: 1$
6+
^SIGNAL=0$

scripts/cpplint.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,6 +4629,27 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
46294629
_ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
46304630

46314631

4632+
def CheckAssert(filename, clean_lines, linenum, error):
4633+
"""Check for uses of assert.
4634+
4635+
Args:
4636+
filename: The name of the current file.
4637+
clean_lines: A CleansedLines instance containing the file.
4638+
linenum: The number of the line to check.
4639+
error: The function to call with any errors found.
4640+
"""
4641+
line = clean_lines.elided[linenum]
4642+
match = Match(r'.*\s+assert\(.*\).*', line)
4643+
if match:
4644+
if Match(r'.*\s+assert\((0|false)\).*', line):
4645+
error(filename, linenum, 'build/deprecated', 4,
4646+
'assert is deprecated, use UNREACHABLE instead')
4647+
else:
4648+
error(filename, linenum, 'build/deprecated', 4,
4649+
'assert is deprecated, use INVARIANT, PRECONDITION, CHECK_RETURN, etc. instead')
4650+
4651+
4652+
46324653
def GetLineWidth(line):
46334654
"""Determines the width of the line in column positions.
46344655
@@ -4853,6 +4874,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
48534874
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
48544875
CheckCheck(filename, clean_lines, linenum, error)
48554876
CheckAltTokens(filename, clean_lines, linenum, error)
4877+
CheckAssert(filename, clean_lines, linenum, error)
48564878
classinfo = nesting_state.InnermostClass()
48574879
if classinfo:
48584880
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)

0 commit comments

Comments
 (0)