Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ linters-settings:
# Settings passed to gocritic.
# The settings key is the name of a supported gocritic checker.
# The list of supported checkers can be find in https://go-critic.github.io/overview.
settings:
settings:
captLocal: # must be valid enabled check name
# whether to restrict checker to params only (default true)
paramsOnly: true
paramsOnly: true
elseif:
# whether to skip balanced if-else pairs (default true)
skipBalanced: true
Expand Down Expand Up @@ -219,13 +219,13 @@ linters-settings:
#
# {{ AUTHOR }} {{ COMPANY }} {{ YEAR }}
# SPDX-License-Identifier: Apache-2.0

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -321,6 +321,11 @@ linters-settings:
simple: true
range-loops: true # Report preallocation suggestions on range loops, true by default
for-loops: false # Report preallocation suggestions on for loops, false by default
predeclared:
# comma-separated list of predeclared identifiers to not report on
ignore: ""
# include method names and field names (i.e., qualified names) in checks
q: false
nolintlint:
# Enable to ensure that nolint directives are all used. Default is true.
allow-unused: false
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
github.com/moricho/tparallel v0.2.1
github.com/nakabonne/nestif v0.3.0
github.com/nishanths/exhaustive v0.1.0
github.com/nishanths/predeclared v0.2.1
github.com/pkg/errors v0.9.1
github.com/polyfloyd/go-errorlint v0.0.0-20201127212506-19bd8db6546f
github.com/ryancurrah/gomodguard v1.2.0
Expand All @@ -69,7 +70,7 @@ require (
github.com/valyala/quicktemplate v1.6.3
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/tools v0.0.0-20201121010211-780cb80bd7fb
golang.org/x/tools v0.0.0-20210101214203-2dba1e4ea05c
gopkg.in/yaml.v2 v2.4.0
honnef.co/go/tools v0.0.1-2020.1.6
mvdan.cc/gofumpt v0.0.0-20201129102820-5c11c50e9475
Expand Down
6 changes: 6 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ type LintersSettings struct {
Makezero MakezeroSettings
Thelper ThelperSettings
Forbidigo ForbidigoSettings
Predeclared PredeclaredSettings

Custom map[string]CustomLinterSettings
}
Expand Down Expand Up @@ -411,6 +412,11 @@ type ForbidigoSettings struct {
Forbid []string `mapstructure:"forbid"`
}

type PredeclaredSettings struct {
Ignore string `mapstructure:"ignore"`
Qualified bool `mapstructure:"q"`
}

var defaultLintersSettings = LintersSettings{
Lll: LllSettings{
LineLength: 120,
Expand Down Expand Up @@ -471,6 +477,10 @@ var defaultLintersSettings = LintersSettings{
ErrorLint: ErrorLintSettings{
Errorf: true,
},
Predeclared: PredeclaredSettings{
Ignore: "",
Qualified: false,
},
}

type CustomLinterSettings struct {
Expand Down
26 changes: 26 additions & 0 deletions pkg/golinters/predeclared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package golinters

import (
"github.com/nishanths/predeclared/passes/predeclared"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewPredeclared(settings *config.PredeclaredSettings) *goanalysis.Linter {
a := predeclared.Analyzer

var cfg map[string]map[string]interface{}
if settings != nil {
cfg = map[string]map[string]interface{}{
a.Name: {
predeclared.IgnoreFlag: settings.Ignore,
predeclared.QualifiedFlag: settings.Qualified,
},
}
}

return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, cfg).
WithLoadMode(goanalysis.LoadModeSyntax)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var exhaustiveCfg *config.ExhaustiveSettings
var errorlintCfg *config.ErrorLintSettings
var thelperCfg *config.ThelperSettings
var predeclaredCfg *config.PredeclaredSettings
if m.cfg != nil {
govetCfg = &m.cfg.LintersSettings.Govet
testpackageCfg = &m.cfg.LintersSettings.Testpackage
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
thelperCfg = &m.cfg.LintersSettings.Thelper
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
}
const megacheckName = "megacheck"
lcs := []*linter.Config{
Expand Down Expand Up @@ -342,6 +344,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewForbidigo()).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/ashanbrown/forbidigo"),
linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/nishanths/predeclared"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/configs/predeclared.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
predeclared:
ignore: "real,recover"
q: true
27 changes: 27 additions & 0 deletions test/testdata/predeclared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//args: -Epredeclared
package testdata

func hello() {
var real int // ERROR "variable real has same name as predeclared identifier"
a := A{}
copy := Clone(a) // ERROR "variable copy has same name as predeclared identifier"

// suppress any "declared but not used" errors
_ = real
_ = a
_ = copy
}

type A struct {
true bool
foo int
}

func Clone(a A) A {
return A{
true: a.true,
foo: a.foo,
}
}

func recover() {} // ERROR "function recover has same name as predeclared identifier"
28 changes: 28 additions & 0 deletions test/testdata/predeclared_custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//args: -Epredeclared
//config_path: testdata/configs/predeclared.yml
package testdata

func hello() {
var real int
a := A{}
copy := Clone(a) // ERROR "variable copy has same name as predeclared identifier"

// suppress any "declared but not used" errors
_ = real
_ = a
_ = copy
}

type A struct {
true bool // ERROR "field true has same name as predeclared identifier"
foo int
}

func Clone(a A) A {
return A{
true: a.true,
foo: a.foo,
}
}

func recover() {}