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

x/tools/gopls/internal/analysis/modernize: use strings.CutPrefix et al #71369

Open
adonovan opened this issue Jan 21, 2025 · 1 comment
Open
Labels
gopls Issues related to the Go language server, gopls. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Jan 21, 2025

Replace:

if strings.HasPrefix(s, pre) { use(strings.TrimPrefix(s, pre)) }
-> 
if after, ok := strings.CutPrefix(s, pre); ok { use(after) } 

Variants:

  • bytes package
  • CutSuffix
  • if rest := strings.TrimPrefix(s, pre); rest != s { use(rest) } -> ...

cc: @josharian

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Jan 21, 2025
@gopherbot gopherbot added this to the Unreleased milestone Jan 21, 2025
@adonovan adonovan added the Refactoring Issues related to refactoring tools label Jan 21, 2025
@josharian
Copy link
Contributor

Also strings.Cut. If there's an idx := strings.Index(s, needle), and then the only expressions involving idx thereafter are idx < 0, idx >= 0, s[:idx], and s[idx+len(needle:] (possible with len(needle) as an int if needle is a constant), then those expressions can be replaced (as appropriate) by before, after, ok := strings.Cut(s, needle), ok, before, and after. Also strings.IndexByte. Mutatis mutandis for bytes.Cut.

@findleyr findleyr modified the milestones: Unreleased, gopls/backlog Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants