Repository: cpplint/cpplint

Issue:
Skip C++-specific warnings for C code
For source code files with `.[cChH]` extension, please skip warnings meant for C++ code, so that programmers don't accidentally introduce C++-isms into their C code.

Example: readability/casting and runtime/int should be disabled for C code.

As a workaround, I am disabling these rules regardless of the source language, so that I can run cpplint more reliably across my C, C++, and mixed C/C++ projects.

Discussion:
aaronliu0130: Also note that the VLC test sample conflicts with #319.
cclauss: Rebase needed.
aaronliu0130: Indeed. I feel like it'd be better if we just fast-tracked the next release with 3.8 support and then drop 3.8 support and merge this, though.

cclauss: When this is rebased, the py3.8 tests will pass.
aaronliu0130: wrong button lol. The tests will pass when we rebase after we drop 3.8.
aaronliu0130: Since we did use type annotations after all with the mypy PR, this should now be merge-ready.

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index d22b664..a481c65 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -8,7 +8,8 @@ Changelog
 Yet another overdue... hotfix. Sorry this took so long.
 
 * The false positive for indented function parameters in namespaces was eradicated. (https://github.com/cpplint/cpplint/pull/304)
-* build/include-what-you-use now recognizes c-style headers, such as <stdio.h> for symbols from <cstdio>. (https://github.com/cpplint/cpplint/pull/319)
+* Files that end in ".c", ".C", or ".cu" will now also automatically suppress C++-only categories. Previously, `// NO_LINT_C` was required. (https://github.com/cpplint/cpplint/pull/308)
+* build/include-what-you-use now recognizes c-style headers such as <stdio.h> for symbols from <cstdio>. (https://github.com/cpplint/cpplint/pull/319)
 * Ruff was ran on the project to improve performance and reader comprehension thanks to @cclauss.
 
 2.0 (2024-10-06)
diff --git a/cpplint.py b/cpplint.py
index 241317c..2670796 100755
--- a/cpplint.py
+++ b/cpplint.py
@@ -934,10 +934,6 @@ _SED_FIXUPS = {
     "Missing space after ,": r"s/,\([^ ]\)/, \1/g",
 }
 
-# {str, set(int)}: a map from error categories to sets of linenumbers
-# on which those errors are expected and should be suppressed.
-_error_suppressions: dict[str, set[int]] = {}
-
 # The root directory used for deriving header guard CPP variable.
 # This is set by --root flag.
 _root = None
@@ -1036,7 +1032,9 @@ class ErrorSuppressions:
         self._open_block_suppression = None
 
 
-_error_suppressions = ErrorSuppressions()  # type: ignore[assignment]
+# {str, set(int)}: a map from error categories to sets of linenumbers
+# on which those errors are expected and should be suppressed.
+_error_suppressions = ErrorSuppressions()
 
 
 def ProcessHppHeadersOption(val):
@@ -1166,12 +1164,7 @@ def ParseNolintSuppressions(filename, raw_line, linenum, error):
                     )
 
 
-def ProcessGlobalSuppresions(lines):
-    """Deprecated; use ProcessGlobalSuppressions."""
-    ProcessGlobalSuppressions(lines)
-
-
-def ProcessGlobalSuppressions(lines):
+def ProcessGlobalSuppressions(filename: str, lines: list[str]) -> None:
     """Updates the list of global error suppressions.
 
     Parses any lint directives in the file that have global effect.
@@ -1179,9 +1172,10 @@ def ProcessGlobalSuppressions(lines):
     Args:
       lines: An array of strings, each representing a line of the file, with the
              last element being empty if the file is terminated with a newline.
+      filename: str, the name of the input file.
     """
     for line in lines:
-        if _SEARCH_C_FILE.search(line):
+        if _SEARCH_C_FILE.search(line) or filename.lower().endswith((".c", ".cu")):
             for category in _DEFAULT_C_SUPPRESSED_CATEGORIES:
                 _error_suppressions.AddGlobalSuppression(category)
         if _SEARCH_KERNEL_FILE.search(line):
@@ -7279,7 +7273,7 @@ def ProcessFileData(filename, file_extension, lines, error, extra_check_function
     ResetNolintSuppressions()
 
     CheckForCopyright(filename, lines, error)
-    ProcessGlobalSuppressions(lines)
+    ProcessGlobalSuppressions(filename, lines)
     RemoveMultiLineComments(filename, lines, error)
     clean_lines = CleansedLines(lines)
 
diff --git a/samples/vlc-sample/simple.def b/samples/vlc-sample/simple.def
index 174819e..05de643 100644
--- a/samples/vlc-sample/simple.def
+++ b/samples/vlc-sample/simple.def
@@ -4,7 +4,7 @@ src/*
 Done processing src/libvlc.c
 Done processing src/libvlc.h
 Done processing src/missing.c
-Total errors found: 601
+Total errors found: 599
 
 src/libvlc.c:41:  Found C system header after other header. Should be: libvlc.h, c system, c++ system, other.  [build/include_order] [4]
 src/libvlc.c:47:  Found C system header after other header. Should be: libvlc.h, c system, c++ system, other.  [build/include_order] [4]
@@ -19,7 +19,6 @@ src/libvlc.c:86:  Extra space before ( in function call  [whitespace/parens] [4]
 src/libvlc.c:86:  Extra space before )  [whitespace/parens] [2]
 src/libvlc.c:92:  Extra space after ( in function call  [whitespace/parens] [4]
 src/libvlc.c:93:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
-src/libvlc.c:98:  Using C-style cast.  Use reinterpret_cast<vlc_object_t *>(...) instead  [readability/casting] [4]
 src/libvlc.c:99:  Extra space before )  [whitespace/parens] [2]
 src/libvlc.c:100:  Missing space before ( in if(  [whitespace/parens] [5]
 src/libvlc.c:103:  Extra space before ( in function call  [whitespace/parens] [4]
@@ -74,7 +73,6 @@ src/libvlc.c:219:  Extra space before )  [whitespace/parens] [2]
 src/libvlc.c:220:  Missing space before ( in if(  [whitespace/parens] [5]
 src/libvlc.c:221:  { should almost always be at the end of the previous line  [whitespace/braces] [4]
 src/libvlc.c:222:  Extra space after ( in function call  [whitespace/parens] [4]
-src/libvlc.c:222:  Using C-style cast.  Use static_cast<int>(...) instead  [readability/casting] [4]
 src/libvlc.c:223:  Extra space after ( in function call  [whitespace/parens] [4]
 src/libvlc.c:223:  Extra space before )  [whitespace/parens] [2]
 src/libvlc.c:224:  Extra space after ( in function call  [whitespace/parens] [4]


Test patch:
diff --git a/cpplint_unittest.py b/cpplint_unittest.py
index 502f55a..241c131 100755
--- a/cpplint_unittest.py
+++ b/cpplint_unittest.py
@@ -470,6 +470,17 @@ class TestCpplint(CpplintTestBase):
         # All categories suppressed: (two aliases)
         self.TestLint("long a = (int64_t) 65;  // NOLINT", "")
         self.TestLint("long a = (int64_t) 65;  // NOLINT(*)", "")
+
+        # Linting a C file
+        error_collector = ErrorCollector(self.assertTrue)
+        cpplint.ProcessFileData(
+            "test.c",
+            "c",
+            ["// Copyright 2014 Your Majesty.", "int64_t a = (int64_t) 65;", ""],
+            error_collector,
+        )
+        assert error_collector.Results() == ""
+
         # Malformed NOLINT directive:
         self.TestLint(
             "long a = 65;  // NOLINT(foo)",