Skip to content

feat: add W014 case-without-else rule#32

Merged
Pawansingh3889 merged 2 commits intoPawansingh3889:mainfrom
hellozzm:feature/w014-case-without-else
May 2, 2026
Merged

feat: add W014 case-without-else rule#32
Pawansingh3889 merged 2 commits intoPawansingh3889:mainfrom
hellozzm:feature/w014-case-without-else

Conversation

@hellozzm
Copy link
Copy Markdown
Contributor

Summary

Implements rule W014 which warns on CASE ... END expressions that lack an ELSE branch. Unmatched rows silently return NULL, which is often unintended.

Changes

  • Added CaseWithoutElse rule class in sql_guard/rules/warnings.py
  • Registered in sql_guard/rules/__init__.py
  • Added fixture in tests/fixtures/warnings.sql
  • Added tests (fires + does-not-fire) in tests/test_rules.py

Test plan

36 passed in 1.60s

Closes #4

Checklist

  • ruff check . passes (N/A - no ruff installed in CI)
  • pytest -q passes (36/36)
  • Rule follows naming conventions (kebab-case, verb-oriented)
  • Rule has docstring citing the SQL hazard

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your first PR on sql-sop! Quick checks:

  • pytest -q and ruff check . pass locally
  • Every new rule has both a "fires on bad SQL" test AND a
    "does NOT fire on safe SQL" test
  • Rule IDs (E001, W001, …) are public API — don't renumber
    or rename existing ones without the deprecation process in
    GOVERNANCE.md
  • CHANGELOG.md [Unreleased] entry for any user-facing change

First-PR-wins on the linked issue applies for 7 days. The
maintainer will review within 14 days of your last response.

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Pawansingh3889
Copy link
Copy Markdown
Owner

Thanks @hellozzm! Structure is right (multi-line check, follows the W019 shape), all 7 CI checks pass on your branch, and the test cases match the issue body cleanly.

That said, three things are off, with the first being a direct miss on an issue requirement.

Issue body required nested-CASE detection. Currently broken. Issue #4 specifically called out: "Test with nested CASE expressions - make sure outer CASE without ELSE still triggers when inner CASE has ELSE." The rule fails exactly that case:

SELECT CASE
   WHEN x THEN CASE WHEN y THEN 1 ELSE 2 END
   WHEN z THEN 3
END FROM t;

@Pawansingh3889 Pawansingh3889 force-pushed the feature/w014-case-without-else branch from c54c6f7 to d9d2fa8 Compare May 2, 2026 10:35
hellozzm and others added 2 commits May 2, 2026 11:59
Warn on CASE expressions that lack an ELSE branch.\nUnmatched rows silently return NULL which is often unintended.\n\nCloses Pawansingh3889#4
@Pawansingh3889 Pawansingh3889 force-pushed the feature/w014-case-without-else branch from d9d2fa8 to ea7fa59 Compare May 2, 2026 11:08
@Pawansingh3889
Copy link
Copy Markdown
Owner

Took this over the line. Rebased on main (which now has W015 from #33), reworked the rule into a depth-aware token walk so each CASE/END block is judged independently — the nested example from issue #4 now fires correctly, since the outer block tracks its own ELSE count. Standalone END (T-SQL BEGIN ... END) is ignored when no matching CASE is on the stack.

Added two regression tests: outer-CASE-without-ELSE-while-inner-has-ELSE, and a BEGIN/END block that should stay quiet. The original passing/failing tests still pass. CHANGELOG / README / key-numbers bumped to 42/27.

Squashing once CI settles. Thanks @hellozzm for the rule, the bones were right and the nesting fix was a small lift on top.

@Pawansingh3889 Pawansingh3889 merged commit 943036d into Pawansingh3889:main May 2, 2026
1 check 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.

rule: W014 case-without-else - warn on CASE expression without ELSE

3 participants