stringlint

package module
v0.0.0-...-3f36b2b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

stringlint

A Go analyzer that detects direct string comparison patterns and recommends using github.com/wego/pkg/strings utility functions.

Installation

This follows golangci-lint's "Automatic Way" module plugin flow.

Requirements: Go and git.

  1. Create .custom-gcl.yml in your project:
version: v2.8.0
plugins:
  - module: github.com/wego/pkg/linters/stringlint
    version: v0.1.0
  1. Build custom golangci-lint:
golangci-lint custom
  1. Configure the plugin in .golangci.yml:
version: "2"

linters:
  enable:
    - stringlint
  settings:
    custom:
      stringlint:
        type: "module"
        description: "Enforces wego/pkg/strings usage"
  1. Run the resulting custom binary:
./custom-gcl run ./...
As a standalone tool
go install github.com/wego/pkg/linters/stringlint/cmd/stringlint@latest
stringlint ./...

What it detects

Pattern Suggestion
s == "" wegostrings.IsEmpty(s)
s != "" wegostrings.IsNotEmpty(s)
len(s) == 0 wegostrings.IsEmpty(s)
len(s) != 0 wegostrings.IsNotEmpty(s)
len(s) > 0 wegostrings.IsNotEmpty(s)
len(s) >= 1 wegostrings.IsNotEmpty(s)
len(s) < 1 wegostrings.IsEmpty(s)
len(s) <= 0 wegostrings.IsEmpty(s)
*ptr == "" wegostrings.IsEmptyP(ptr)
*ptr != "" wegostrings.IsNotEmptyP(ptr)

Reversed comparisons with the literal on the left are also detected, for example: 0 == len(s), 0 != len(s), 0 < len(s), 0 >= len(s), 1 <= len(s).

Import Convention

The linter uses the alias wegostrings to avoid conflict with the stdlib strings package:

import wegostrings "github.com/wego/pkg/strings"

// The linter suggests:
if wegostrings.IsEmpty(s) { ... }

Auto-fix

The linter provides suggested fixes that can be applied automatically:

# With golangci-lint
./custom-gcl run --fix ./...

# With standalone tool
stringlint -fix ./...

Note: Auto-fix replaces the comparison but does not add the import statement. You will need to:

  1. Run goimports to add missing imports
  2. Ensure the import uses the wegostrings alias

Development

Local tests

The testdata directory is a standalone module. Run tests from the module root:

go test -v ./...
Using a commit before tagging

If you need to consume an untagged commit from another repo, use a Go pseudo-version instead of a raw SHA.

go list -m -json github.com/wego/pkg/linters/stringlint@<commit>

Then use the returned Version value in .custom-gcl.yml:

version: v2.8.0
plugins:
  - module: github.com/wego/pkg/linters/stringlint
    version: v0.0.0-20260120hhmmss-abcdef123456

Documentation

Overview

Package stringlint provides a Go analyzer that detects direct string comparison patterns and recommends using github.com/wego/pkg/strings utility functions instead.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "stringlint",
	Doc:      "recommends using github.com/wego/pkg/strings functions over direct string comparisons",
	URL:      "https://github.com/wego/pkg/linters/stringlint",
	Run:      run,
	Requires: []*analysis.Analyzer{inspect.Analyzer},
}

Analyzer is the stringlint analyzer that checks for direct string comparisons.

Functions

func New

func New(_ any) (register.LinterPlugin, error)

New is the entry point for the golangci-lint module plugin system. The signature must be: func New(any) (register.LinterPlugin, error).

Types

This section is empty.

Directories

Path Synopsis
cmd
stringlint command
Command stringlint runs the stringlint analyzer.
Command stringlint runs the stringlint analyzer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL