Zum Inhalt springen

Suppress CodeCharter findings via comments or config

Deliberately exclude individual findings with code comments, or whole rules and paths in the configuration file.

CodeCharter lets you deliberately exclude findings with suppression comments in your code (three forms) for a single line or file, or switch whole rules and paths off in .codecharter/config.yml.

1. Inline Suppression on the Next Line

// codecharter-disable-next-line datetime-direct-usage
var migrationTimestamp = DateTime.UtcNow;

The directive applies to the immediately following line. The rule slug is required: a bare // codecharter-disable-next-line without a slug does not suppress anything.

2. Suppression on the Same Line or the Line Above

var migrationTimestamp = DateTime.UtcNow; // codecharter-disable datetime-direct-usage

Or on the line above:

// codecharter-disable datetime-direct-usage
var migrationTimestamp = DateTime.UtcNow;

Both comment forms accept several rule slugs in one comment, separated by spaces, for example // codecharter-disable datetime-direct-usage magic-number. Slugs are matched case-sensitively against the rule id, which is the rule file name without the .ccr extension.

A free-text reason may follow the rule slug(s), so suppressions can be self-documenting; this works in the codecharter-disable-next-line form as well. The rule list ends at the first delimiter, and everything after it is prose that suppresses nothing — even when it happens to name another rule:

// codecharter-disable magic-number — the retry budget is fixed by the protocol
// codecharter-disable magic-number -- flag-argument would be worse here
// codecharter-disable flag-argument. Legacy code, leave as is.
// codecharter-disable magic-number: matches the wire format

A delimiter is an em dash (), a --, a full stop, a colon, or a closing quote or bracket. Commas and semicolons only separate rule slugs and keep the list open, so // codecharter-disable magic-number, flag-argument — reason suppresses both. Surrounding punctuation is stripped before a token is matched, so // codecharter-disable ("magic-number", "flag-argument") reason works too.

Write the delimiter. Without one, every whitespace-separated token is still read as a rule slug, so a bare reason such as // codecharter-disable magic-number flag-argument is unavoidable here would suppress flag-argument as well.

3. File-Level Suppression

A // codecharter-disable comment that stands alone on its own line, with no rule slug and nothing else on the line, suppresses all findings for the entire file. It can appear anywhere in the file, but placed after code on the same line it has no file-level effect. Useful for auto-generated code or legacy files you don't want to touch right now.

// codecharter-disable
namespace Acme.Generated;

Scope of Inline Suppressions

Inline comments work at two scopes: a single line, and the whole file. For anything wider — a region of code, or a rule across several files — use .codecharter/config.yml: switch the rule off with an ignore entry, or drop the paths with exclude (below).

Repository-Level Suppressions

To switch off a whole rule, or to drop entire paths from analysis, use .codecharter/config.yml instead of inline comments. The ignore section turns findings off (optionally limited to a namespace with in: or to a glob over the entity's reported name with match: — fully qualified for types and members, plain for events and occurrence-level entities), and exclude removes paths from analysis:

version: 1
ignore:
  - rule: namespace-distance
    in: Acme.Generated
  - rule: todo-comment
exclude:
  - "**/tests/**"
  - "**/obj/**"

To change a rule's severity instead of switching it off, use the overrides section; see Findings. For the full set of sections, scopes, and the machine-local overlay, see Configuration file.