Skip to content

Possible false positive for enforce-slice-style in an expression context #1543

@connorszczepaniak-wk

Description

@connorszczepaniak-wk

Describe the bug
enforce-slice-style dictates using var x []int rather than x := []int{} for slice definitions when configured with the "nil" argument, but it also reports the usage of []int{} in an expression context where var x []int can't be used

To Reproduce
Steps to reproduce the behavior:

  1. I updated revive go install github.com/mgechev/revive@latest
  2. I run it with the following flags & configuration file:
revive -config=revive.toml ./...
[rule.enforce-slice-style]
Arguments = ["nil"]

With the following code:

package main

func main() {
    foo([]int{})
}

func foo(x []int) {}

And got this output:

main.go:4:6: use nil slice declaration (e.g. var args []type) instead of []type{}

Expected behavior
I didn't expect this to be a lint finding because this particular rule seems to be all about declarations, not expressions. The error message makes me think the linter is suggesting the following because it mentions var args []type

func main() {
    var x []int
    foo(x)
}

So I wonder if either:

  1. Only statements should be checked
  2. Or, maybe the error message could say something about use nil instead of []type when findings are in an expression context; using foo(nil) here also suppresses the lint.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions