From 8f03a93b9231a3a3b463ec832246336d6eaec3e1 Mon Sep 17 00:00:00 2001 From: qdegraaf Date: Fri, 15 Sep 2023 17:58:55 +0200 Subject: [PATCH] Make test fixture more compact, clippy, regen snapshots --- .../test/fixtures/flake8_logging/LOG007.py | 11 ++------ crates/ruff/src/codes.rs | 5 +--- .../rules/exc_info_false_in_exception.rs | 15 +++++++---- ...ake8_logging__tests__LOG007_LOG007.py.snap | 27 ++++++++++++------- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_logging/LOG007.py b/crates/ruff/resources/test/fixtures/flake8_logging/LOG007.py index 45d49c6c0d6c6f..a2f4326706039b 100644 --- a/crates/ruff/resources/test/fixtures/flake8_logging/LOG007.py +++ b/crates/ruff/resources/test/fixtures/flake8_logging/LOG007.py @@ -1,17 +1,10 @@ import logging -from logging import exception - logging.exception("foo") # OK - - logging.exception("foo", exc_info=False) # LOG007 - - logging.exception("foo", exc_info=[]) # LOG007 +from logging import exception exception("foo", exc_info=False) # LOG007 - - -logging.exception("foo", exc_info=True) # OK +exception("foo", exc_info=True) # OK diff --git a/crates/ruff/src/codes.rs b/crates/ruff/src/codes.rs index fba066b4119f0f..aa47d8eb0af4ae 100644 --- a/crates/ruff/src/codes.rs +++ b/crates/ruff/src/codes.rs @@ -922,12 +922,9 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { // flake8-logging (Flake8Logging, "001") => (RuleGroup::Preview, rules::flake8_logging::rules::DirectLoggerInstantiation), + (Flake8Logging, "007") => (RuleGroup::Preview, rules::flake8_logging::rules::ExcInfoFalseInException), (Flake8Logging, "009") => (RuleGroup::Preview, rules::flake8_logging::rules::UndocumentedWarn), - // flake8-logging - (Flake8Logging, "007") => (RuleGroup::Nursery, rules::flake8_logging::rules::ExcInfoFalseInException), - (Flake8Logging, "009") => (RuleGroup::Nursery, rules::flake8_logging::rules::UndocumentedWarn), - _ => return None, }) } diff --git a/crates/ruff/src/rules/flake8_logging/rules/exc_info_false_in_exception.rs b/crates/ruff/src/rules/flake8_logging/rules/exc_info_false_in_exception.rs index 69d4d66a405636..a34cfc20774c31 100644 --- a/crates/ruff/src/rules/flake8_logging/rules/exc_info_false_in_exception.rs +++ b/crates/ruff/src/rules/flake8_logging/rules/exc_info_false_in_exception.rs @@ -15,7 +15,7 @@ use crate::checkers::ast::Checker; /// `exc_info=False` is the same as using `error()`, which is clearer and doesn’t need the /// `exc_info`argument. This rule detects `exception()` calls with an exc_info argument that is /// falsy. -/// +/// /// ## Example /// ```python /// logging.exception("foo", exc_info=False) @@ -42,10 +42,15 @@ pub(crate) fn exc_info_false_in_exception(checker: &mut Checker, call: &ExprCall .resolve_call_path(call.func.as_ref()) .is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "exception"])) { - if let Some(_) = call.arguments.find_keyword("exc_info").filter(|keyword| { - Truthiness::from_expr(&keyword.value, |id| checker.semantic().is_builtin(id)) - .is_falsey() - }) { + if call + .arguments + .find_keyword("exc_info") + .filter(|keyword| { + Truthiness::from_expr(&keyword.value, |id| checker.semantic().is_builtin(id)) + .is_falsey() + }) + .is_some() + { checker .diagnostics .push(Diagnostic::new(ExcInfoFalseInException, call.range())); diff --git a/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG007_LOG007.py.snap b/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG007_LOG007.py.snap index ea8b63f5060584..80156dcf8173a7 100644 --- a/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG007_LOG007.py.snap +++ b/crates/ruff/src/rules/flake8_logging/snapshots/ruff__rules__flake8_logging__tests__LOG007_LOG007.py.snap @@ -1,22 +1,31 @@ --- source: crates/ruff/src/rules/flake8_logging/mod.rs --- -LOG007.py:8:1: LOG007 Use of `logging.exception` with falsy `exc_info` +LOG007.py:4:1: LOG007 Use of `logging.exception` with falsy `exc_info` | -8 | logging.exception("foo", exc_info=False) # LOG007 +3 | logging.exception("foo") # OK +4 | logging.exception("foo", exc_info=False) # LOG007 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007 +5 | logging.exception("foo", exc_info=[]) # LOG007 | -LOG007.py:11:1: LOG007 Use of `logging.exception` with falsy `exc_info` - | -11 | logging.exception("foo", exc_info=[]) # LOG007 - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007 - | +LOG007.py:5:1: LOG007 Use of `logging.exception` with falsy `exc_info` + | +3 | logging.exception("foo") # OK +4 | logging.exception("foo", exc_info=False) # LOG007 +5 | logging.exception("foo", exc_info=[]) # LOG007 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007 +6 | +7 | from logging import exception + | -LOG007.py:14:1: LOG007 Use of `logging.exception` with falsy `exc_info` +LOG007.py:9:1: LOG007 Use of `logging.exception` with falsy `exc_info` | -14 | exception("foo", exc_info=False) # LOG007 + 7 | from logging import exception + 8 | + 9 | exception("foo", exc_info=False) # LOG007 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOG007 +10 | exception("foo", exc_info=True) # OK |