Skip to content

tolowerequalfold: avoid false positives on ToLower(x) == x / != x case-detection idioms#36855

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-tolowerequalfold-issue
Draft

tolowerequalfold: avoid false positives on ToLower(x) == x / != x case-detection idioms#36855
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-tolowerequalfold-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 4, 2026

tolowerequalfold was flagging comparisons where one side is strings.ToLower/ToUpper and the other is the same operand, e.g. strings.ToLower(x) != x. Those are case-detection checks, not case-insensitive equality checks, and rewriting to strings.EqualFold can produce incorrect logic.

  • Analyzer precision: self-comparison guard

    • Added a guard in pkg/linters/tolowerequalfold/tolowerequalfold.go to skip diagnostics when:
      • one side is strings.ToLower/ToUpper(E), and
      • the other side is the same identifier E (matched via types.Object identity).
    • Refactored call detection with caseConvArg(...) and reused it from isCaseConvCall(...).
  • Fixture coverage for the missed idiom

    • Extended pkg/linters/tolowerequalfold/testdata/src/tolowerequalfold/tolowerequalfold.go okExamples with:
      • strings.ToLower(name) == name
      • strings.ToLower(name) != name
    • Existing flagged examples remain unchanged (e.g. comparisons against literals / different expressions).
if arg, ok := caseConvArg(expr.X); ok && sameOperand(pass, arg, expr.Y) {
    return // case-detection idiom: do not suggest EqualFold
}
if arg, ok := caseConvArg(expr.Y); ok && sameOperand(pass, expr.X, arg) {
    return
}
if isCaseConvCall(expr.X) || isCaseConvCall(expr.Y) {
    pass.ReportRangef(expr, "use strings.EqualFold ...")
}

Copilot AI and others added 2 commits June 4, 2026 06:09
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix linter precision for tolowerequalfold false-positives tolowerequalfold: avoid false positives on ToLower(x) == x / != x case-detection idioms Jun 4, 2026
Copilot AI requested a review from pelikhan June 4, 2026 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linter precision: tolowerequalfold false-positives on the strings.ToLower(x) == x case-detection idiom

2 participants