Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
errstringmatch: extend brittle error-string detection to HasPrefix/HasSuffix/EqualFold/Index/LastIndex/Compare #40248
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
errstringmatch: extend brittle error-string detection to HasPrefix/HasSuffix/EqualFold/Index/LastIndex/Compare #40248
Changes from all commits
0ba0075fe6b1fdFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
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.
[/zoom-out]
EqualFoldandCompareare semantically symmetric —strings.EqualFold(someString, err.Error())andstrings.Compare(someString, err.Error()) == 0are equally brittle patterns, but they would not be flagged because onlyouter.Args[0]is checked forerr.Error(). This is consistent with the existing behaviour forContains, so it is not a regression introduced here.Worth adding a brief comment (or a
TODO) to the package doc orisErrDotErroracknowledging this known gap, so future contributors do not confuse the absence of second-arg detection with intentional design.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.
EqualFoldandComparewith swapped arguments silently bypass the linter, creating a false-confidence gap for the two symmetric functions this PR introduces.💡 Details
strings.EqualFoldandstrings.Compareare symmetric (argument order does not change the equality result). The linter only checksouter.Args[0]againsterr.Error(), so:For the non-symmetric functions (
HasPrefix,HasSuffix,Index,LastIndex) the reversed order has a different semantic, so skipping it is defensible. But EqualFold and Compare are genuinely equivalent in either order when used for equality testing.Consider extending the check for these two specifically:
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.
Negative test coverage is incomplete:
Index,LastIndex, andComparehave no "not flagged" test cases for non-error string arguments.💡 Details
Lines 69–77 add negative cases only for
HasPrefixandEqualFoldwith non-error string arguments.Index,LastIndex, andComparehave no corresponding negative tests. A regression where the linter incorrectly flagsstrings.Index(someString, "x")would go undetected. Add: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.
[/tdd] Only
HasPrefixandEqualFoldhave negative ("not flagged") test cases;HasSuffix,Index,LastIndex, andCompareare missing equivalent coverage. IfisErrDotErrorwere inadvertently broken for those functions, no test would catch it.💡 Suggested additions
One negative test per function makes the testdata file a self-documenting specification of what is and is not flagged.
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.
No
nolint:errstringmatchsuppression test covers any of the six newly added functions, leaving the nolint path untested for the expanded diagnostic message format.💡 Details
The existing nolint tests at lines 30–37 verify suppression only for
strings.Contains. None of the new functions (HasPrefix, HasSuffix, EqualFold, Index, LastIndex, Compare) have a corresponding suppression test. The diagnostic message changed from a hardcoded string to afmt.Sprintf-formatted string ("avoid strings.%s(err.Error(), ..."). If the format regresses, the nolint directive lookup could silently break for any of these six new functions without a test catching it.Add at least one suppression test for a representative new function:
Uh oh!
There was an error while loading. Please reload this page.