Skip to content

Commit d6a4890

Browse files
jedudenclaude
andcommitted
Drop defensive error branches in new fix-path code
User feedback: don't write error checks for branches that can't be reached. Mirror the pattern at internal/archetype/gensection/ranges.go:59 (\"NewFile never errors with current implementation\"). - buildPostFixFile: drop the lint.NewFile error return; signature becomes plain *lint.File. Caller no longer needs to handle a parse error after fix. - cachedGitignore: drop the filepath.Abs fallback. On the rare error case Abs returns the input string unchanged, which is still a usable cache key for the inputs the fix pipeline passes (filepath.Dir(path) or f.RootDir). Coverage on the new functions is now 100%. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ee5cb79 commit d6a4890

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

internal/fix/fix.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,19 @@ type Fixer struct {
4545
// creating and caching it on first use. Mirrors engine.Runner so the
4646
// fix path's lint.File values give catalog (and any other rule that
4747
// calls f.GetGitignore()) the same matcher the check path would.
48+
//
49+
// filepath.Abs is allowed to error (it can fail when the process
50+
// can't read its current directory); on that path it returns the
51+
// input string unchanged, which is still a usable cache key — the
52+
// only consequence is that two callers passing the same relative
53+
// path from different working directories would share a cache entry,
54+
// which is acceptable for the fix pipeline that always passes either
55+
// filepath.Dir(path) or f.RootDir.
4856
func (f *Fixer) cachedGitignore(dir string) *lint.GitignoreMatcher {
4957
if f.gitignoreCache == nil {
5058
f.gitignoreCache = make(map[string]*lint.GitignoreMatcher)
5159
}
52-
absDir, err := filepath.Abs(dir)
53-
if err != nil {
54-
absDir = filepath.Clean(dir)
55-
}
60+
absDir, _ := filepath.Abs(dir)
5661
if m, ok := f.gitignoreCache[absDir]; ok {
5762
return m
5863
}
@@ -161,11 +166,7 @@ func (f *Fixer) fixFile(path string) ([]lint.Diagnostic, []lint.Diagnostic, stri
161166
modified = path
162167
}
163168

164-
finalFile, err := buildPostFixFile(path, current, lf, dirFS)
165-
if err != nil {
166-
errs = append(errs, fmt.Errorf("parsing %q after fix: %w", path, err))
167-
return beforeDiags, beforeDiags, modified, errs
168-
}
169+
finalFile := buildPostFixFile(path, current, lf, dirFS)
169170

170171
diags, checkErrs := engine.CheckRules(finalFile, f.Rules, effective)
171172
errs = append(errs, checkErrs...)
@@ -201,13 +202,10 @@ func hydrateLintFile(parsed *lint.File, lf *lint.File, dirFS fs.FS) {
201202
// buildPostFixFile parses post-fix bytes and hydrates them with the
202203
// per-file context from lf so the post-fix CheckRules call sees the
203204
// same lint.File the runner would.
204-
func buildPostFixFile(path string, source []byte, lf *lint.File, dirFS fs.FS) (*lint.File, error) {
205-
finalFile, err := lint.NewFile(path, source)
206-
if err != nil {
207-
return nil, err
208-
}
205+
func buildPostFixFile(path string, source []byte, lf *lint.File, dirFS fs.FS) *lint.File {
206+
finalFile, _ := lint.NewFile(path, source) // NewFile never errors with current implementation
209207
hydrateLintFile(finalFile, lf, dirFS)
210-
return finalFile, nil
208+
return finalFile
211209
}
212210

213211
// applyFixPasses repeatedly applies fixable rules until the content stabilizes.

0 commit comments

Comments
 (0)