Skip to content

Conversation

@sugyan
Copy link

@sugyan sugyan commented Jun 9, 2025

Thank you for this excellent analysis tool!

Summary

As mentioned in issue #43, it would be helpful to check not only if resp.Body.Close() is called, but also whether the response body is actually consumed. This PR implements an optional -check-consumption flag to provide this
functionality.

Motivation

While the current tool ensures bodies are closed, checking consumption is also valuable for:

  • Proper connection reuse in HTTP clients
  • Preventing potential resource leaks
  • Following Go HTTP client best practices

Implementation

Key Features

  • Fully backward compatible - existing behavior unchanged without the flag
  • Opt-in functionality - only active with -check-consumption flag
  • Comprehensive detection for common consumption patterns

Supported Consumption Patterns

  • io.Copy(io.Discard, resp.Body)
  • io.ReadAll(resp.Body) / ioutil.ReadAll(resp.Body)
  • json.NewDecoder(resp.Body)
  • bufio.NewScanner(resp.Body) / bufio.NewReader(resp.Body)

Documentation

  • False positive cases documented with clear examples and workarounds
  • README updated with usage examples and limitations

Known Limitations

Since detecting all consumption patterns is challenging, some false positives may occur:

  • Custom consumption patterns may not be detected
  • Execution order checking is not implemented
  • Workaround: Use //nolint:bodyclose for false positives

All limitations are documented in the README with examples.

Compatibility

No impact on existing functionality - the new feature is completely opt-in via the -check-consumption flag.


Note: This pull request includes commits generated with Claude Code assistance, but I have carefully reviewed and guided the implementation throughout the development process.

This enhancement helps developers write more robust HTTP client code while maintaining the tool's existing simplicity and reliability.

sugyan and others added 3 commits June 9, 2025 13:40
- Add -check-consumption flag to enable body consumption verification
- Implement detection for common consumption patterns (io.Copy, io.ReadAll, json.NewDecoder, etc.)
- Add comprehensive test suite with 215 test cases covering supported patterns and edge cases
- Document limitations and false positive scenarios in README
- Maintain full backward compatibility with existing functionality

When enabled, the analyzer ensures response bodies are both closed AND consumed,
helping prevent resource leaks and incomplete request handling.

Supported consumption patterns:
- io.Copy(io.Discard, resp.Body)
- io.ReadAll(resp.Body) / ioutil.ReadAll(resp.Body)
- json.NewDecoder(resp.Body)
- bufio.NewScanner(resp.Body) / bufio.NewReader(resp.Body)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Switch from unitchecker to singlechecker for cleaner implementation
- Remove unnecessary analyzers() wrapper function
- Simplify main function to single line

This change improves maintainability and follows best practices for
single-analyzer tools.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@sugyan sugyan force-pushed the feature/consumption-checking branch from 9319fc3 to 742a4ad Compare June 9, 2025 05:44
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.

1 participant