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

Add perfsprint linter #3714

Merged
merged 5 commits into from Oct 20, 2023
Merged

Add perfsprint linter #3714

merged 5 commits into from Oct 20, 2023

Conversation

catenacyber
Copy link
Contributor

@catenacyber catenacyber commented Mar 21, 2023

https://github.com/catenacyber/perfsprint

This is a linter for performance, aiming at usages of fmt.Sprintf which have faster alternatives in the standard library such as strconv.Itoa


I hope I did this correctly by following https://golangci-lint.run/contributing/new-linters/ and https://disaev.me/p/writing-useful-go-analysis-linter/

make vendor README.md did not work

See golang/go#59144 for an example on the Go std lib

@boring-cyborg
Copy link

boring-cyborg bot commented Mar 21, 2023

Hey, thank you for opening your first Pull Request !

@CLAassistant
Copy link

CLAassistant commented Mar 21, 2023

CLA assistant check
All committers have signed the CLA.

@ldez
Copy link
Member

ldez commented Mar 21, 2023

In order for a pull request adding a linter to be reviewed, the linter and the PR must follow some requirements.

  • The CLA must be signed

Pull Request Description

  • It must have a link to the linter repository.
  • It must provide a short description of the linter.

Linter

  • It must not be a duplicate of another linter or a rule of a linter. (the team will help to verify that)
  • It must have a valid license (AGPL is not allowed) and the file must contain the required information by the license, ex: author, year, etc.
  • The linter repository must have a CI and tests.
  • It must use go/analysis.
  • It must have a valid tag, ex: v1.0.0, v0.1.0.
  • It must not contain init().
  • It must not contain panic(), log.fatal(), os.exit(), or similar.
  • It must not have false positives/negatives. (the team will help to verify that)
  • It must have tests inside golangci-lint.

The Linter Tests Inside Golangci-lint

  • They must have at least one std lib import.
  • They must work with T=<name of the linter test file>.go make test_linters. (the team will help to verify that)

.golangci.reference.yml

  • The linter must be added to the list of available linters (alphabetical case-insensitive order).
    • enable and disable options
  • If the linter has a configuration, the exhaustive configuration of the linter must be added (alphabetical case-insensitive order)
    • The values must be different from the default ones.
    • The default values must be defined in a comment.
    • The option must have a short description.

Others Requirements

  • The files (tests and linter) inside golangci-lint must have the same name as the linter.
  • The .golangci.yml of golangci-lint itself must not be edited and the linter must not be added to this file.
  • The linters must be sorted in the alphabetical order (case-insensitive) in the Manager.GetAllSupportedLinterConfigs(...) and .golangci.reference.yml.
  • The load mode (WithLoadMode(...)):
    • if the linter doesn't use types: goanalysis.LoadModeSyntax
    • goanalysis.LoadModeTypesInfo required WithLoadForGoAnalysis() in the Manager.GetAllSupportedLinterConfigs(...)
  • The version in WithSince(...) must be the next minor version (v1.X.0) of golangci-lint.

Recommendations

  • The linter should not use SSA. (currently, SSA does not support generics)
  • The linter repository should have a readme and linting.
  • The linter should be published as a binary. (useful to diagnose bug origins)

The golangci-lint team will edit this comment to check the boxes before and during the review.

If the author of the PR is a member of the golangci-lint team, he should not edit this message.

The code review will start after the completion of those checkboxes (except for the specific items that the team will help to verify).

This checklist does not imply that we will accept the linter.

@ldez ldez added the linter: new Support new linter label Mar 21, 2023
@ldez ldez changed the title linter: add gostrconv Add gostrconv Mar 21, 2023
@ldez ldez added the feedback required Requires additional feedback label Mar 21, 2023
@leonklingele
Copy link
Member

This would close #3479.

@catenacyber have you checked your linter against the testcases of https://github.com/gostaticanalysis/numtostr?

