Skip to content

Conversation

gmlewis
Copy link
Collaborator

@gmlewis gmlewis commented Oct 4, 2025

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.

Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
Copy link

codecov bot commented Oct 4, 2025

Codecov Report

❌ Patch coverage is 88.43537% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.11%. Comparing base (6195c56) to head (69c428e).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
example/commitpr/main.go 0.00% 6 Missing ⚠️
example/actionpermissions/main.go 0.00% 5 Missing ⚠️
example/verifyartifact/main.go 0.00% 3 Missing ⚠️
example/listenvironments/main.go 0.00% 1 Missing ⚠️
example/ratelimit/main.go 0.00% 1 Missing ⚠️
github/repos_contents.go 90.90% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Oct 4, 2025
@gmlewis
Copy link
Collaborator Author

gmlewis commented Oct 4, 2025

@stevehipwell - @alexandear - @zyfy29 - might you have time for a code review? Thank you!

Copy link
Contributor

@zyfy29 zyfy29 left a 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 ☺️

Comment on lines +11 to +14
_ = 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`
Copy link
Contributor

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.

Copy link
Collaborator Author

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 like fmt.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?

Copy link
Collaborator Author

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.

Copy link
Contributor

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!

Copy link
Contributor

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.

Copy link
Collaborator Author

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>
@gmlewis gmlewis changed the title Prefer %v over %d,%s and add urlpercentv custom linter Prefer %v over %d,%s and add fmtpercentv custom linter Oct 5, 2025
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
@gmlewis
Copy link
Collaborator Author

gmlewis commented Oct 5, 2025

OK, great idea, @zyfy29! I've renamed urlpercentv to fmtpercentv and generalized it to lint all call expression format strings, not just ones involving assignments. This only flagged 3 perfectly valid lines in the entire repo, so I've gone ahead and added the //nolint comments to those.

PTAL.

@zyfy29
Copy link
Contributor

zyfy29 commented Oct 6, 2025

Greet! LGTM

@gmlewis
Copy link
Collaborator Author

gmlewis commented Oct 6, 2025

Thank you, @zyfy29!
Merging.

@gmlewis gmlewis removed the NeedsReview PR is awaiting a review before merging. label Oct 6, 2025
@gmlewis gmlewis merged commit 4505771 into google:master Oct 6, 2025
6 of 7 checks passed
@gmlewis gmlewis deleted the add-url-percent-v-linter branch October 6, 2025 01:40
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

Successfully merging this pull request may close these issues.

2 participants