-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
awsproviderlint: Add AWSV001 check, switch fmtsprintfcallexpr pass to…
… upstream (#14681) Reference: #14601 Reference: https://github.com/bflad/tfproviderlint/releases/tag/v0.17.0
- Loading branch information
Showing
13 changed files
with
187 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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 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 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,69 @@ | ||
package AWSV001 | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"github.com/bflad/tfproviderlint/helper/astutils" | ||
"github.com/bflad/tfproviderlint/passes/commentignore" | ||
"github.com/bflad/tfproviderlint/passes/helper/validation/stringinslicecallexpr" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
const Doc = `check for validation.StringInSlice() using []string parameter | ||
The AWSV001 analyzer reports when a validation.StringInSlice() call has the | ||
first parameter of a []string, which suggests either that AWS API model | ||
constants are not available or that the usage is prior to the AWS Go SDK adding | ||
functions that return all values for the enumeration type. | ||
If the API model constants are not available, this check can be ignored but it | ||
is recommended to submit an AWS Support case to the AWS service team for adding | ||
the constants. | ||
If the hardcoded strings are AWS Go SDK constants, this check reports when the | ||
first parameter should be switched to the newer ENUM_Values() function. | ||
` | ||
|
||
const analyzerName = "AWSV001" | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: analyzerName, | ||
Doc: Doc, | ||
Requires: []*analysis.Analyzer{ | ||
commentignore.Analyzer, | ||
stringinslicecallexpr.Analyzer, | ||
}, | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
callExprs := pass.ResultOf[stringinslicecallexpr.Analyzer].([]*ast.CallExpr) | ||
commentIgnorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer) | ||
|
||
for _, callExpr := range callExprs { | ||
if commentIgnorer.ShouldIgnore(analyzerName, callExpr) { | ||
continue | ||
} | ||
|
||
if len(callExpr.Args) < 2 { | ||
continue | ||
} | ||
|
||
ast.Inspect(callExpr.Args[0], func(n ast.Node) bool { | ||
compositeLit, ok := n.(*ast.CompositeLit) | ||
|
||
if !ok { | ||
return true | ||
} | ||
|
||
if astutils.IsExprTypeArrayString(compositeLit.Type) { | ||
pass.Reportf(callExpr.Args[0].Pos(), "%s: prefer AWS Go SDK ENUM_Values() function (ignore if not applicable)", analyzerName) | ||
return false | ||
} | ||
|
||
return true | ||
}) | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains 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,12 @@ | ||
package AWSV001 | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestAWSV001(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, Analyzer, "a") | ||
} |
This file contains 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,38 @@ | ||
# AWSV001 | ||
|
||
The `AWSV001` analyzer reports when a `validation.StringInSlice()` call has the first parameter of a `[]string`, which suggests either that AWS API model constants are not available or that the usage is prior to the AWS Go SDK adding functions that return all values for the enumeration type. | ||
|
||
If the API model constants are not available, this check can be ignored but it is recommended to submit an AWS Support case to the AWS service team for adding the constants. | ||
|
||
If the elements of the string slice are AWS Go SDK constants, this check reports when the parameter should be switched to the newer AWS Go SDK `ENUM_Values()` function. | ||
|
||
## Flagged Code | ||
|
||
```go | ||
&schema.Schema{ | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
service.EnumTypeExample1, | ||
service.EnumTypeExample2, | ||
}, false), | ||
} | ||
``` | ||
|
||
## Passing Code | ||
|
||
```go | ||
&schema.Schema{ | ||
ValidateFunc: validation.StringInSlice(service.EnumType_Values(), false), | ||
} | ||
``` | ||
|
||
## Ignoring Check | ||
|
||
The check can be ignored for a certain line via a `//lintignore:AWSV001` comment on the previous line or at the end of the offending line, e.g. | ||
|
||
```go | ||
//lintignore:AWSV001 | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
service.EnumTypeExample1, | ||
service.EnumTypeExample2, | ||
}, false), | ||
``` |
This file contains 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,32 @@ | ||
package a | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func f() { | ||
testSlice := []string{"test"} | ||
|
||
/* Passing cases */ | ||
|
||
_ = validation.StringInSlice(testSlice, false) | ||
|
||
_ = validation.StringInSlice(testSlice, true) | ||
|
||
_ = validation.StringInSlice(testFunc(), false) | ||
|
||
/* Comment ignored cases */ | ||
|
||
//lintignore:AWSV001 | ||
_ = validation.StringInSlice([]string{"test"}, false) | ||
|
||
_ = validation.StringInSlice([]string{"test"}, false) //lintignore:AWSV001 | ||
|
||
/* Failing cases */ | ||
|
||
_ = validation.StringInSlice([]string{"test"}, false) // want "prefer AWS Go SDK ENUM_Values\\(\\) function" | ||
} | ||
|
||
func testFunc() []string { | ||
return []string{"test"} | ||
} |
This file contains 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 @@ | ||
../../../../../../vendor |
This file contains 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
55 changes: 0 additions & 55 deletions
55
awsproviderlint/passes/fmtsprintfcallexpr/fmtsprintfcallexpr.go
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
awsproviderlint/passes/fmtsprintfcallexpr/fmtsprintfcallexpr_test.go
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
...ad/tfproviderlint/passes/helper/validation/stringinslicecallexpr/stringinslicecallexpr.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
...or/github.com/bflad/tfproviderlint/passes/stdlib/fmtsprintfcallexpr/fmtsprintfcallexpr.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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