Skip to content

Commit

Permalink
Slice source code instead of generating it for EM fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Nov 9, 2023
1 parent 7391f74 commit a61a608
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
12 changes: 12 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_errmsg/EM.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ def f_fix_indentation_check(foo):
# Report these, but don't fix them
if foo: raise RuntimeError("This is an example exception")
if foo: x = 1; raise RuntimeError("This is an example exception")


def f_triple_quoted_string():
raise RuntimeError(f"""This is an {"example"} exception""")


# Generate a violaiton for this, but don't fix it
def f_multi_line_string():
raise RuntimeError(
"first"
"second"
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use ruff_python_ast::{self as ast, Arguments, Expr, ExprContext, Stmt};
use ruff_text_size::{Ranged, TextRange};
use ruff_python_ast::{self as ast, Arguments, Expr, Stmt};
use ruff_source_file::Locator;
use ruff_text_size::Ranged;

use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::whitespace;
use ruff_python_codegen::{Generator, Stylist};
use ruff_python_codegen::Stylist;

use crate::checkers::ast::Checker;
use crate::registry::Rule;
Expand Down Expand Up @@ -190,13 +191,15 @@ 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") {
if checker.semantic().is_available("msg")
&& !checker.locator().contains_line_break(first.range())
{
diagnostic.set_fix(generate_fix(
stmt,
first,
indentation,
checker.stylist(),
checker.generator(),
checker.locator(),
));
}
}
Expand All @@ -216,7 +219,7 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
first,
indentation,
checker.stylist(),
checker.generator(),
checker.locator(),
));
}
}
Expand All @@ -235,13 +238,15 @@ 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") {
if checker.semantic().is_available("msg")
&& !checker.locator().contains_line_break(first.range())
{
diagnostic.set_fix(generate_fix(
stmt,
first,
indentation,
checker.stylist(),
checker.generator(),
checker.locator(),
));
}
}
Expand Down Expand Up @@ -271,23 +276,13 @@ fn generate_fix(
exc_arg: &Expr,
indentation: &str,
stylist: &Stylist,
generator: Generator,
locator: &Locator,
) -> Fix {
let assignment = Stmt::Assign(ast::StmtAssign {
targets: vec![Expr::Name(ast::ExprName {
id: "msg".into(),
ctx: ExprContext::Store,
range: TextRange::default(),
})],
value: Box::new(exc_arg.clone()),
range: TextRange::default(),
});

Fix::unsafe_edits(
Edit::insertion(
format!(
"{}{}{}",
generator.stmt(&assignment),
"msg = {}{}{}",
locator.slice(exc_arg.range()),
stylist.line_ending().as_str(),
indentation,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,23 @@ EM.py:60:35: EM101 Exception must not use a string literal, assign to variable f
|
= help: Assign to variable; remove string literal

EM.py:64:24: EM102 [*] Exception must not use an f-string literal, assign to variable first
|
63 | def f_triple_quoted_string():
64 | raise RuntimeError(f"""This is an {"example"} exception""")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM102
|
= help: Assign to variable; remove f-string literal

Unsafe fix
61 61 |
62 62 |
63 63 | def f_triple_quoted_string():
64 |- raise RuntimeError(f"""This is an {"example"} exception""")
64 |+ msg = f"""This is an {"example"} exception"""
65 |+ raise RuntimeError(msg)
65 66 |
66 67 |
67 68 | # Generate a violaiton for this, but don't fix it


Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,35 @@ EM.py:60:35: EM101 Exception must not use a string literal, assign to variable f
|
= help: Assign to variable; remove string literal

EM.py:64:24: EM102 [*] Exception must not use an f-string literal, assign to variable first
|
63 | def f_triple_quoted_string():
64 | raise RuntimeError(f"""This is an {"example"} exception""")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM102
|
= help: Assign to variable; remove f-string literal

Unsafe fix
61 61 |
62 62 |
63 63 | def f_triple_quoted_string():
64 |- raise RuntimeError(f"""This is an {"example"} exception""")
64 |+ msg = f"""This is an {"example"} exception"""
65 |+ raise RuntimeError(msg)
65 66 |
66 67 |
67 68 | # Generate a violaiton for this, but don't fix it

EM.py:70:9: EM101 Exception must not use a string literal, assign to variable first
|
68 | def f_multi_line_string():
69 | raise RuntimeError(
70 | "first"
| _________^
71 | | "second"
| |________________^ EM101
72 | )
|
= help: Assign to variable; remove string literal


0 comments on commit a61a608

Please sign in to comment.