@@ -20,6 +20,10 @@ use super::helpers::is_empty_or_null_string;
2020/// A `pytest.warns` context manager should only contain a single
2121/// simple statement that triggers the expected warning.
2222///
23+ /// In [preview], this rule allows `pytest.warns` bodies to contain `for`
24+ /// loops with empty bodies (e.g., `pass` or `...` statements), to test
25+ /// iterator behavior.
26+ ///
2327/// ## Example
2428/// ```python
2529/// import pytest
@@ -38,12 +42,14 @@ use super::helpers::is_empty_or_null_string;
3842///
3943/// def test_foo_warns():
4044/// setup()
41- /// with pytest.warning (Warning):
45+ /// with pytest.warns (Warning):
4246/// foo()
4347/// ```
4448///
4549/// ## References
4650/// - [`pytest` documentation: `pytest.warns`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-warns)
51+ ///
52+ /// [preview]: https://docs.astral.sh/ruff/preview/
4753#[ derive( ViolationMetadata ) ]
4854pub ( crate ) struct PytestWarnsWithMultipleStatements ;
4955
@@ -204,8 +210,9 @@ pub(crate) fn complex_warns(checker: &mut Checker, stmt: &Stmt, items: &[WithIte
204210
205211 match stmt {
206212 Stmt :: With ( ast:: StmtWith { body, .. } ) => is_non_trivial_with_body ( body) ,
207- // Allow function and class definitions to test decorators
213+ // Allow function and class definitions to test decorators.
208214 Stmt :: ClassDef ( _) | Stmt :: FunctionDef ( _) => false ,
215+ // Allow empty `for` loops to test iterators.
209216 Stmt :: For ( ast:: StmtFor { body, .. } ) if in_preview => match & body[ ..] {
210217 [ Stmt :: Pass ( _) ] => false ,
211218 [ Stmt :: Expr ( ast:: StmtExpr { value, .. } ) ] => !value. is_ellipsis_literal_expr ( ) ,
0 commit comments