@catenacyber
Copy link
Contributor Author

@leonklingele both linters are similar but target different functions : Sprint or Sprintf

I think this linter should also target "%x" as a format string...

@catenacyber
Copy link
Contributor Author

Just pushed a fixup commit to get go test -run TestSourcesFromTestdata

@ldez
Copy link
Member

ldez commented Oct 9, 2023

The items on the checklist should be handled.
Also, the readme of the linter repo should be updated.

@ldez ldez self-requested a review October 9, 2023 15:09
@catenacyber
Copy link
Contributor Author

Thanks for the feedback

It must provide a short description of the linter.

Just put one

@catenacyber
Copy link
Contributor Author

Also, the readme of the linter repo should be updated.

indeed

@Antonboom
Copy link
Contributor

@catenacyber hi!

I deeply reviewed your linter and found a lot of concerns (and fixed the most part of them).

Critical:

  1. Productionization catenacyber/perfsprint#1
  2. Please, rename project to nosprintfconv (or similar) catenacyber/perfsprint#2

Optional:

  1. Consider to support more strconv functions catenacyber/perfsprint#3

The good new – I run my branch on a some largest Go projects and found a lot of issues.
No false positives, no panics. But just terrible code like this pops up:

c.UI.Info(fmt.Sprintf("%s", pair.Key))          // Key string

Your linter did a great job 👍

@catenacyber
Copy link
Contributor Author

Thank you Anton, I will look into this :-)

@catenacyber

This comment was marked as off-topic.

@ldez ldez added blocked Need's direct action from maintainer and removed feedback required Requires additional feedback labels Oct 18, 2023
@catenacyber
Copy link
Contributor Author

So, what is left to do now ?
Rebase with the new name ?
How do I add the options for uint32 type conversion ?
Tag a new release ?
Anything else ?

@ldez ldez changed the title Add gostrconv Add perfsprint Oct 18, 2023
@ldez
Copy link
Member

ldez commented Oct 18, 2023

you have to update your PR and create a new tag for your linter to be able to update it inside your PR.

@catenacyber
Copy link
Contributor Author

Just tagged a new release and updated my PR. How does that look ?

@ldez
Copy link
Member

ldez commented Oct 19, 2023

Have you tried your PR locally?

pkg/lint/lintersdb/manager.go Outdated Show resolved Hide resolved
test/testdata/perfsprint.go Outdated Show resolved Hide resolved
@ldez
Copy link
Member

ldez commented Oct 19, 2023

FYI it doesn't compile because of a missing rename.
And the test produces a panic.

@catenacyber
Copy link
Contributor Author

I still get the panic locally but I do not understand why

@ldez
Copy link
Member

ldez commented Oct 19, 2023

please don't push force: you have removed my commits 2 times.

@ldez ldez removed the blocked Need's direct action from maintainer label Oct 19, 2023
@ldez ldez requested a review from Antonboom October 19, 2023 20:45
@ldez ldez changed the title Add perfsprint Add perfsprint linter Oct 19, 2023
Copy link
Contributor

@Antonboom Antonboom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

The single question highlighted by @catenacyber is requiring type cast in rare cases like:

// Before
dummyVMFullName := dummyVMPrefixName + "-" + fmt.Sprint(fnvHash.Sum32())

// After
dummyVMFullName := dummyVMPrefixName + "-" + strconv.FormatUint(uint64(fnvHash.Sum32()), 10)

But I do not consider this as a blocker due to linter performance nature (not style or readability).
Should be covered by catenacyber/perfsprint#7

Copy link
Member

@ldez ldez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ldez ldez merged commit de1c391 into golangci:master Oct 20, 2023
11 checks passed
@catenacyber
Copy link
Contributor Author

I am fine with this as first version.
Next version can have configuration for these integer conversions, and maybe more fmt.Sprintf cases covered (like %x on uint64 as strconv.FormatUint(x, 16) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linter: new Support new linter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants