Skip to content

Commit

Permalink
Metalint for checking against the deprecaetd lint.RegisterLint functi…
Browse files Browse the repository at this point in the history
…on (#775)

* Metalint for checking against the deprecaetd lint.RegisterLint function

* go imports
  • Loading branch information
christopher-henderson authored Dec 3, 2023
1 parent ebf2071 commit 7f6ef92
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 0 deletions.
57 changes: 57 additions & 0 deletions v3/integration/lints/lints/register_lint_deprecated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lints

/*
* ZLint Copyright 2023 Regents of the University of Michigan
*
* 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. See the License for the specific language governing
* permissions and limitations under the License.
*/

import (
"go/ast"

"github.com/zmap/zlint/v3/integration/lints/filters"
"github.com/zmap/zlint/v3/integration/lints/lint"
)

type RegisterLintDeprecated struct{}

func (r *RegisterLintDeprecated) CheckApplies(tree *ast.File, file *lint.File) bool {
return filters.IsALint(file)
}

func (r *RegisterLintDeprecated) Lint(tree *ast.File, file *lint.File) *lint.Result {
var result *lint.Result
visitor := &selectorExprVisitor{fn: func(expr *ast.SelectorExpr, node ast.Node) {
if expr.Sel.Name != "RegisterLint" {
return
}
result = lint.NewResult("lint.RegisterLint is deprecated and should not be used. "+
"Please use the register function specific to your lint classification (I.E. "+
"lint.RegisterCertificateLint for certificate lints and lint.RegisterRevocationListLint for CRL lints).").
AddCodeCitation(node.Pos(), node.End(), file).
SetCitations("https://github.com/zmap/zlint/issues/765")
}}
ast.Walk(visitor, tree)
return result
}

type selectorExprVisitor struct {
fn func(expr *ast.SelectorExpr, node ast.Node)
}

func (v *selectorExprVisitor) Visit(node ast.Node) ast.Visitor {
selectorExpr, ok := node.(*ast.SelectorExpr)
if !ok {
return v
}
v.fn(selectorExpr, node)
return nil
}
36 changes: 36 additions & 0 deletions v3/integration/lints/lints/register_lint_deprecated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lints

import (
"testing"

"github.com/zmap/zlint/v3/integration/lints/lint"
)

func TestRegisterLintDeprecated_Lint(t *testing.T) {

data := []struct {
inputFile string
expectPass bool
}{
{inputFile: "testdata/lint_usesRegisterLint.go", expectPass: false},
{inputFile: "testdata/lint_usesRegisterCertificateLint.go", expectPass: true},
{inputFile: "testdata/lint_usesRegisterProfile.go", expectPass: true},
{inputFile: "testdata/lint_usesRegisterRevocationListLint.go", expectPass: true},
}
l := &RegisterLintDeprecated{}
for _, test := range data {
file := test.inputFile
want := test.expectPass
t.Run(file, func(t *testing.T) {
r, err := lint.RunLintForFile(file, l)
if err != nil {
t.Fatal(err)
}
if want && r != nil {
t.Errorf("got unexepcted error result, %s", r)
} else if !want && r == nil {
t.Errorf("expected failure but got nothing")
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package testdata

import "github.com/zmap/zlint/v3/lint"

/*
* ZLint Copyright 2023 Regents of the University of Michigan
*
* 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. See the License for the specific language governing
* permissions and limitations under the License.
*/

func init() {
lint.RegisterCertificateLint(nil)
}
21 changes: 21 additions & 0 deletions v3/integration/lints/lints/testdata/lint_usesRegisterLint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package testdata

import "github.com/zmap/zlint/v3/lint"

/*
* ZLint Copyright 2023 Regents of the University of Michigan
*
* 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. See the License for the specific language governing
* permissions and limitations under the License.
*/

func init() {
lint.RegisterLint(nil)
}
21 changes: 21 additions & 0 deletions v3/integration/lints/lints/testdata/lint_usesRegisterProfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package testdata

import "github.com/zmap/zlint/v3/lint"

/*
* ZLint Copyright 2023 Regents of the University of Michigan
*
* 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. See the License for the specific language governing
* permissions and limitations under the License.
*/

func init() {
lint.RegisterProfile(lint.Profile{})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package testdata

import "github.com/zmap/zlint/v3/lint"

/*
* ZLint Copyright 2023 Regents of the University of Michigan
*
* 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. See the License for the specific language governing
* permissions and limitations under the License.
*/

func init() {
lint.RegisterRevocationListLint(nil)
}
1 change: 1 addition & 0 deletions v3/integration/lints/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
var linters = []lint.Lint{
&lints.InitFirst{},
&lints.NotCommittingGenTestCerts{},
&lints.RegisterLintDeprecated{},
}

func main() {
Expand Down

0 comments on commit 7f6ef92

Please sign in to comment.