ruff/crates/ruff_linter/src
Loïc Riegel 9490fbf1e1
[`pylint`] Detect subclasses of builtin exceptions (`PLW0133`) (#21382)
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
Closes #17347

Goal is to detect the useless exception statement not just for builtin
exceptions but also custom (user defined) ones.

## Test Plan

<!-- How was it tested? -->
I added test cases in the rule fixture and updated the insta snapshot.
Note that I first moved up a test case case which was at the bottom to
the correct "violation category".
I wasn't sure if I should create new test cases or just insert inside
those tests. I know that ideally each test case should test only one
thing, but here, duplicating twice 12 test cases seemed very verbose,
and actually less maintainable in the future. The drawback is that the
diff in the snapshot is hard to review, sorry. But you can see that the
snapshot gives 38 diagnostics, which is what we expect.

Alternatively, I also created this file for manual testing.
```py
# tmp/test_error.py

class MyException(Exception):
    ...
class MyBaseException(BaseException):
    ...
class MyValueError(ValueError):
    ...
class MyExceptionCustom(Exception):
    ...
class MyBaseExceptionCustom(BaseException):
    ...
class MyValueErrorCustom(ValueError):
    ...
class MyDeprecationWarning(DeprecationWarning):
    ...
class MyDeprecationWarningCustom(MyDeprecationWarning):
    ...
class MyExceptionGroup(ExceptionGroup):
    ...
class MyExceptionGroupCustom(MyExceptionGroup):
    ...
class MyBaseExceptionGroup(ExceptionGroup):
    ...
class MyBaseExceptionGroupCustom(MyBaseExceptionGroup):
    ...


def foo():
    Exception("...")
    BaseException("...")
    ValueError("...")
    RuntimeError("...")
    DeprecationWarning("...")
    GeneratorExit("...")
    SystemExit("...")
    ExceptionGroup("eg", [ValueError(1), TypeError(2), OSError(3), OSError(4)])
    BaseExceptionGroup("eg", [ValueError(1), TypeError(2), OSError(3), OSError(4)])
    MyException("...")
    MyBaseException("...")
    MyValueError("...")
    MyExceptionCustom("...")
    MyBaseExceptionCustom("...")
    MyValueErrorCustom("...")
    MyDeprecationWarning("...")
    MyDeprecationWarningCustom("...")
    MyExceptionGroup("...")
    MyExceptionGroupCustom("...")
    MyBaseExceptionGroup("...")
    MyBaseExceptionGroupCustom("...")

```

and you can run this to check the PR:
```sh
target/debug/ruff check tmp/test_error.py --select PLW0133 --unsafe-fixes --diff --no-cache --isolated --target-version py310
target/debug/ruff check tmp/test_error.py --select PLW0133 --unsafe-fixes --diff --no-cache --isolated --target-version py314
```
2025-12-09 13:49:55 -05:00
..
checkers New diagnostics for unused range suppressions (#21783) 2025-12-09 08:30:27 -08:00
comments Update insta snapshots (#14366) 2024-11-15 19:31:15 +01:00
cst Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
docstrings Add missing docstring sections to the numpy list (#20931) 2025-10-24 17:19:30 -04:00
fix Update Rust crate anyhow to v1.0.100 (#20499) 2025-09-22 09:51:52 +02:00
importer Move `Token`, `TokenKind` and `Tokens` to `ruff-python-ast` (#21760) 2025-12-02 20:10:46 +01:00
linter [`pylint`] Detect more exotic NaN literals in `PLW0177` (#18630) 2025-06-19 11:05:06 +00:00
message Set the diagnostic URL for lint errors (#21514) 2025-11-18 13:34:50 -05:00
registry Remove `Message::to_rule` (#18447) 2025-06-05 12:48:29 -04:00
rules [`pylint`] Detect subclasses of builtin exceptions (`PLW0133`) (#21382) 2025-12-09 13:49:55 -05:00
settings apply range suppressions to filter diagnostics (#21623) 2025-12-08 16:11:59 -08:00
snapshots [`flake8-bugbear`] Catch `yield` expressions within other statements (`B901`) (#21200) 2025-12-03 12:05:15 -05:00
codes.rs Add rule to detect unnecessary class properties (#21535) 2025-11-26 09:31:22 +01:00
directives.rs Move `Token`, `TokenKind` and `Tokens` to `ruff-python-ast` (#21760) 2025-12-02 20:10:46 +01:00
doc_lines.rs Move `Token`, `TokenKind` and `Tokens` to `ruff-python-ast` (#21760) 2025-12-02 20:10:46 +01:00
fs.rs Render unsupported syntax errors in formatter tests (#20777) 2025-10-13 10:00:37 -04:00
lib.rs new module for parsing ranged suppressions (#21441) 2025-12-02 15:39:59 -08:00
line_width.rs [`configuration`] Fix unclear error messages for line-length values exceeding `u16::MAX` (#21329) 2025-11-10 18:29:35 +00:00
linter.rs apply range suppressions to filter diagnostics (#21623) 2025-12-08 16:11:59 -08:00
locator.rs Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
logging.rs Display diffs for `ruff format --check` and add support for different output formats (#20443) 2025-09-30 12:00:51 -04:00
noqa.rs New diagnostics for unused range suppressions (#21783) 2025-12-09 08:30:27 -08:00
package.rs Detect empty implicit namespace packages (#14236) 2024-11-09 22:03:34 -05:00
packaging.rs Nested namespace packages support (#10541) 2024-03-24 22:53:32 -04:00
preview.rs [`pylint`] Detect subclasses of builtin exceptions (`PLW0133`) (#21382) 2025-12-09 13:49:55 -05:00
pyproject_toml.rs Rename `Diagnostic::syntax_error` methods, separate `Ord` implementation (#19179) 2025-07-08 09:54:19 -04:00
registry.rs Apply fix availability and applicability when adding to `DiagnosticGuard` and remove `NoqaCode::rule` (#18834) 2025-06-24 10:08:36 -04:00
renamer.rs Add a `ScopeKind` for the `__class__` cell (#20048) 2025-08-26 09:49:08 -04:00
rule_redirects.rs Document when a rule was added (#21035) 2025-10-23 14:48:41 -04:00
rule_selector.rs Document when a rule was added (#21035) 2025-10-23 14:48:41 -04:00
source_kind.rs Update MSRV to 1.85 and toolchain to 1.87 (#18126) 2025-05-16 09:19:55 +02:00
suppression.rs New diagnostics for unused range suppressions (#21783) 2025-12-09 08:30:27 -08:00
test.rs apply range suppressions to filter diagnostics (#21623) 2025-12-08 16:11:59 -08:00
text_helpers.rs Handle non-printable characters in diff view (#11687) 2024-06-08 06:22:03 +00:00
upstream_categories.rs Fix pylint upstream categories not showing in docs (#10441) 2024-03-18 01:27:39 +00:00
violation.rs Document when a rule was added (#21035) 2025-10-23 14:48:41 -04:00