Skip to content
Open
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
5 changes: 3 additions & 2 deletions cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6720,7 +6720,7 @@ def CheckCasts(filename, clean_lines, linenum, error):
else:
# Check pointer casts for other than string constants
CheckCStyleCast(
filename, clean_lines, linenum, "reinterpret_cast", r"\((\w+\s?\*+\s?)\)", error
filename, clean_lines, linenum, "reinterpret_cast", r"(?<!\))\((\w+\s?\*+\s?)\)", error
Comment thread
coderabbitai[bot] marked this conversation as resolved.
)

# In addition, we look for people taking the address of a cast. This
Expand Down Expand Up @@ -6782,6 +6782,7 @@ def CheckCasts(filename, clean_lines, linenum, error):
)


# TODO(aaronliu0130): refactor to avoid running checks already run through previous call of this
def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
"""Checks for a C-style cast by looking for the pattern.

Expand Down Expand Up @@ -6823,7 +6824,7 @@ def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
# A single unnamed argument for a function tends to look like old style cast.
# If we see those, don't issue warnings for deprecated casts.
remainder = line[match.end(0) :]
if re.match(r"^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)", remainder):
if re.match(r"^\s*(?:;|(?:const|throw|final|override)\b|[=>{),]|->)", remainder):
return False

# At this point, all that should be left is actual casts.
Expand Down
8 changes: 6 additions & 2 deletions cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,18 +1050,21 @@ def testDeprecatedCast(self):
"Use static_cast<int>(...) instead"
" [readability/casting] [4]",
)

self.TestLint(
'(char *) "foo"',
"Using C-style cast. Use const_cast<char *>(...) instead [readability/casting] [4]",
)

self.TestLint(
"(int*)foo",
"Using C-style cast. "
"Use reinterpret_cast<int*>(...) instead"
" [readability/casting] [4]",
)
self.TestLint(
"(Type**) noexcept(f())",
"Using C-style cast. "
"Use reinterpret_cast<Type**>(...) instead [readability/casting] [4]",
)

# Checks for false positives...
self.TestLint("int a = int();", "") # constructor
Expand Down Expand Up @@ -1107,6 +1110,7 @@ def testDeprecatedCast(self):
self.TestLint("void Function(bool(FunctionPointerArg)()) {}", "")
self.TestLint("typedef set<int64_t, bool(*)(int64_t, int64_t)> SortedIdSet", "")
self.TestLint("bool TraverseNode(T *Node, bool(VisitorBase:: *traverse) (T *t)) {}", "")
self.TestLint("void (*execute_)(operation_base*) noexcept(may_throw());", "")
Comment thread
aaronliu0130 marked this conversation as resolved.

# The second parameter to a gMock method definition is a function signature
# that often looks like a bad cast but should not picked up by lint.
Expand Down