Skip to content
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

optimize deprecatedComment_checker: delete regexp usage #1188

Merged
merged 3 commits into from Jan 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions checkers/deprecatedComment_checker.go
Expand Up @@ -2,7 +2,6 @@ package checkers

import (
"go/ast"
"regexp"
"strings"

"github.com/go-critic/go-critic/checkers/internal/astwalk"
Expand All @@ -24,12 +23,15 @@ func FuncOld() int`
collection.AddChecker(&info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) {
c := &deprecatedCommentChecker{ctx: ctx}

c.commonPatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)this (?:function|type) is deprecated`),
regexp.MustCompile(`(?i)deprecated[.!]? use \S* instead`),
regexp.MustCompile(`(?i)\[\[deprecated\]\].*`),
regexp.MustCompile(`(?i)note: deprecated\b.*`),
regexp.MustCompile(`(?i)deprecated in.*`),
c.commonPatterns = []string{
"this type is deprecated",
"this function is deprecated",
"[[deprecated]]",
"note: deprecated",
"deprecated in",
"deprecated. use",
"deprecated! use",
"deprecated use",
// TODO(quasilyte): more of these?
}

Expand Down Expand Up @@ -63,7 +65,7 @@ type deprecatedCommentChecker struct {
astwalk.WalkHandler
ctx *linter.CheckerContext

commonPatterns []*regexp.Regexp
commonPatterns []string
commonTypos []string
}

Expand Down Expand Up @@ -114,7 +116,7 @@ func (c *deprecatedCommentChecker) VisitDocComment(doc *ast.CommentGroup) {

// Check for other commonly used patterns.
for _, pat := range c.commonPatterns {
if pat.MatchString(l) {
if strings.HasPrefix(strings.ToLower(l), pat) {
quasilyte marked this conversation as resolved.
Show resolved Hide resolved
c.warnPattern(comment)
return
}
Expand Down