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

Do not use Sprintf to convert types #873

Open
breml opened this issue Sep 10, 2019 · 2 comments
Open

Do not use Sprintf to convert types #873

breml opened this issue Sep 10, 2019 · 2 comments

Comments

@breml
Copy link
Contributor

breml commented Sep 10, 2019

Do not use fmt.Sprintf to convert types (e.g. fmt.Sprintf("%d", aNumber), fmt.Sprintf("%t", aBoolean))

Before:

_ = fmt.Sprintf("%d", i)
_ = fmt.Sprintf("%t", i&1 == 0)

After:

_ = strconv.FormatInt(int64(i), 10)
_ = strconv.FormatBool(i&1 == 0)

Inspired by Bad Go: frivolous Sprintf

@quasilyte
Copy link
Member

It looks like it can be expressed with AST-based patterns quite easily:

https://github.com/quasilyte/go-ruleguard/blob/master/analyzer/testdata/src/extra/extra.rules.go

I'll copy a small example here:

m.Match(`fmt.Sprintf("%t", $i&1 == 0)`).
	Report(`use strconv.FormatBool($i&1 == 0)`)

We might include an ability to load ruleguard-based rules into go-critic later when it's more stable and is actually released, but for now, you can use go-ruleguard on its own. :)

If anyone submits a code that implements this check into go-critic (without ruleguard), I'm still willing to merge that PR.

quasilyte added a commit that referenced this issue Mar 18, 2020
This is part of the ruleguard integration experiment.

Hopefully, we can get some feedback to improve the way it's integrated.

The simplest way to try it out:
	gocritic check -enable ruleguard -@ruleguard.rules <gorules> <target>

Where:
	<gorules> is a path to a Go rules file
	<target> is a linting target

An example gorules file can be found here:
	https://github.com/quasilyte/go-ruleguard/blob/master/rules.go

Visit https://github.com/quasilyte/go-ruleguard for more info on the ruleguard.

Updates #873
Updates #869
Updates #837
Updates #836
Updates #831

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
quasilyte added a commit that referenced this issue Mar 18, 2020
This is part of the ruleguard integration experiment.

Hopefully, we can get some feedback to improve the way it's integrated.

The simplest way to try it out:
	gocritic check -enable ruleguard -@ruleguard.rules <gorules> <target>

Where:
	<gorules> is a path to a Go rules file
	<target> is a linting target

An example gorules file can be found here:
	https://github.com/quasilyte/go-ruleguard/blob/master/rules.go

Visit https://github.com/quasilyte/go-ruleguard for more info on the ruleguard.

Updates #873
Updates #869
Updates #837
Updates #836
Updates #831

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
quasilyte added a commit that referenced this issue Mar 18, 2020
This is part of the ruleguard integration experiment.

Hopefully, we can get some feedback to improve the way it's integrated.

The simplest way to try it out:
	gocritic check -enable ruleguard -@ruleguard.rules <gorules> <target>

Where:
	<gorules> is a path to a Go rules file
	<target> is a linting target

An example gorules file can be found here:
	https://github.com/quasilyte/go-ruleguard/blob/master/rules.go

Visit https://github.com/quasilyte/go-ruleguard for more info on the ruleguard.

Updates #873
Updates #869
Updates #837
Updates #836
Updates #831

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
quasilyte added a commit that referenced this issue Mar 18, 2020
This is part of the ruleguard integration experiment.

Hopefully, we can get some feedback to improve the way it's integrated.

The simplest way to try it out:
	gocritic check -enable ruleguard -@ruleguard.rules <gorules> <target>

Where:
	<gorules> is a path to a Go rules file
	<target> is a linting target

An example gorules file can be found here:
	https://github.com/quasilyte/go-ruleguard/blob/master/rules.go

Visit https://github.com/quasilyte/go-ruleguard for more info on the ruleguard.

Updates #873
Updates #869
Updates #837
Updates #836
Updates #831

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
quasilyte added a commit that referenced this issue Mar 18, 2020
This is part of the ruleguard integration experiment.

Hopefully, we can get some feedback to improve the way it's integrated.

The simplest way to try it out:
	gocritic check -enable ruleguard -@ruleguard.rules <gorules> <target>

Where:
	<gorules> is a path to a Go rules file
	<target> is a linting target

An example gorules file can be found here:
	https://github.com/quasilyte/go-ruleguard/blob/master/rules.go

Visit https://github.com/quasilyte/go-ruleguard for more info on the ruleguard.

Updates #873
Updates #869
Updates #837
Updates #836
Updates #831

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
quasilyte added a commit that referenced this issue Mar 18, 2020
This is part of the ruleguard integration experiment.

Hopefully, we can get some feedback to improve the way it's integrated.

The simplest way to try it out:
	gocritic check -enable ruleguard -@ruleguard.rules <gorules> <target>

Where:
	<gorules> is a path to a Go rules file
	<target> is a linting target

An example gorules file can be found here:
	https://github.com/quasilyte/go-ruleguard/blob/master/rules.go

Visit https://github.com/quasilyte/go-ruleguard for more info on the ruleguard.

Updates #873
Updates #869
Updates #837
Updates #836
Updates #831

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
@Antonboom
Copy link

Antonboom commented Oct 12, 2023

Related to golangci/golangci-lint#3714

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

No branches or pull requests

3 participants