Skip to content

[Code Quality] Fix trimleftright linter heuristic: repeated-rune axis misses common cases #46574

Description

@github-actions

Description

The trimleftright linter in pkg/linters/trimleftright/trimleftright.go fires only when a cutset contains a repeated rune (e.g., TrimLeft(s, "foo") triggers because 'o' repeats). This misses the most common real instances of the bug — distinct-char strings like "http", "bar", "abc", "0x", "prefix" — which are exactly the patterns where users confuse TrimLeft/TrimRight with TrimPrefix/TrimSuffix.

The repeated-rune guard adds nothing to false-positive avoidance (the all-alphanumeric guard already handles whitespace character classes) and only removes true positives. The testdata in goodNoRepeatedAlnum canonizes this broken behavior as intentional.

Suggested Changes

  • Replace the repeated-rune trigger in looksSuspiciousCutset with a class-aware discriminator
  • Flag all multi-rune alphanumeric cutsets except recognized class idioms (contiguous ranges, hex digits, digit sets like "0123456789", vowel sets like "aeiou")
  • Update testdata/trimleftright.go to expect diagnostics for TrimLeft(s, "http"), TrimLeft(s, "abc"), TrimLeft(s, "txt")
  • Keep TrimLeft(s, "aeiou"), TrimLeft(s, "0123456789"), TrimLeft(s, "abcdef") as passing (recognized class patterns)
  • Or document the narrow scope explicitly in the linter README if intentional

Files Affected

  • pkg/linters/trimleftright/trimleftright.go (lines 125-146, looksSuspiciousCutset)
  • pkg/linters/trimleftright/testdata/ (update test fixtures)

Success Criteria

  • TrimLeft(s, "http") is flagged as suspicious
  • TrimLeft(s, "aeiou") is not flagged (recognized character class)
  • make golint-custom passes with updated tests
  • False-positive rate does not increase for whitespace character classes

Source

Extracted from Sergo Report: New-Linter Audit - 2026-07-19 #46528

Priority

Medium — The linter misses its primary use case; fixing it will catch real bugs in the codebase

🔍 Task mining by Discussion Task Miner - Code Quality Improvement Agent · 83.8 AIC · ⌖ 5.28 AIC · ⊞ 7K ·

  • expires on Jul 19, 2026, 11:45 PM UTC-08:00

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions