dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.19k stars 377 forks source link

Recommend replacing `rx.FindIndex(b) != nil` with `rx.Match(b)` #1518

Open kevinburkesegment opened 7 months ago

kevinburkesegment commented 7 months ago

I was just looking at some code that compiled a regex, and then checked for a match by writing:

rx := regexp.MustCompile(/* ... */)
return rx.FindIndex(b) != nil 

This is reducible to return rx.Match(b), which is less wasteful. It would be good to suggest this optimization.

dominikh commented 7 months ago

I've retitled the issue to more closely match what I think your intention is.

kevinburkesegment commented 7 months ago

Exactly thank you!

On Thu, Apr 11, 2024 at 19:56 Dominik Honnef @.***> wrote:

I've retitled the issue to more closely match what I think your intention is.

— Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/dominikh/go-tools/issues/1518*issuecomment-2050875005__;Iw!!NCc8flgU!Yl6go6hZF-NhVXzsBJIXRL1AqAqKucHHPLONTtohsSeS9jV2YyXfnOWrfMQnfdbgWYxiBEScECxw-wnwnkTLdmDVX4o$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AW5SL4XSPT3ACXWPKHD4FP3Y45EMXAVCNFSM6AAAAABGDBO7UKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJQHA3TKMBQGU__;!!NCc8flgU!Yl6go6hZF-NhVXzsBJIXRL1AqAqKucHHPLONTtohsSeS9jV2YyXfnOWrfMQnfdbgWYxiBEScECxw-wnwnkTLS3iqSV4$ . You are receiving this because you authored the thread.Message ID: @.***>