Skip to content

Conversation

@2bndy5
Copy link
Contributor

@2bndy5 2bndy5 commented Dec 22, 2025

This fixes a problem about hyperlinking a clang-tidy diagnostic name to the clang-tidy docs.

Specifically when the diagnostic name does not satisfy any of the following conditions:

  • starts with clang-diagnostic-
  • starts with clang-analyzer-
  • contains at least 1 hyphen (-)

Summary by CodeRabbit

  • Bug Fixes
    • Improved diagnostic link handling to gracefully process diagnostics with unexpected formats. When a diagnostic identifier doesn't match the expected structure, the system now returns the raw diagnostic name instead of failing, enhancing overall reliability.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 22, 2025

Walkthrough

The diagnostic_link function in clang_tidy.py is refactored to relax strict assertion handling for clang-analyzer diagnostics by replacing a fixed split with guarded splits, returning the original diagnostic if it lacks expected parts. A corresponding test is added to verify this behavior.

Changes

Cohort / File(s) Summary
diagnostic_link function refactoring
cpp_linter/clang_tools/clang_tidy.py
Modifies diagnostic_link method to use guarded splits (maxsplit=1) instead of strict assertions for clang-analyzer and non-clang-diagnostic paths; returns original diagnostic string if split is unsuccessful, preventing assertion failures on malformed diagnostics
New test for permissive diagnostic handling
tests/test_misc.py
Adds test_diagnostic_link_no_hyphen decorated with @pytest.mark.no_clang to verify TidyNotification.diagnostic_link returns the raw diagnostic name when diagnostic_name lacks a hyphen

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: use diagnostic name as default' directly aligns with the main objective of the PR, which is to use the diagnostic name as a default when it doesn't match other patterns.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch diag-link-err

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
cpp_linter/clang_tools/clang_tidy.py (1)

95-98: LGTM! The defensive check correctly prevents errors.

The fix properly handles diagnostics without hyphens by checking the split result length before formatting. This prevents unpacking errors and gracefully falls back to returning the plain diagnostic name.

One minor edge case: if self.diagnostic is exactly "-", it would split into ['', ''] (length 2) and format an invalid URL //html). However, this scenario is highly unlikely in practice since diagnostic names should be meaningful identifiers.

Optional: Guard against empty parts after split
 diag_split = self.diagnostic.split("-", maxsplit=1)
-if len(diag_split) < 2:
+if len(diag_split) < 2 or not all(diag_split):
     return self.diagnostic
 return link + "{}/{}.html)".format(*diag_split)
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7885064 and ba8e5e4.

📒 Files selected for processing (1)
  • cpp_linter/clang_tools/clang_tidy.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Codacy Static Code Analysis

@github-actions github-actions bot added the bug Something isn't working label Dec 22, 2025
@2bndy5 2bndy5 changed the title fix: default to diagnostic name fix: use diagnostic name as default Dec 22, 2025
@codecov
Copy link

codecov bot commented Dec 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.26%. Comparing base (7e2a47a) to head (a0382a6).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #173   +/-   ##
=======================================
  Coverage   98.26%   98.26%           
=======================================
  Files          23       23           
  Lines        1897     1904    +7     
=======================================
+ Hits         1864     1871    +7     
  Misses         33       33           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@2bndy5

This comment was marked as resolved.

@2bndy5 2bndy5 changed the title fix: use diagnostic name as default fix: skip parsing diagnostic rationale or use diagnostic name as default Dec 23, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
cpp_linter/clang_tools/clang_tidy.py (1)

95-98: Good defensive check for handling diagnostics without hyphens.

The guard correctly prevents attempting to format a documentation link when the diagnostic name lacks a hyphen, returning the raw diagnostic name instead. This aligns well with the PR objective.

💡 Optional: Consider applying similar defensive handling to the clang-analyzer case

For consistency, you could replace the assertion at line 93 with a defensive check similar to this one:

 if self.diagnostic.startswith("clang-analyzer-"):
     check_name_parts = self.diagnostic.split("-", maxsplit=2)
-    assert len(check_name_parts) > 2, "diagnostic name malformed"
-    return link + "clang-analyzer/{}.html)".format(check_name_parts[2])
+    if len(check_name_parts) > 2:
+        return link + "clang-analyzer/{}.html)".format(check_name_parts[2])
+    return self.diagnostic

This would ensure TidyNotification objects constructed directly (outside of parse_tidy_output) with malformed clang-analyzer diagnostics don't trigger assertion failures.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ba8e5e4 and 6151d33.

