mgechev / revive

🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
https://revive.run
MIT License
4.69k stars 269 forks source link

var-naming rule can suggest invalid identifiers #977

Closed jonathangjertsen closed 3 months ago

jonathangjertsen commented 3 months ago

Describe the bug

I have a program in which there is a concept of "struct" which does not refer to go structs. struct is not a valid identifier, so I use struct_. This triggers the var-naming check, which says don't use underscores in Go names; var struct_ should be struct.

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 & no configuration file:
# flags

revive ./... | grep struct_

Expected behavior

If the result of removing the underscore is an invalid identifier, var-naming should either

  1. Allow leading or trailing underscore
  2. Suggest a valid identifier

Logs

N/A

Desktop (please complete the following information):

Additional context

This issue seems related to #613, but regardless of whether one thinks underscores in this usecase, the check should not suggest an invalid name

chavacava commented 3 months ago

Hi @jonathangjertsen , thanks for reporting the issue.

Using in identifiers is not idiomatic in Go, that is why the rule complains on `record`

Sure, the proposed fix (record) is not valid because it results to be a reserved word of the language. It could be possible to avoid these kinds of incorrect recommendations by checking against the list of reserved words.
In these cases, it could be tricky to propose an alternative name. Thus, the rule could just signal the naming problem without proposing a fix.

IMO, adding these checks isn't worth the performance and complexity penalities.

In your case, you could just use something like strukt to avoid the names clash.

chavacava commented 3 months ago

I will close the issue for now, and if it turns out to be impacting a larger number of users, I'll reopen it.