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

WIP Add a bit more rules for strings module #94

Closed
wants to merge 20 commits into from

Conversation

orsinium
Copy link
Contributor

Blocked by #93, contains it in diff (because I need a place to put tests)

Improved checks for n value in strings.Replace, strings.MatchN, and strings.MatchAfterN:

  1. Handle any negative number, not only -1
  2. Use suggestion instead of message for 0.
  3. Suggest 2 instead of 1 because n means "parts count", not "splits count" (it's the opposite in Python).
  4. Report strings.Count($s1, $s2) >= 0, it's always true.

@cristaloleg
Copy link
Collaborator

Looks like there are commits from #93 care to rebase?

m.Match(`strings.TrimFunc($s, unicode.IsSpace)`).Suggest(`strings.TrimSpace($s)`)
m.Match(`strings.Map(unicode.ToUpper, $s)`).Suggest(`strings.ToUpper($s)`)
m.Match(`strings.Map(unicode.ToLower, $s)`).Suggest(`strings.ToLower($s)`)
m.Match(`strings.Map(unicode.ToTitle, $s)`).Suggest(`strings.ToTitle($s)`)

m.Match(`bytes.SplitN($s, $sep, -1)`).Suggest(`bytes.Split($s, $sep)`)
m.Match(`bytes.Replace($s, $old, $new, -1)`).Suggest(`bytes.ReplaceAll($s, $old, $new)`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

why it's removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The rule is covered by the newly introduced more general rule. Now, we handle all negative numbers, not only -1. See the tests.

@@ -48,6 +50,8 @@ func _(m fluent.Matcher) {
Suggest(`strings.Contains($s1, $s2)`)
m.Match(`strings.Count($s1, $s2) == 0`).
Suggest(`!strings.Contains($s1, $s2)`)
m.Match(`strings.Count($s1, $s2) >= 0`).
Report(`statement always true`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like bytes.Count deserve same fame ;}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch! Thank you. Let's add it :)

func gocriticBadCall(m fluent.Matcher) {
m.Match(`strings.Replace($_, $_, $_, 0)`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here - why removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The rule is covered by the newly introduced more general rule. See the tests.

@orsinium
Copy link
Contributor Author

Looks like there are commits from #93 care to rebase?

Yes, it is blocked by that PR. See the PR description. I need the test file I introduced there. I'll merge the master when #93 is merged. Sorry for messing it up.

@orsinium
Copy link
Contributor Author

I mark PR as WIP until #93 is merged.

@orsinium orsinium changed the title Add a bit more rules for strings module WIP Add a bit more rules for strings module Oct 23, 2020
@cristaloleg
Copy link
Collaborator

Why not just push only changes related to checks? 👀

@orsinium
Copy link
Contributor Author

Because there are no tests for checks in master yet. I feel uncomfortable to commit untested changes.

@cristaloleg
Copy link
Collaborator

https://tenor.com/view/allow-community-sombrero-ill-allow-it-gif-10817586

@quasilyte
Copy link
Owner

quasilyte commented Oct 23, 2020

This change includes more than new rules.

It would be easier to review if it was separated into independent PRs.

I'm 100% fine with changes that cleanup the readme in a separate PR. They're super easy to review and can be merged right away.

@orsinium orsinium closed this Oct 23, 2020
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.

3 participants