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:
Dhruv Manilawala 2023-12-08 15:16:15 -06:00 committed by GitHub
parent e043bd46b5
commit b7dd2b5941
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 34 deletions

View file

@ -27,7 +27,7 @@ def f_ok():
raise RuntimeError(msg) raise RuntimeError(msg)
def f_unfixable(): def f_msg_defined():
msg = "hello" msg = "hello"
raise RuntimeError("This is an example exception") raise RuntimeError("This is an example exception")

View file

@ -191,15 +191,13 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
if let Some(indentation) = if let Some(indentation) =
whitespace::indentation(checker.locator(), stmt) whitespace::indentation(checker.locator(), stmt)
{ {
if checker.semantic().is_available("msg") { diagnostic.set_fix(generate_fix(
diagnostic.set_fix(generate_fix( stmt,
stmt, first,
first, indentation,
indentation, checker.stylist(),
checker.stylist(), checker.locator(),
checker.locator(), ));
));
}
} }
checker.diagnostics.push(diagnostic); 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()); let mut diagnostic = Diagnostic::new(FStringInException, first.range());
if let Some(indentation) = whitespace::indentation(checker.locator(), stmt) if let Some(indentation) = whitespace::indentation(checker.locator(), stmt)
{ {
if checker.semantic().is_available("msg") { diagnostic.set_fix(generate_fix(
diagnostic.set_fix(generate_fix( stmt,
stmt, first,
first, indentation,
indentation, checker.stylist(),
checker.stylist(), checker.locator(),
checker.locator(), ));
));
}
} }
checker.diagnostics.push(diagnostic); 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) = if let Some(indentation) =
whitespace::indentation(checker.locator(), stmt) whitespace::indentation(checker.locator(), stmt)
{ {
if checker.semantic().is_available("msg") { diagnostic.set_fix(generate_fix(
diagnostic.set_fix(generate_fix( stmt,
stmt, first,
first, indentation,
indentation, checker.stylist(),
checker.stylist(), checker.locator(),
checker.locator(), ));
));
}
} }
checker.diagnostics.push(diagnostic); checker.diagnostics.push(diagnostic);
} }

View file

@ -59,15 +59,26 @@ EM.py:22:24: EM103 [*] Exception must not use a `.format()` string directly, ass
24 25 | 24 25 |
25 26 | def f_ok(): 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" 31 | msg = "hello"
32 | raise RuntimeError("This is an example exception") 32 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
| |
= help: Assign to variable; remove string literal = 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 EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variable first
| |
37 | msg = "hello" 37 | msg = "hello"
@ -88,7 +99,7 @@ EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variab
41 42 | 41 42 |
42 43 | def f_msg_in_parent_scope(): 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(): 45 | def nested():
46 | raise RuntimeError("This is an example exception") 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 = 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 EM.py:51:28: EM101 [*] Exception must not use a string literal, assign to variable first
| |
49 | def f_fix_indentation_check(foo): 49 | def f_fix_indentation_check(foo):

View file

@ -97,15 +97,26 @@ EM.py:22:24: EM103 [*] Exception must not use a `.format()` string directly, ass
24 25 | 24 25 |
25 26 | def f_ok(): 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" 31 | msg = "hello"
32 | raise RuntimeError("This is an example exception") 32 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
| |
= help: Assign to variable; remove string literal = 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 EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variable first
| |
37 | msg = "hello" 37 | msg = "hello"
@ -126,7 +137,7 @@ EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variab
41 42 | 41 42 |
42 43 | def f_msg_in_parent_scope(): 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(): 45 | def nested():
46 | raise RuntimeError("This is an example exception") 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 = 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 EM.py:51:28: EM101 [*] Exception must not use a string literal, assign to variable first
| |
49 | def f_fix_indentation_check(foo): 49 | def f_fix_indentation_check(foo):