diff --git a/crates/ruff/resources/test/fixtures/flake8_bandit/S101.py b/crates/ruff/resources/test/fixtures/flake8_bandit/S101.py index 3fd80d6bd3..2b9ab1b337 100644 --- a/crates/ruff/resources/test/fixtures/flake8_bandit/S101.py +++ b/crates/ruff/resources/test/fixtures/flake8_bandit/S101.py @@ -1,11 +1,13 @@ -# Error -assert True +assert True # S101 + def fn(): x = 1 + assert x == 1 # S101 + assert x == 2 # S101 - # Error - assert x == 1 - # Error - assert x == 2 +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + assert True # OK diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index c59c7ff272..0c0b480d6c 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -1603,16 +1603,18 @@ where } } StmtKind::Assert { test, msg } => { + if !self.ctx.in_type_checking_block { + if self.settings.rules.enabled(Rule::Assert) { + self.diagnostics + .push(flake8_bandit::rules::assert_used(stmt)); + } + } if self.settings.rules.enabled(Rule::AssertTuple) { pyflakes::rules::assert_tuple(self, stmt, test); } if self.settings.rules.enabled(Rule::AssertFalse) { flake8_bugbear::rules::assert_false(self, stmt, test, msg.as_deref()); } - if self.settings.rules.enabled(Rule::Assert) { - self.diagnostics - .push(flake8_bandit::rules::assert_used(stmt)); - } if self.settings.rules.enabled(Rule::PytestAssertAlwaysFalse) { if let Some(diagnostic) = flake8_pytest_style::rules::assert_falsy(stmt, test) { self.diagnostics.push(diagnostic); diff --git a/crates/ruff/src/rules/flake8_bandit/snapshots/ruff__rules__flake8_bandit__tests__S101_S101.py.snap b/crates/ruff/src/rules/flake8_bandit/snapshots/ruff__rules__flake8_bandit__tests__S101_S101.py.snap index 31e64a5c69..fd5eee585c 100644 --- a/crates/ruff/src/rules/flake8_bandit/snapshots/ruff__rules__flake8_bandit__tests__S101_S101.py.snap +++ b/crates/ruff/src/rules/flake8_bandit/snapshots/ruff__rules__flake8_bandit__tests__S101_S101.py.snap @@ -1,29 +1,27 @@ --- source: crates/ruff/src/rules/flake8_bandit/mod.rs --- -S101.py:2:1: S101 Use of `assert` detected +S101.py:1:1: S101 Use of `assert` detected | -2 | # Error -3 | assert True +1 | assert True # S101 | ^^^^^^ S101 -4 | -5 | def fn(): | -S101.py:8:5: S101 Use of `assert` detected - | - 8 | # Error - 9 | assert x == 1 - | ^^^^^^ S101 -10 | -11 | # Error - | +S101.py:6:5: S101 Use of `assert` detected + | +6 | def fn(): +7 | x = 1 +8 | assert x == 1 # S101 + | ^^^^^^ S101 +9 | assert x == 2 # S101 + | -S101.py:11:5: S101 Use of `assert` detected - | -11 | # Error -12 | assert x == 2 - | ^^^^^^ S101 - | +S101.py:7:5: S101 Use of `assert` detected + | +7 | x = 1 +8 | assert x == 1 # S101 +9 | assert x == 2 # S101 + | ^^^^^^ S101 + |