Skip to content

Warn on unclosed Postgres connects in functions#140

Merged
joshcramer merged 4 commits into
mainfrom
perf/connect-lint
Jun 26, 2026
Merged

Warn on unclosed Postgres connects in functions#140
joshcramer merged 4 commits into
mainfrom
perf/connect-lint

Conversation

@larimonious

Copy link
Copy Markdown
Contributor

Summary

  • add an ntnt lint warning for function-local std/db/postgres connect() calls that do not also close the handle
  • support aliased connect/close imports and wildcard postgres imports
  • add CLI lint regressions for unclosed, explicitly closed, and module-scope connects

Verification

  • cargo fmt
  • cargo build --profile dev-release
  • cargo test postgres_connect -- --nocapture
  • cargo test

@greptile-apps

greptile-apps Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a lint for function-local Postgres connections. The main changes are:

  • Track std/db/postgres connect and close imports, including aliases and wildcard imports.
  • Walk function ASTs to warn when a local Postgres connection is not closed.
  • Treat branch-local closes as conditional and match closes to specific handles.
  • Add lint tests for unclosed, closed, module-scope, conditional, and shadowed connection cases.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
src/main.rs Adds AST-based Postgres connect and close linting with alias support, conditional-close handling, and per-handle close matching.
tests/type_checker_tests.rs Adds tests for the new Postgres connection lint and the previously covered edge cases.

Reviews (4): Last reviewed commit: "fix: avoid reusing Postgres close events..." | Re-trigger Greptile

Comment thread src/main.rs
Comment thread src/main.rs
Comment thread src/main.rs
@larimonious

Copy link
Copy Markdown
Contributor Author

Addressed the Greptile findings in f37d25d.

Changes:

  • replaced the raw line/brace scanner with AST-based function-body scanning, so strings/comments containing braces cannot hide later connect() calls
  • require close() to target the same handle after the connect() line; unrelated closes no longer suppress the warning
  • match actual imported call identifiers instead of substring matching, so reconnect()/my_connect() do not false-positive
  • added regression tests for all three cases

Verification:

  • cargo fmt
  • cargo build --profile dev-release
  • cargo test test_lint -- --nocapture
  • cargo test
  • git diff --check

Comment thread src/main.rs Outdated
Comment thread src/main.rs Outdated
@joshcramer joshcramer merged commit 5757e65 into main Jun 26, 2026
9 checks passed
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.

2 participants