Skip to content

False positives for logging constant keys in another package? #66

@yizeng

Description

@yizeng

Wondering if I'm missing anything or this is by design: I defined a bunch of logging constant keys in a separate logging package and set no-raw-keys: true, but I get false positives "raw keys should not be used (sloglint)".

// main.go
package main

import (
	"log/slog"

	"sloglint-example/logging"
)

func main() {
	contentLength := int64(12345)
	url := "https://example.com/file"

	slog.Info("File downloaded successfully",
		slog.Int64(logging.KeyContentLength, contentLength),
		slog.String(logging.KeyURL, url),
	)
}
// logging/logging.go
package logging

const (
	KeyContentLength = "content_length"
	KeyURL           = "url"
)
linters:
  disable-all: true
  enable:
    - sloglint # Enable only sloglint

linters-settings:
  sloglint:
    # Enforce not mixing key-value pairs and attributes.
    no-mixed-args: true
    # Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only).
    kv-only: false
    # Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only).
    attr-only: true
    # Enforce using methods that accept a context.
    context: scope
    # Enforce using static values for log messages.
    static-msg: false
    # Enforce using constants instead of raw keys.
    no-raw-keys: true
    # Enforce a single key naming convention.
    # Values: snake, kebab, camel, pascal
    key-naming-case: snake
    # Enforce putting arguments on separate lines.
    args-on-sep-lines: true

Expected: No errors
Actual:

main.go:13:2: raw keys should not be used (sloglint)
slog.Info("File downloaded successfully",
^

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions