Skip to content

fix: resolve ZeroDivisionError false positives via AST-based detection#360

Open
Prakhar54-byte wants to merge 15 commits into
imDarshanGK:mainfrom
Prakhar54-byte:fix/zero-division-ast-338
Open

fix: resolve ZeroDivisionError false positives via AST-based detection#360
Prakhar54-byte wants to merge 15 commits into
imDarshanGK:mainfrom
Prakhar54-byte:fix/zero-division-ast-338

Conversation

@Prakhar54-byte
Copy link
Copy Markdown
Contributor

Description
This PR resolves the false positive ZeroDivisionError reports by replacing the broad regex-based detection with a high-precision
AST-based analyzer. This significantly reduces noise in the analysis results and adds the capability to track zero-division
risks through function parameters.

Key Changes:

  • Engine Migration: Removed the noisy ZeroDivisionError regex from the rule-based engine that was flagging valid code like x /
    2.
  • AST Detection: Implemented direct literal zero-division detection (e.g., x / 0) using Python's Abstract Syntax Tree.
  • Deep Tracking: Added detection for literal 0 passed as an argument to function parameters that are subsequently used as
    divisors.
  • UX Enhancement: Updated the ast_analyzer to include line-numbered code context for all detections.
  • Testing: Added comprehensive regression tests in tests/test_ast_analyzer.py.

Related Issue
Fixes #338

Type of change

  • Bug fix
  • New feature / enhancement
  • Documentation update
  • Test addition
  • Refactor

Checklist

  • x] I have read [CONTRIBUTING.md (../CONTRIBUTING.md)
  • My branch is up to date with main
  • I have run pytest -v and all tests pass
  • I have not introduced duplicate issues or features
  • My PR title follows the format: fix: resolve ZeroDivisionError false positives via AST-based detection
  • I have added tests for new features
  • No hardcoded secrets or API keys in my code
  • This PR is linked to a GSSoC 2026 issue

Test evidence

1 $ cd backend && pytest tests/test_ast_analyzer.py -v
2 ============================= test session starts ==============================
3 platform linux -- Python 3.11.15, pytest-9.0.3, pluggy-1.6.0
4 rootdir: /home/prakhar/Downloads/AI-dev-assistant/backend
5 plugins: anyio-4.13.0, asyncio-1.3.0
6 collected 13 items                                                              
7
8 tests/test_ast_analyzer.py::test_unreachable_code PASSED                 [  7%]
9 tests/test_ast_analyzer.py::test_unused_import PASSED                    [ 15%]

10 tests/test_ast_analyzer.py::test_unused_argument PASSED [ 23%]
11 tests/test_ast_analyzer.py::test_clean_code_no_false_positives PASSED [ 30%]
12 tests/test_ast_analyzer.py::test_correct_line_number_unreachable PASSED [ 38%]
13 tests/test_ast_analyzer.py::test_self_not_flagged_as_unused PASSED [ 46%]
14 tests/test_ast_analyzer.py::test_underscore_param_not_flagged PASSED [ 53%]
15 tests/test_ast_analyzer.py::test_used_alias_not_flagged PASSED [ 61%]
16 tests/test_ast_analyzer.py::test_too_many_returns_exact_boundary PASSED [ 69%]
17 tests/test_ast_analyzer.py::test_unreachable_only_flags_after_not_before PASSED [ 76%]
18 tests/test_ast_analyzer.py::test_nested_function_returns_not_counted_in_outer PASSED [ 84%]
19 tests/test_ast_analyzer.py::test_deep_nesting_exact_boundary PASSED [ 92%]
20 tests/test_ast_analyzer.py::test_zero_division PASSED [100%]
21
22 ============================== 13 passed in 0.03s ==============================

- Removed broad and noisy ZeroDivisionError regex from rule-based engine
- Migrated ZeroDivisionError detection to ast_analyzer.py for higher precision
- Added support for detecting literal 0 passed to function divisor parameters
- Enhanced AST analyzer issues with line-numbered code context
- Added regression tests for both direct and parameter-based zero division
@imDarshanGK
Copy link
Copy Markdown
Owner

@Prakhar54-byte update your branch with the latest main changes

@Prakhar54-byte
Copy link
Copy Markdown
Contributor Author

Hi @imDarshanGK, I've updated the branch with the latest main changes and all checks are passing cleanly. Since the
repository is highly active and conflicts happen frequently, could you please review and merge this when you get a chance
so
it doesn't fall out of date again? Thank you!

@imDarshanGK
Copy link
Copy Markdown
Owner

@Prakhar54-byte CI failing

@Prakhar54-byte
Copy link
Copy Markdown
Contributor Author

@imDarshanGK ci failing resolved pls check

@Prakhar54-byte
Copy link
Copy Markdown
Contributor Author

Hi @imDarshanGK, I've updated the branch with the latest main changes and all checks are passing cleanly. Since the
repository is highly active and conflicts happen frequently, could you please review and merge this when you get a chance
so
it doesn't fall out of date again? Thank you!

@imDarshanGK
Copy link
Copy Markdown
Owner

@Prakhar54-byte update the branch with the latest main changes

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.

[Bug] ZeroDivisionError regex flags valid division operations

2 participants