📒 Files selected for processing (4)
  • cpp_linter/clang_tools/clang_tidy.py
  • cspell.config.yml
  • tests/test_misc.py
  • tests/test_parse_clang_tidy_stdout.py
✅ Files skipped from review due to trivial changes (1)
  • cspell.config.yml
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-10-05T05:49:09.136Z
Learnt from: 2bndy5
Repo: cpp-linter/cpp-linter PR: 124
File: cpp_linter/clang_tools/clang_tidy.py:145-145
Timestamp: 2024-10-05T05:49:09.136Z
Learning: In the `get_suggestions_from_patch` method in `cpp_linter/clang_tools/clang_tidy.py`, it is acceptable to use `assert` statements for type checking when the assertion cannot evaluate as false due to `review_comments` data being passed by reference, and when it's intended exclusively for type checking tools. In such cases, writing unit tests to cover the assertion may be unnecessary.

Applied to files:

  • tests/test_parse_clang_tidy_stdout.py
🧬 Code graph analysis (2)
tests/test_parse_clang_tidy_stdout.py (1)
cpp_linter/clang_tools/clang_tidy.py (1)
  • parse_tidy_output (279-322)
tests/test_misc.py (1)
cpp_linter/clang_tools/clang_tidy.py (1)
  • diagnostic_link (86-98)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (21)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: test (ubuntu-latest, 9)
  • GitHub Check: test (ubuntu-latest, 12)
  • GitHub Check: test (ubuntu-latest, 11)
  • GitHub Check: test (ubuntu-latest, 19)
  • GitHub Check: test (ubuntu-latest, 10)
  • GitHub Check: test (ubuntu-latest, 14)
  • GitHub Check: test (ubuntu-latest, 20)
  • GitHub Check: test (ubuntu-latest, 17)
  • GitHub Check: test (ubuntu-latest, 15)
  • GitHub Check: test (windows-latest, 14)
  • GitHub Check: test (ubuntu-latest, 16)
  • GitHub Check: test (ubuntu-latest, 21)
  • GitHub Check: test (windows-latest, 9)
  • GitHub Check: test (windows-latest, 19)
  • GitHub Check: test (ubuntu-latest, 18)
  • GitHub Check: test (windows-latest, 18)
  • GitHub Check: test (windows-latest, 17)
  • GitHub Check: test (windows-latest, 10)
  • GitHub Check: test (windows-latest, 21)
  • GitHub Check: test (windows-latest, 16)
🔇 Additional comments (3)
tests/test_misc.py (1)

158-171: LGTM! Test correctly validates the defensive fallback.

The test properly verifies that diagnostic_link returns the raw diagnostic name when no hyphen is present. While a diagnostic containing spaces like "no diagnostic name" wouldn't be created by the updated parse_tidy_output logic (which filters for no spaces), the defensive check is still valuable since TidyNotification can be constructed directly in other contexts.

tests/test_parse_clang_tidy_stdout.py (1)

47-55: LGTM! Test provides good validation coverage.

The test correctly validates that parse_tidy_output properly:

  • Parses the 4 diagnostic lines with [clang-diagnostic-error] pattern from TIDY_OUT
  • Filters diagnostic names to ensure no spaces (avoiding false matches like [with auto = ...])
  • Ensures diagnostic names contain hyphens (matching valid clang-tidy diagnostic format)

The assertions align well with the updated parsing logic in cpp_linter/clang_tools/clang_tidy.py.

cpp_linter/clang_tools/clang_tidy.py (1)

296-310: Excellent filtering to prevent false diagnostic matches.

The addition of validation logic correctly handles edge cases where clang-tidy output includes square-bracketed context (e.g., [with auto = typename ...]) that shouldn't be treated as diagnostic notifications. The dual check for no spaces and containing a hyphen effectively filters out these false positives while allowing valid diagnostic names through.

The explanatory comment is helpful for future maintainers.

This fixes a problem about hyperlinking a clang-tidy diagnostic name to the clang-tidy docs.

Specifically when the diagnostic name does not satisfy any of the following conditions:
- starts with `clang-diagnostic-`
- starts with `clang-analyzer-`
- does not contain any `-` (hyphens) at all

And I added an arbitrary test for code coverage.
@2bndy5 2bndy5 changed the title fix: skip parsing diagnostic rationale or use diagnostic name as default fix: use diagnostic name as default Dec 23, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/test_misc.py (1)

158-171: Test correctly validates the no-hyphen case.

The test properly verifies that diagnostic_link returns the diagnostic name unchanged when it contains no hyphens. The logic aligns with the implementation that checks len(diag_split) < 2 after splitting on -.

