Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rules definitions #39

Open
mmorel-35 opened this issue Nov 22, 2024 · 5 comments
Open

Rules definitions #39

mmorel-35 opened this issue Nov 22, 2024 · 5 comments

Comments

@mmorel-35
Copy link
Contributor

The actual rules needs to clearly explicit to be easily understood and used by the users. This is a first draft that needs to be challenged to then enable the separation of concerns in the tool.

error-format

fmt.Errorf("Simple message")
fmt.Sprintf("%s", err)
fmt.Sprintf("%v", err)

✅
errors.New("Simple message")
err.Error()

with:

  • error-format.err-error option equivalent to err-error to use err.Error() even if it is only equivalent for non-nil errors

string-format

fmt.Sprintf("%s", strVal)
fmt.Sprintf("format string %s", strVal)
fmt.Sprintf("%s format string", strVal)

✅
strVal
"format string " + strVal
strVal + "format string"

with

  • string-format.concat option equivalent to strconcat to enable strings concatenation

bool-format

fmt.Sprintf("%t", boolVal)

✅
strconv.FormatBool(boolBal)

hash-format

fmt.Sprintf("%x", hash)

✅
hex.EncodeToString(hash)

integer-format

fmt.Sprintf("%d", id)
fmt.Sprintf("%v", version)

✅
strconv.Itoa(id)
strconv.FormatUint(uint64(version), 10)

with

  • integer-format.cast option equivalent to int-conversion to enable int or uint type cast
@alexandear
Copy link
Contributor

Please remember that we have to maximize compatibility with existing rules:

  perfsprint:
    # Optimizes even if it requires an int or uint type cast.
    # Default: true
    int-conversion: false
    # Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
    # Default: false
    err-error: true
    # Optimizes `fmt.Errorf`.
    # Default: true
    errorf: false
    # Optimizes `fmt.Sprintf` with only one argument.
    # Default: true
    sprintf1: false
    # Optimizes into strings concatenation.
    # Default: true
    strconcat: false

https://github.com/golangci/golangci-lint/blob/e0e37c43762068149116bcd69b305c1ca05647ae/.golangci.reference.yml#L2190

Maybe it is better to add simply only one string-format rule to cover format cases for all types fmt.Sprintf("%t", boolVal), fmt.Sprintf("%x", hash), fmt.Sprintf("%d", id).

@catenacyber
Copy link
Owner

hash-format

Let's name it hex-format ?

@ccoVeille
Copy link

I was more likely to report them the way @alexandear suggested.

So hex would be reported as string-format

But, then the logic of having a way to configure what linter is enabled, it won't be easy.

Does it mean that it would be reported as string-format.hex

@ccoVeille
Copy link

Another distinct question, the related configuration would be something hex-conv ? The same way we have int-conv

I try to think about the configuration at the same time as the way we report them

@catenacyber
Copy link
Owner

I guess you need some rules like integer-format to have options like int-conversion

One thing to emphasize about current option is that there are some that do behavior changes :

  • err-error : breaks behavior for nil errors
  • sprintf1 : changes behavior (avoids panic) for string with user inputting %whatever in it

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

No branches or pull requests

4 participants