Skip to content

Commit 6f85308

Browse files
committed
Fix names
1 parent 9229004 commit 6f85308

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

crates/ruff_linter/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ruff_python_stdlib::logging::LoggingLevel;
66
use ruff_text_size::Ranged;
77

88
use crate::checkers::ast::Checker;
9+
use crate::importer::ImportRequest;
910

1011
/// ## What it does
1112
/// Check for usages of the deprecated `warn` method from the `logging` module.
@@ -81,11 +82,25 @@ pub(crate) fn deprecated_log_warn(checker: &mut Checker, call: &ast::ExprCall) {
8182

8283
let mut diagnostic = Diagnostic::new(DeprecatedLogWarn, call.func.range());
8384
if checker.settings.preview.is_enabled() {
84-
if let Expr::Attribute(ast::ExprAttribute { attr, .. }) = call.func.as_ref() {
85-
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
86-
"warning".to_string(),
87-
attr.range(),
88-
)));
85+
match call.func.as_ref() {
86+
Expr::Attribute(ast::ExprAttribute { attr, .. }) => {
87+
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
88+
"warning".to_string(),
89+
attr.range(),
90+
)));
91+
}
92+
Expr::Name(_) => {
93+
diagnostic.try_set_fix(|| {
94+
let (import_edit, binding) = checker.importer().get_or_import_symbol(
95+
&ImportRequest::import("logging", "warning"),
96+
call.start(),
97+
checker.semantic(),
98+
)?;
99+
let name_edit = Edit::range_replacement(binding, call.func.range());
100+
Ok(Fix::safe_edits(import_edit, [name_edit]))
101+
});
102+
}
103+
_ => {}
89104
}
90105
}
91106
checker.diagnostics.push(diagnostic);

crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__preview__PGH002_PGH002_1.py.snap

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PGH002_1.py:4:1: PGH002 [*] `warn` is deprecated in favor of `warning`
2121
6 6 |
2222
7 7 | logger = logging.getLogger(__name__)
2323

24-
PGH002_1.py:5:1: PGH002 `warn` is deprecated in favor of `warning`
24+
PGH002_1.py:5:1: PGH002 [*] `warn` is deprecated in favor of `warning`
2525
|
2626
4 | logging.warn("this is not ok")
2727
5 | warn("not ok")
@@ -31,6 +31,16 @@ PGH002_1.py:5:1: PGH002 `warn` is deprecated in favor of `warning`
3131
|
3232
= help: Replace with `warning`
3333

34+
Safe fix
35+
2 2 | from logging import warn
36+
3 3 |
37+
4 4 | logging.warn("this is not ok")
38+
5 |-warn("not ok")
39+
5 |+logging.warning("not ok")
40+
6 6 |
41+
7 7 | logger = logging.getLogger(__name__)
42+
8 8 | logger.warn("this is not ok")
43+
3444
PGH002_1.py:8:1: PGH002 [*] `warn` is deprecated in favor of `warning`
3545
|
3646
7 | logger = logging.getLogger(__name__)

0 commit comments

Comments
 (0)