Consider using a more realistic diagnostic name without spaces (e.g., "nodashes" or "singleword") instead of "no diagnostic name". Real diagnostic names typically don't contain spaces. While the current test works correctly, a more realistic name would better represent potential edge cases.

🔎 Optional refinement
 @pytest.mark.no_clang
 def test_diagnostic_link_no_hyphen() -> None:
     """Test that diagnostic_link returns diagnostic name if no hyphen is present."""
     note = TidyNotification(
         (
             "test.cpp",
             1,
             1,
             "error",
             "Test rationale",
-            "no diagnostic name",
+            "nodashes",
         )
     )
-    assert note.diagnostic_link == "no diagnostic name"
+    assert note.diagnostic_link == "nodashes"
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6151d33 and 1cd0226.

📒 Files selected for processing (2)
  • cpp_linter/clang_tools/clang_tidy.py
  • tests/test_misc.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • cpp_linter/clang_tools/clang_tidy.py
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: 2bndy5
Repo: cpp-linter/cpp-linter PR: 124
File: cpp_linter/clang_tools/clang_tidy.py:145-145
Timestamp: 2024-10-05T05:49:09.136Z
Learning: In the `get_suggestions_from_patch` method in `cpp_linter/clang_tools/clang_tidy.py`, it is acceptable to use `assert` statements for type checking when the assertion cannot evaluate as false due to `review_comments` data being passed by reference, and when it's intended exclusively for type checking tools. In such cases, writing unit tests to cover the assertion may be unnecessary.
🧬 Code graph analysis (1)
tests/test_misc.py (1)
cpp_linter/clang_tools/clang_tidy.py (2)
  • TidyNotification (19-104)
  • diagnostic_link (86-98)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (21)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: docs / build
  • GitHub Check: test (ubuntu-latest, 9)
  • GitHub Check: test (ubuntu-latest, 21)
  • GitHub Check: test (ubuntu-latest, 13)
  • GitHub Check: test (ubuntu-latest, 19)
  • GitHub Check: test (windows-latest, 11)
  • GitHub Check: test (ubuntu-latest, 20)
  • GitHub Check: test (windows-latest, 16)
  • GitHub Check: test (ubuntu-latest, 16)
  • GitHub Check: test (ubuntu-latest, 18)
  • GitHub Check: test (ubuntu-latest, 14)
  • GitHub Check: test (ubuntu-latest, 15)
  • GitHub Check: test (ubuntu-latest, 10)
  • GitHub Check: test (ubuntu-latest, 12)
  • GitHub Check: test (windows-latest, 9)
  • GitHub Check: test (ubuntu-latest, 11)
  • GitHub Check: test (windows-latest, 13)
  • GitHub Check: test (windows-latest, 14)
  • GitHub Check: test (windows-latest, 17)
  • GitHub Check: test (windows-latest, 19)
🔇 Additional comments (1)
tests/test_misc.py (1)

156-157: LGTM!

Standard Python formatting with two blank lines between test functions.

@2bndy5
Copy link
Contributor Author

2bndy5 commented Dec 23, 2025

This is related to cpp-linter/cpp-linter-action#389 but not the solution to the actual cause. See #176 for the actual solution.

2bndy5 added a commit to cpp-linter/cpp-linter-rs that referenced this pull request Dec 23, 2025
Corresponds to cpp-linter/cpp-linter#173. This should never happen in the wild, but just in case there's an underlying problem with parsing clang-tidy output...

This fixes a problem about hyperlinking a clang-tidy diagnostic name to the clang-tidy docs.

Specifically when the diagnostic name does not satisfy the following conditions:
- starts with "clang-analyzer-"
- starts with "clang-diagnostic-"
- contains at least 1 hyphen (`-`)

This also removes the `.unwrap()` calls (bad practice) and adds a test for code coverage.
@2bndy5 2bndy5 merged commit 54e4dd0 into main Dec 25, 2025
43 checks passed
@2bndy5 2bndy5 deleted the diag-link-err branch December 25, 2025 21:42
2bndy5 added a commit to cpp-linter/cpp-linter-rs that referenced this pull request Dec 25, 2025
Corresponds to cpp-linter/cpp-linter#173. This should never happen in
the wild, but just in case there's an underlying problem with parsing
clang-tidy output...

This fixes a problem about hyperlinking a clang-tidy diagnostic name to
the clang-tidy docs.

Specifically when the diagnostic name does not satisfy the following
conditions:
- starts with "clang-analyzer-"
- starts with "clang-diagnostic-"
- contains at least 1 hyphen (`-`)

This also removes the `.unwrap()` calls (bad practice) and adds a test
for code coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants