-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Prefer %v
over %d
,%s
and add fmtpercentv
custom linter
#3756
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
Conversation
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3756 +/- ##
=======================================
Coverage 91.11% 91.11%
=======================================
Files 187 187
Lines 16702 16702
=======================================
Hits 15218 15218
Misses 1296 1296
Partials 188 188 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@stevehipwell - @alexandear - @zyfy29 - might you have time for a code review? Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions. I'm not familiar with golangci custom linters, so please let me know if I'm wrong
_ = fmt.Sprintf("some/%d/url", 1) // want `use %v instead of %d` | ||
_ = fmt.Sprintf("some/%s/url", "yo") // want `use %v instead of %s` | ||
_ = fmt.Sprintf("some/%s/%d/url", "yo", 1) // want `use %v instead of %s and %d` | ||
_ = fmt.Sprintf("some/%d/%s/url", 1, "yo") // want `use %v instead of %s and %d` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+ fmt.Printf("some/%d/url", 1) // want `use %v instead of %d`
+ fmt.Printf("some/%s/url", "yo") // want `use %v instead of %s`
The linter currently only catches violations in assignment statements (e.g., url := fmt.Sprintf("some/%s", x)
), but misses
standalone function calls like fmt.Printf("some/%s", x)
. I've reproduced it locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linter currently only catches violations in assignment statements (e.g.,
url := fmt.Sprintf("some/%s", x)
), but misses
standalone function calls likefmt.Printf("some/%s", x)
.
I suppose that it wouldn't hurt to be consistent throughout the entire repo.
I originally wrote it just to catch all the URL generators, but maybe it should do the whole repo.
Should I change the name of the linter to percentv
instead of urlpercentv
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose that it wouldn't hurt to be consistent throughout the entire repo.
I now remember why I didn't do this. It was because %s
is perfectly valid when you have a []byte
and I didn't want to have to flag all these usages with a // nolint...
comment.
I believe the vast majority of these cases are in assignment statements, although I could be wrong. I could run some experiments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes, I didn't notice the linter name. It seems that all URL variable is created by a assignment statement. It's enough!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it could be avoided to use %s
when converting a []byte
to string
(the only usage for the remaining %s
). IMO since we have come to this point, it's better to lightly refactor it and ban all %s
usage without using // nolint...
. But it's a trade-off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, that's a fair point. Let me investigate by removing the requirement for making an assignment and see what happens. First, though, I'll try to take care of unnecessary .String()
calls... because that is so rare that I don't think we need a linter for it... or maybe golangci
already has an option for it... 👀 no, apparently it does not... and I still don't think we need a linter for that.
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
%v
over %d
,%s
and add urlpercentv
custom linter%v
over %d
,%s
and add fmtpercentv
custom linter
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
OK, great idea, @zyfy29! I've renamed PTAL. |
Greet! LGTM |
Thank you, @zyfy29! |
This PR formalizes the preference of
%v
over the use of%s
or%d
in this repo by adding a custom linter that enforces this preference.