feat: add --fix CLI option to auto-apply clang-format fixes by shenxianpeng · Pull Request #202 · cpp-linter/cpp-linter · GitHub
Skip to content

feat: add --fix CLI option to auto-apply clang-format fixes#202

Draft
shenxianpeng wants to merge 1 commit into
mainfrom
feature/auto-fix
Draft

feat: add --fix CLI option to auto-apply clang-format fixes#202
shenxianpeng wants to merge 1 commit into
mainfrom
feature/auto-fix

Conversation

@shenxianpeng

Copy link
Copy Markdown
Member

Summary

Adds a --fix CLI flag to cpp-linter that runs clang-format -i on files with detected style issues, then clears the format advice so no issues are reported.

This is part 1 of implementing auto-fix for cpp-linter-action (see cpp-linter/cpp-linter-action#439).

Changes

cpp_linter/cli.py

  • Added fix: bool = False attribute to Args class
  • Added --fix / -F CLI argument (store_true)

cpp_linter/clang_tools/__init__.py

  • In capture_clang_tools_output(), after collecting all results, if args.fix is True and format_cmd is available:
    • Iterates over files with format issues
    • Runs clang-format -i on each affected file
    • Clears the format_advice so zero format issues are reported
    • Logs the number of files fixed

tests/test_cli_args.py

  • Added test case for --fix flag parsing

Design Decisions

  • Fixing always operates on the entire file (ignoring lines-changed-only), because clang-format -i cannot target specific line ranges
  • Only clang-format fixes are applied; clang-tidy fixes are explicitly out of scope for now (documented in the CLI help)
  • Files with format issues get their format_advice reset, so the existing comment/review logic sees zero format issues
  • Tidy issues are unaffected and still reported normally

Related

Adds a --fix flag that runs clang-format -i on files with detected
style issues, clearing the format advice so no issues are reported.

Related to cpp-linter/cpp-linter-action#439
@2bndy5

2bndy5 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

@2bndy5

2bndy5 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

@2bndy5 2bndy5 removed the bug Something isn't working label Jun 9, 2026

@2bndy5 2bndy5 Jun 9, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. This patch runs clang-format serially, not in parallel. This will add a significant amount of time for events with a large number of changed files.
  2. This patch ignores the lines-changed-only feature. Formatting the entire file may not be the behavior that users expect.

I think it would be better to handle this inside run_clang_format() because that happens in parallel and the same clang-format args that are already assembled can be reused with -i (which is also done for PR reviews I think).


On a side note, it would be nice to switch from parsing XML to parsing the diff between original source and -i output. But that is a separate issue: #168.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

how to commit changes if checks fail (without using PR review suggestions)

2 participants