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

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

Rules definitions #39

mmorel-35 opened this issue Nov 22, 2024 · 13 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
Contributor

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
Contributor

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

@ccoVeille
Copy link
Contributor

The Christmas break is now over.

Let's regroup and start to think about it again

@catenacyber
Copy link
Owner

Happy new year @ccoVeille

Do you expect something from me here ?

@ccoVeille
Copy link
Contributor

ccoVeille commented Jan 5, 2025

Maybe yes, from you or from @mmorel-35 or @alexandear

It would be helpful if someone could list the option the linter should have and how to group them

Here is an example

- integer-format
    - int-conversion

- error-format
    - errorf

- string-format that will report
   - string-format.hex to use hex.EncodeToString
   - string-format.bool to use FormatBool
   - string-format.concat to use concatenation

Did I forgot something?

Assuming that if you disable string-format you disable everything, same for others.

Existing conf value will be supported, and mapped to new one.

Matthieu provided some guidance by creating a well documented issues. But everyone feedback made it difficult to follow. It's something like that I would like to get from you @catenacyber

About possible breaking change with the reporting:

we could add a setting that will allow to report the rules. This would allow us to provide rules name (like string-format.bool) in the reported error message, something @mmorel-35 tried to add I think.

  • or we simply consider people will have to deal with the breaking changes. It would only affect people who ignored some rules via issues node in golangci-lint.

  • or this change won't affect the way to report them, only the configuration

@catenacyber
Copy link
Owner

So, I see (adding current options that were not here)

- integer-format
    - int-conversion

- error-format
    - errorf
    - err-error (known behavior change adding panic for nil errors)

- string-format
   - sprintf1 (known behavior change removing panic)
   - strconcat

- bool-format

- hex-format

(We format into strings different objects)

@ccoVeille
Copy link
Contributor

ccoVeille commented Jan 8, 2025

Thanks

Is this post OK for you @alexandear @mmorel-35 ?

I'm asking before anyone start to consider working on this

@mmorel-35
Copy link
Contributor Author

It seems good to me.
IMHO, the next thing to do would to formalize it in a README or anything that is useful to document the rules namings and behavior.
And then the code could be aligned accordingly

@alexandear
Copy link
Contributor

Looks good to me, also.
Thank you for your work, @ccoVeille

This was referenced Jan 29, 2025
@catenacyber
Copy link
Owner

Fixed by #42 right ?

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