From b3e8b1b78726d0df433fef2e25e0f0fe144ebe1f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 27 Jan 2023 18:41:16 -0500 Subject: [PATCH] Expand heuristic for detecting logging calls (#2279) --- resources/test/fixtures/tryceratops/TRY400.py | 10 +++++++++ src/ast/helpers.rs | 2 +- ..._error-instead-of-exception_TRY400.py.snap | 22 ++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/resources/test/fixtures/tryceratops/TRY400.py b/resources/test/fixtures/tryceratops/TRY400.py index c9d6f4697a..f8deb6ad9b 100644 --- a/resources/test/fixtures/tryceratops/TRY400.py +++ b/resources/test/fixtures/tryceratops/TRY400.py @@ -28,6 +28,16 @@ def bad(): logger.error("Context message here") +def bad(): + try: + a = 1 + except Exception: + log.error("Context message here") + + if True: + log.error("Context message here") + + def bad(): try: a = 1 diff --git a/src/ast/helpers.rs b/src/ast/helpers.rs index 4ef1223ea2..1af0a0668c 100644 --- a/src/ast/helpers.rs +++ b/src/ast/helpers.rs @@ -1010,7 +1010,7 @@ pub fn is_logger_candidate(func: &Expr) -> bool { if let ExprKind::Attribute { value, .. } = &func.node { let call_path = collect_call_path(value); if let Some(tail) = call_path.last() { - if *tail == "logging" || tail.ends_with("logger") { + if tail.starts_with("log") || tail.ends_with("logger") || tail.ends_with("logging") { return true; } } diff --git a/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__error-instead-of-exception_TRY400.py.snap b/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__error-instead-of-exception_TRY400.py.snap index 0c3edfb929..99cf19c4a9 100644 --- a/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__error-instead-of-exception_TRY400.py.snap +++ b/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__error-instead-of-exception_TRY400.py.snap @@ -49,7 +49,7 @@ expression: diagnostics column: 8 end_location: row: 35 - column: 49 + column: 41 fix: ~ parent: ~ - kind: @@ -59,6 +59,26 @@ expression: diagnostics column: 12 end_location: row: 38 + column: 45 + fix: ~ + parent: ~ +- kind: + ErrorInsteadOfException: ~ + location: + row: 45 + column: 8 + end_location: + row: 45 + column: 49 + fix: ~ + parent: ~ +- kind: + ErrorInsteadOfException: ~ + location: + row: 48 + column: 12 + end_location: + row: 48 column: 53 fix: ~ parent: ~