From 291239b9f11edf49960664cf795b92bfef7973ee Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 26 Jan 2023 16:36:18 -0500 Subject: [PATCH] Fix range for `try-consider-else` (#2228) --- README.md | 2 +- src/rules/tryceratops/rules/try_consider_else.rs | 9 ++++----- ..._tryceratops__tests__try-consider-else_TRY300.py.snap | 7 ++++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 871191d62a..6912f20e88 100644 --- a/README.md +++ b/README.md @@ -1274,7 +1274,7 @@ For more, see [tryceratops](https://pypi.org/project/tryceratops/1.1.0/) on PyPI | TRY004 | prefer-type-error | Prefer `TypeError` exception for invalid type | 🛠 | | TRY200 | reraise-no-cause | Use `raise from` to specify exception cause | | | TRY201 | verbose-raise | Use `raise` without specifying exception name | | -| TRY300 | try-consider-else | Consider `else` block | | +| TRY300 | try-consider-else | Consider moving this statement to an `else` block | | | TRY301 | raise-within-try | Abstract `raise` to an inner function | | | TRY400 | error-instead-of-exception | Use `logging.exception` instead of `logging.error` | | diff --git a/src/rules/tryceratops/rules/try_consider_else.rs b/src/rules/tryceratops/rules/try_consider_else.rs index 96e1fd8810..0e7fdb6045 100644 --- a/src/rules/tryceratops/rules/try_consider_else.rs +++ b/src/rules/tryceratops/rules/try_consider_else.rs @@ -13,7 +13,7 @@ define_violation!( impl Violation for TryConsiderElse { #[derive_message_formats] fn message(&self) -> String { - format!("Consider `else` block") + format!("Consider moving this statement to an `else` block") } } @@ -22,10 +22,9 @@ pub fn try_consider_else(checker: &mut Checker, body: &[Stmt], orelse: &[Stmt]) if body.len() > 1 && orelse.is_empty() { if let Some(stmt) = body.last() { if let StmtKind::Return { .. } = &stmt.node { - checker.diagnostics.push(Diagnostic::new( - TryConsiderElse, - Range::from_located(&body[0]), - )); + checker + .diagnostics + .push(Diagnostic::new(TryConsiderElse, Range::from_located(stmt))); } } } diff --git a/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__try-consider-else_TRY300.py.snap b/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__try-consider-else_TRY300.py.snap index 408f5ea79a..07a6db2f9c 100644 --- a/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__try-consider-else_TRY300.py.snap +++ b/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__try-consider-else_TRY300.py.snap @@ -5,10 +5,11 @@ expression: diagnostics - kind: TryConsiderElse: ~ location: - row: 18 + row: 20 column: 8 end_location: - row: 18 - column: 13 + row: 20 + column: 16 fix: ~ parent: ~ +