feat: Add nolint directive support to errstklint#4
Merged
Conversation
Added support for golangci-lint compatible nolint directives to allow excluding specific functions or files from linting. Supported directives: - //nolint:errstklint (function-level) - //nolint:all (all linters) - //nolint:errstklint,other (multiple linters) - //lint:ignore errstklint reason (staticcheck style) - //lint:file-ignore errstklint reason (file-level) Features: - Function-level exclusion with //nolint before function - File-level exclusion with //nolint before package - Compatible with golangci-lint standards - Supports both nolint and lint:ignore formats - No support for line-level nolint (not needed for this linter) Changes: - Added nolint directive parsing to analyzer.go - Added ignoredRange tracking for excluded code - Added comprehensive test cases for all directive types - Updated documentation (README.md, errstklint/README.md) - Added examples of exclude-rules in golangci-lint 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Added support for golangci-lint compatible nolint directives to allow excluding specific functions or files from linting.
Supported Directives
Function-level exclusion
File-level exclusion
Features
//nolintdirective format//lint:ignoreformat//nolint:errstklint,otherImplementation
Changes to analyzer.go
Added directive parsing:
parseNolintDirectives()- Parses comments for nolint/lint:ignore directivesshouldIgnoreLinter()- Checks if errstklint is in the linter listcreateIgnoredRange()- Creates line ranges for ignored codeisPositionIgnored()- Checks if a position falls within ignored rangeRegex patterns:
nolintPattern:^nolint(?::([\w,]+))?(?:\s|$)lintIgnorePattern:^lint:(ignore|file-ignore)\s+(\S+)(?:\s+(.+))?$Integration: Modified
run()function to parse directives and check before reportingTest Coverage
Created comprehensive test cases in
testdata/src/c/:nolint.go - Function-level directives:
//nolint:errstklint//nolint:all//nolint:errstklint,unused//lint:ignore errstklint reasonfile_nolint.go - File-level
//nolint:errstklintfile_lint_ignore.go - File-level
//lint:file-ignore errstklintAll tests pass:
$ go test -v ./errstklint === RUN TestAnalyzer --- PASS: TestAnalyzer (0.13s) === RUN TestAnalyzerWithNolint --- PASS: TestAnalyzerWithNolint (0.02s) === RUN TestShouldExclude --- PASS: TestShouldExclude (0.00s) === RUN TestMatchDoubleStarPattern --- PASS: TestMatchDoubleStarPattern (0.00s) PASSDocumentation Updates
Updated Files
linters.exclusions.rules)golangci-lint Integration
Users can combine nolint directives with golangci-lint exclusion rules:
Note: Exclusion rules are handled by golangci-lint core automatically.
Design Decisions
Why no line-level nolint support?
errstklint detects missing
defer errstk.Wrap(&err)which is a function-level issue, not a line-specific issue. Line-level exclusion doesn't make semantic sense for this linter.Why both nolint and lint:ignore?
//nolint:errstklint- golangci-lint standard (most common)//lint:ignore errstklint- staticcheck standard (requires reason)Examples
Before (no exclusion possible)
After (with nolint)
Related Issues
This PR addresses the need for fine-grained control over linting without modifying source code structure or using file-level exclude patterns.
🤖 Generated with Claude Code