diff --git a/crates/ruff/resources/test/fixtures/flake8_logging_format/G010.py b/crates/ruff/resources/test/fixtures/flake8_logging_format/G010.py index 8d16e5e050b8d..cd22173b5acac 100644 --- a/crates/ruff/resources/test/fixtures/flake8_logging_format/G010.py +++ b/crates/ruff/resources/test/fixtures/flake8_logging_format/G010.py @@ -1,5 +1,8 @@ import logging from distutils import log +from logging_setup import logger + logging.warn("Hello World!") log.warn("Hello world!") # This shouldn't be considered as a logger candidate +logger.warn("Hello world!") diff --git a/crates/ruff/src/rules/flake8_logging_format/snapshots/ruff__rules__flake8_logging_format__tests__G010.py.snap b/crates/ruff/src/rules/flake8_logging_format/snapshots/ruff__rules__flake8_logging_format__tests__G010.py.snap index b0a57cc6d18c3..e426e51f942bc 100644 --- a/crates/ruff/src/rules/flake8_logging_format/snapshots/ruff__rules__flake8_logging_format__tests__G010.py.snap +++ b/crates/ruff/src/rules/flake8_logging_format/snapshots/ruff__rules__flake8_logging_format__tests__G010.py.snap @@ -1,22 +1,40 @@ --- source: crates/ruff/src/rules/flake8_logging_format/mod.rs --- -G010.py:4:9: G010 [*] Logging statement uses `warn` instead of `warning` +G010.py:6:9: G010 [*] Logging statement uses `warn` instead of `warning` | -2 | from distutils import log -3 | -4 | logging.warn("Hello World!") +4 | from logging_setup import logger +5 | +6 | logging.warn("Hello World!") | ^^^^ G010 -5 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate +7 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate +8 | logger.warn("Hello world!") | = help: Convert to `warn` ℹ Fix -1 1 | import logging -2 2 | from distutils import log 3 3 | -4 |-logging.warn("Hello World!") - 4 |+logging.warning("Hello World!") -5 5 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate +4 4 | from logging_setup import logger +5 5 | +6 |-logging.warn("Hello World!") + 6 |+logging.warning("Hello World!") +7 7 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate +8 8 | logger.warn("Hello world!") + +G010.py:8:8: G010 [*] Logging statement uses `warn` instead of `warning` + | +6 | logging.warn("Hello World!") +7 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate +8 | logger.warn("Hello world!") + | ^^^^ G010 + | + = help: Convert to `warn` + +ℹ Fix +5 5 | +6 6 | logging.warn("Hello World!") +7 7 | log.warn("Hello world!") # This shouldn't be considered as a logger candidate +8 |-logger.warn("Hello world!") + 8 |+logger.warning("Hello world!") diff --git a/crates/ruff_python_semantic/src/analyze/logging.rs b/crates/ruff_python_semantic/src/analyze/logging.rs index 48fbc3fb16392..dd87707dd510a 100644 --- a/crates/ruff_python_semantic/src/analyze/logging.rs +++ b/crates/ruff_python_semantic/src/analyze/logging.rs @@ -19,24 +19,20 @@ use crate::model::SemanticModel; /// ``` pub fn is_logger_candidate(func: &Expr, semantic: &SemanticModel) -> bool { if let Expr::Attribute(ast::ExprAttribute { value, .. }) = func { - let Some(call_path) = (if let Some(call_path) = semantic.resolve_call_path(value) { - if call_path - .first() - .map_or(false, |module| *module == "logging") - || call_path.as_slice() == ["flask", "current_app", "logger"] - { - Some(call_path) - } else { - None + if let Some(call_path) = semantic + .resolve_call_path(value) + .or_else(|| collect_call_path(value)) + { + // Avoid some false positives. + if matches!(call_path.as_slice(), ["distutils", "log"]) { + return false; } - } else { - collect_call_path(value) - }) else { - return false; - }; - if let Some(tail) = call_path.last() { - if tail.starts_with("log") || tail.ends_with("logger") || tail.ends_with("logging") { - return true; + + if let Some(tail) = call_path.last() { + if tail.starts_with("log") || tail.ends_with("logger") || tail.ends_with("logging") + { + return true; + } } } }