-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat: add uncalled linter #3348
base: master
Are you sure you want to change the base?
Conversation
Hey, thank you for opening your first Pull Request ! |
While this implements the same functionality as rowserrcheck that linter uses buildssa which is currently incompatible with go generics as buildssa.SSA.SrcFuncs doesn't currently include Instantiations. This is a from scratch implementation that uses inspect.Analyzer which doesn't have such problems. It also doesn't suffer from the false positives when the code has multiple reassigned Credit to rowserrcheck for inspiration. |
In order for a pull request adding a linter to be reviewed, the linter and the PR must follow some requirements. Pull Request Description
Linter
The Linter Tests Inside Golangci-lint
|
I've actually refactored the original implementation which was limited to checking for missing database/sql Rows.Err() to use a configuration that will allow it to check for any number of missing calls. Working my way though validating your check list. |
@ldez thanks for the checklist, very helpful. I did notice it has more details than new linters guide so I'll try to find some time to update that if it would help. I believe all the items on your list are good, but look forward to your feedback. |
@ldez any feedback I need to address? |
Add rowserr a linter which checks for missing sql Rows.Err() calls that supports generics.
Refactor to use uncalled which is a generic version rowserr that uses a configuration to enable rules based checks instead of hard-coded for database/sql Rows.Err() checks only.
Update to uncalled v0.4.0 to align with golangci-lint config style.
Update to uncalled v0.5.0 which adds net http response close checks.
Run go mod tidy to clean up go.mod
Update to uncalled v0.7.1 which adds more checkers, adds the ability to handle more called types including direct calls in defers. This adds the following new rules: - http Response.Body.Close() - context context.CancelFunc()
Add default config, correct want and fix linter name.
Update to v0.8.0 which adds merge config support. This improves compatibility with golangci-lint by allowing the internal rules to be applied and overridden with minimal configuration.
Update uncalled to v0.8.1 which addresses a config data race.
cfc5a75
to
602314d
Compare
I see this is now tagged at blocked @ldez anything that I need to address? I just updated to the latest version and rebased so it's in line with current master. |
I blocked the PR because I think this linter is a kind of duplicate but I need to investigate. |
It does duplicate the functionality of rowserrcheck as mentioned in previous comment, but it uses buildssa which is currently incompatible with go generics as buildssa.SSA.SrcFuncs so users currently have no way to check for that issue if they are using go 1.18+ This is a from scratch implementation that uses inspect.Analyzer which doesn't have such problems. It also doesn't suffer from the false positives when the code has multiple reassigned Row variables that don't result in a real check. Finally this uses an extensible model based on configuration, providing three separate checks currently:
|
SSA supports generics for about 1 year #2649 (comment) And since go1.20, go1.18 is unmaintained. Maybe I miss something here 🤔 |
Thanks for checking back in on this. When I wrote this SSA didn't have full generics support specifically it didn't support instantiations causing rowserrcheck to be disabled in go 1.18+ which have generics. I only mentioned 1.18 as that's when generics was introduced. I just did a check with golangci-lint v1.51.2 and rowserrcheck is still disabled when running with go 1.20:
Looking at rowserrcheck it hasn't been updated since 2021, so unfortunately still isn't working with generics, although should be easier to fix now. uncalled, also has the ability to be extended with additional user checks via extra configuration so I think it still has value to the community, thoughts? |
Hi everyone I'm interested in the feature that uncalled linter would provide I'm thinking about a lot of use cases in my codebase. But I'm a bit lost in the current discussion, so bare with me, please. From what I understood, @ldez think it might be a duplicate, and is also worried about the fact that uncalled is coded in a way that prevents it to be added easily to golangci. Here is the point where you lost me guys 📦 I'm able to use uncalled via precommit hook, but having them in golangci would be easier. With my newbie point of view, I understand that uncalled might be a solution to replace rowserrcheck and provides a way to make people able to detect again what rowserrcheck was detecting, but that is likely to never be available as the project seems dead. So now, I said that... what is the current status ? @ldez : does you only need time to look at this project ? or does it mean @stevenh needs to write it totally ? because right now it's not compatible or may cause issues. Thank you both for your time on working on golangci-lint |
I believe the only concern is that it duplicates the check in uncalled is already fully integrated with |
Thanks for the explanation. From my understanding, it doesn't duplicate it. It supersedes it. I would be able not only to restore the feature of rowserrcheck on version above go1. 18 It will allow to provide way to define custom rules. rowserrcheck is only one possible things it can do. |
FYI rowserrcheck will be re-enabled #3691 |
Thanks for the information. Does it reduce the chance to get |
Yes because duplicated linters are not accepted, but I have to evaluate pro/cons to replace |
@stevenh, hi! @ldez, looks like
The killer feature is support not only std types 😎 P.S. @stevenh could you provide an example of |
But I confused from |
It's there to add structured logging with control over the level when running checks. Now std lib has slog I'll likely look to migrate to that. |
Thanks :)
bodyclose and rowserrcheck are already present in the config here
# Check for missing sql Rows.Close() calls.
- name: sql-rows-close
disabled: false
category: sql
packages:
- database/sql
- github.com/jmoiron/sqlx
methods: []
results:
- type: .Rows
pointer: true
expect:
call: .Close
args: []
- type: error
pointer: false
# Check for missing sql Stmt.Close() calls.
- name: sql-stmt-close
disabled: false
category: sql
packages:
- database/sql
- github.com/jmoiron/sqlx
methods: []
results:
- type: .Stmt
pointer: true
expect:
call: .Close
args: []
- type: error
pointer: false
# Check for missing sql NamedStmt.Close() calls.
- name: sql-namedstmt-close
disabled: false
category: sql
packages:
- github.com/jmoiron/sqlx
methods: []
results:
- type: .NamedStmt
pointer: true
expect:
call: .Close
args: []
- type: error
pointer: false
# Check for missing xml Encoder.Close() calls.
- name: xml-encoder-close
disabled: false
category: xml
packages:
- encoding/xml
methods: []
results:
- type: .Encoder
pointer: true
expect:
call: .Close
args: []
- type: error
pointer: false
If this is of interest, I can look at creating tests to confirm the above? |
Looks like Rows.Close is more complex as if |
Looking at it https://github.com/ryanrolds/sqlclosecheck suffers from the false positive when you have checked func Called(db *sql.DB) {
rows, _ := db.Query("select id from tb")
for rows.Next() {
// Handle row.
}
if rows.Err() != nil {
// Handle error.
fmt.Fprintln(ioutil.Discard, "error")
}
} |
Add uncalled linter which checks for missing calls based on configured rules and supports generics.