Skip to content

rawloginlib precision: isRawLog matches the identifier name "log" syntactically — shadowing FP and alias-import FN in a CI-enfor [Content truncated due to length] #39981

Description

@github-actions

Summary

rawloginlib decides whether a call targets the standard log package by comparing the receiver identifier's name to the literal string "log", with no type resolution. This produces both a false positive (a local/parameter/field named log bound to a custom logger) and a false negative (an aliased import of log). Because rawloginlib is CI-enforced (.github/workflows/cgo.yml:1123 LINTER_FLAGS), the false positive is a build-blocker.

This is the same precision class already fixed for sortslice (#38029, merged) and still open for regexpcompileinfunction (#39733) and ctxbackground (#38789).

Location

pkg/linters/rawloginlib/rawloginlib.go:62

ident, ok := sel.X.(*ast.Ident)
if !ok {
    return
}
if ident.Name == "log" && rawLogFuncs[sel.Sel.Name] {   // <-- syntactic name match, no TypesInfo

False positive (build-blocking)

log is one of the most common local-variable / parameter names for a logger. A custom logger that shadows the package name is wrongly flagged:

func handle(log *logger.Logger) {
    log.Printf("...")   // custom logger — NOT stdlib log, but ident.Name == "log" => reported
}

Since the linter runs in CI enforcement, this fails the build on legitimate code.

False negative

An aliased import escapes detection entirely, defeating the enforcement the linter exists to provide:

import applog "log"

func f() { applog.Fatal("boom") }   // ident.Name == "applog" => not reported

Recommended fix

Resolve package identity through the type system, mirroring the existing canonical helper astutil.IsFmtErrorf (pkg/linters/internal/astutil/astutil.go:64-72):

obj := pass.TypesInfo.ObjectOf(ident)
pkgName, ok := obj.(*types.PkgName)
if ok && pkgName.Imported().Path() == "log" && rawLogFuncs[sel.Sel.Name] { ... }

Note that osexitinlibrary (osexitinlibrary.go:59, ident.Name == "os") and fprintlnsprintf (fprintlnsprintf.go:144, ident.Name == "fmt") share the identical syntactic gap. Consider extracting a shared astutil.IsStdPkgSelector(pass, sel, pkgPath) helper and reusing it across all three to fix the class once.

Validation checklist

  • Shadowing test: parameter/local log of a non-log type calling log.Printfno diagnostic.
  • Alias test: import applog "log" then applog.Fatal(...) → diagnostic reported.
  • Existing positive: direct log.Printf in a pkg/ package still reported.
  • go test ./pkg/linters/rawloginlib/... green.

Effort

Small — single-file change, or single-file plus an optional shared astutil helper that also closes osexitinlibrary and fprintlnsprintf.

Generated by 🤖 Sergo - Serena Go Expert ·

  • expires on Jun 24, 2026, 9:25 PM UTC-08:00

Metadata

Metadata

Labels

cookieIssue Monster Loves Cookies!sergo

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions