-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: add errchkjson linter #2349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b13842
Add errchkjson linter
breml 78e0e0e
Make report-no-exported configurable
breml a6b6db9
Exclude json encoding functions from errcheck
breml 869c2ff
Add errchkjson linter
breml 0607414
Fix typo, order imports
breml 228fb75
Update errchkjson to v0.2.0
breml b4d995b
Fix typo
breml File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package golinters | ||
|
||
import ( | ||
"github.com/breml/errchkjson" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
) | ||
|
||
func NewErrChkJSONFuncName(linters *config.Linters, | ||
cfg *config.ErrChkJSONSettings, | ||
errcheckCfg *config.ErrcheckSettings, | ||
) *goanalysis.Linter { | ||
a := errchkjson.NewAnalyzer() | ||
|
||
var omitSafe bool | ||
var reportNoExported bool | ||
if cfg != nil { | ||
omitSafe = cfg.OmitSafe | ||
reportNoExported = cfg.ReportNoExported | ||
} | ||
|
||
// Modify errcheck config if this linter is enabled and OmitSafe is false | ||
if isEnabled(linters, a.Name) && !omitSafe { | ||
if errcheckCfg == nil { | ||
errcheckCfg = &config.ErrcheckSettings{} | ||
} | ||
errcheckCfg.ExcludeFunctions = append(errcheckCfg.ExcludeFunctions, | ||
"encoding/json.Marshal", | ||
"encoding/json.MarshalIndent", | ||
"(*encoding/json.Encoder).Encode", | ||
) | ||
} | ||
|
||
cfgMap := map[string]map[string]interface{}{} | ||
cfgMap[a.Name] = map[string]interface{}{ | ||
"omit-safe": omitSafe, | ||
"report-no-exported": reportNoExported, | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
"errchkjson", | ||
"Checks types passed to the json encoding functions. "+ | ||
"Reports unsupported types and reports occations, where the check for the returned error can be omitted.", | ||
[]*analysis.Analyzer{a}, | ||
cfgMap, | ||
).WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} | ||
|
||
func isEnabled(linters *config.Linters, linterName string) bool { | ||
if linters != nil { | ||
var enabled bool | ||
for _, linter := range linters.Enable { | ||
if linter == linterName { | ||
enabled = true | ||
break | ||
} | ||
} | ||
var disabled bool | ||
for _, linter := range linters.Disable { | ||
if linter == linterName { | ||
disabled = true | ||
break | ||
} | ||
} | ||
var presetEnabled bool | ||
for _, preset := range linters.Presets { | ||
if preset == linter.PresetBugs || preset == linter.PresetUnused { | ||
presetEnabled = true | ||
break | ||
} | ||
} | ||
return enabled || | ||
linters.EnableAll && !disabled || | ||
presetEnabled && !disabled | ||
} | ||
return false | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
issues: | ||
max-issues-per-linter: 100 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
linters-settings: | ||
errchkjson: | ||
report-no-exported: true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
issues: | ||
max-issues-per-linter: 100 | ||
linters-settings: | ||
errchkjson: | ||
omit-safe: true |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I already said, a linter cannot interact with another linter.
The fact to create links between 2 linters means that 1 linter can be a part of the other linter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ldez Thanks for having a look at my PR again, I really appreciate it.
I do not exactly understand what you mean by "cannot". My understanding of "cannot" is, that it is technically not possible. But I don't think this is true, because the implementation of the PR does work as intended.
Or do you mean "cannot" in the sense of "MUST NOT" (or "SHALL NOT") as defined in https://datatracker.ietf.org/doc/html/rfc2119? That is: it is prohibited by guideline (even though, it could technically be possible).
If it is the later, I would like to know, if this guideline is written down somewhere and I would like to better understand the reasoning behind it. I understand, that in some cases the exclude patterns have been used to handle exactly such situations, e.g. for duplicates between gosec and errcheck (see
EXC0008
).pkg/config/issues.go
, but I am not sure how exactly this would work given the fact, that the exclude pattern should only be added under special circumstances (both linters enabled and omitSafe not true).