mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
Allow EM
fixes even if msg
variable is defined (#9059)
This PR updates the `EM` rules to generate the auto-fix even if the `msg` variable is defined in the current scope. As discussed in https://github.com/astral-sh/ruff/issues/9052.
This commit is contained in:
parent
e043bd46b5
commit
b7dd2b5941
4 changed files with 72 additions and 34 deletions
|
@ -27,7 +27,7 @@ def f_ok():
|
|||
raise RuntimeError(msg)
|
||||
|
||||
|
||||
def f_unfixable():
|
||||
def f_msg_defined():
|
||||
msg = "hello"
|
||||
raise RuntimeError("This is an example exception")
|
||||
|
||||
|
|
|
@ -191,15 +191,13 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
|
|||
if let Some(indentation) =
|
||||
whitespace::indentation(checker.locator(), stmt)
|
||||
{
|
||||
if checker.semantic().is_available("msg") {
|
||||
diagnostic.set_fix(generate_fix(
|
||||
stmt,
|
||||
first,
|
||||
indentation,
|
||||
checker.stylist(),
|
||||
checker.locator(),
|
||||
));
|
||||
}
|
||||
diagnostic.set_fix(generate_fix(
|
||||
stmt,
|
||||
first,
|
||||
indentation,
|
||||
checker.stylist(),
|
||||
checker.locator(),
|
||||
));
|
||||
}
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
@ -211,15 +209,13 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
|
|||
let mut diagnostic = Diagnostic::new(FStringInException, first.range());
|
||||
if let Some(indentation) = whitespace::indentation(checker.locator(), stmt)
|
||||
{
|
||||
if checker.semantic().is_available("msg") {
|
||||
diagnostic.set_fix(generate_fix(
|
||||
stmt,
|
||||
first,
|
||||
indentation,
|
||||
checker.stylist(),
|
||||
checker.locator(),
|
||||
));
|
||||
}
|
||||
diagnostic.set_fix(generate_fix(
|
||||
stmt,
|
||||
first,
|
||||
indentation,
|
||||
checker.stylist(),
|
||||
checker.locator(),
|
||||
));
|
||||
}
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
@ -236,15 +232,13 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
|
|||
if let Some(indentation) =
|
||||
whitespace::indentation(checker.locator(), stmt)
|
||||
{
|
||||
if checker.semantic().is_available("msg") {
|
||||
diagnostic.set_fix(generate_fix(
|
||||
stmt,
|
||||
first,
|
||||
indentation,
|
||||
checker.stylist(),
|
||||
checker.locator(),
|
||||
));
|
||||
}
|
||||
diagnostic.set_fix(generate_fix(
|
||||
stmt,
|
||||
first,
|
||||
indentation,
|
||||
checker.stylist(),
|
||||
checker.locator(),
|
||||
));
|
||||
}
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
|
|
@ -59,15 +59,26 @@ EM.py:22:24: EM103 [*] Exception must not use a `.format()` string directly, ass
|
|||
24 25 |
|
||||
25 26 | def f_ok():
|
||||
|
||||
EM.py:32:24: EM101 Exception must not use a string literal, assign to variable first
|
||||
EM.py:32:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
||||
|
|
||||
30 | def f_unfixable():
|
||||
30 | def f_msg_defined():
|
||||
31 | msg = "hello"
|
||||
32 | raise RuntimeError("This is an example exception")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
||||
|
|
||||
= help: Assign to variable; remove string literal
|
||||
|
||||
ℹ Unsafe fix
|
||||
29 29 |
|
||||
30 30 | def f_msg_defined():
|
||||
31 31 | msg = "hello"
|
||||
32 |- raise RuntimeError("This is an example exception")
|
||||
32 |+ msg = "This is an example exception"
|
||||
33 |+ raise RuntimeError(msg)
|
||||
33 34 |
|
||||
34 35 |
|
||||
35 36 | def f_msg_in_nested_scope():
|
||||
|
||||
EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
||||
|
|
||||
37 | msg = "hello"
|
||||
|
@ -88,7 +99,7 @@ EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variab
|
|||
41 42 |
|
||||
42 43 | def f_msg_in_parent_scope():
|
||||
|
||||
EM.py:46:28: EM101 Exception must not use a string literal, assign to variable first
|
||||
EM.py:46:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
||||
|
|
||||
45 | def nested():
|
||||
46 | raise RuntimeError("This is an example exception")
|
||||
|
@ -96,6 +107,17 @@ EM.py:46:28: EM101 Exception must not use a string literal, assign to variable f
|
|||
|
|
||||
= help: Assign to variable; remove string literal
|
||||
|
||||
ℹ Unsafe fix
|
||||
43 43 | msg = "hello"
|
||||
44 44 |
|
||||
45 45 | def nested():
|
||||
46 |- raise RuntimeError("This is an example exception")
|
||||
46 |+ msg = "This is an example exception"
|
||||
47 |+ raise RuntimeError(msg)
|
||||
47 48 |
|
||||
48 49 |
|
||||
49 50 | def f_fix_indentation_check(foo):
|
||||
|
||||
EM.py:51:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
||||
|
|
||||
49 | def f_fix_indentation_check(foo):
|
||||
|
|
|
@ -97,15 +97,26 @@ EM.py:22:24: EM103 [*] Exception must not use a `.format()` string directly, ass
|
|||
24 25 |
|
||||
25 26 | def f_ok():
|
||||
|
||||
EM.py:32:24: EM101 Exception must not use a string literal, assign to variable first
|
||||
EM.py:32:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
||||
|
|
||||
30 | def f_unfixable():
|
||||
30 | def f_msg_defined():
|
||||
31 | msg = "hello"
|
||||
32 | raise RuntimeError("This is an example exception")
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
||||
|
|
||||
= help: Assign to variable; remove string literal
|
||||
|
||||
ℹ Unsafe fix
|
||||
29 29 |
|
||||
30 30 | def f_msg_defined():
|
||||
31 31 | msg = "hello"
|
||||
32 |- raise RuntimeError("This is an example exception")
|
||||
32 |+ msg = "This is an example exception"
|
||||
33 |+ raise RuntimeError(msg)
|
||||
33 34 |
|
||||
34 35 |
|
||||
35 36 | def f_msg_in_nested_scope():
|
||||
|
||||
EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
||||
|
|
||||
37 | msg = "hello"
|
||||
|
@ -126,7 +137,7 @@ EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variab
|
|||
41 42 |
|
||||
42 43 | def f_msg_in_parent_scope():
|
||||
|
||||
EM.py:46:28: EM101 Exception must not use a string literal, assign to variable first
|
||||
EM.py:46:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
||||
|
|
||||
45 | def nested():
|
||||
46 | raise RuntimeError("This is an example exception")
|
||||
|
@ -134,6 +145,17 @@ EM.py:46:28: EM101 Exception must not use a string literal, assign to variable f
|
|||
|
|
||||
= help: Assign to variable; remove string literal
|
||||
|
||||
ℹ Unsafe fix
|
||||
43 43 | msg = "hello"
|
||||
44 44 |
|
||||
45 45 | def nested():
|
||||
46 |- raise RuntimeError("This is an example exception")
|
||||
46 |+ msg = "This is an example exception"
|
||||
47 |+ raise RuntimeError(msg)
|
||||
47 48 |
|
||||
48 49 |
|
||||
49 50 | def f_fix_indentation_check(foo):
|
||||
|
||||
EM.py:51:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
||||
|
|
||||
49 | def f_fix_indentation_check(foo):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue