Skip to content

Commit

Permalink
Use locator.slice(range) over locator.contents()[range] (#7759)
Browse files Browse the repository at this point in the history
**Summary** Refactoring inspired by
#7741 (comment)
  • Loading branch information
konstin committed Oct 2, 2023
1 parent f70e8a7 commit 13748dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
8 changes: 7 additions & 1 deletion crates/ruff_linter/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ fn delete_noqa(range: TextRange, locator: &Locator) -> Edit {
Edit::deletion(range.start() - leading_space_len, line_range.end())
}
// Ex) `x = 1 # noqa # type: ignore`
else if locator.contents()[usize::from(range.end() + trailing_space_len)..].starts_with('#') {
else if locator
.slice(TextRange::new(
range.end() + trailing_space_len,
line_range.end(),
))
.starts_with('#')
{
Edit::deletion(range.start(), range.end() + trailing_space_len)
}
// Ex) `x = 1 # noqa here`
Expand Down
12 changes: 10 additions & 2 deletions crates/ruff_linter/src/rules/flake8_simplify/rules/ast_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,11 @@ pub(crate) fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt) {
// Don't flag if the resulting expression would exceed the maximum line length.
let line_start = checker.locator().line_start(stmt.start());
if LineWidthBuilder::new(checker.settings.tab_size)
.add_str(&checker.locator().contents()[TextRange::new(line_start, stmt.start())])
.add_str(
checker
.locator()
.slice(TextRange::new(line_start, stmt.start())),
)
.add_str(&contents)
> checker.settings.line_length
{
Expand Down Expand Up @@ -965,7 +969,11 @@ pub(crate) fn use_dict_get_with_default(checker: &mut Checker, stmt_if: &ast::St
// Don't flag if the resulting expression would exceed the maximum line length.
let line_start = checker.locator().line_start(stmt_if.start());
if LineWidthBuilder::new(checker.settings.tab_size)
.add_str(&checker.locator().contents()[TextRange::new(line_start, stmt_if.start())])
.add_str(
checker
.locator()
.slice(TextRange::new(line_start, stmt_if.start())),
)
.add_str(&contents)
> checker.settings.line_length
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
// Don't flag if the resulting expression would exceed the maximum line length.
let line_start = checker.locator().line_start(stmt.start());
if LineWidthBuilder::new(checker.settings.tab_size)
.add_str(&checker.locator().contents()[TextRange::new(line_start, stmt.start())])
.add_str(
checker
.locator()
.slice(TextRange::new(line_start, stmt.start())),
)
.add_str(&contents)
> checker.settings.line_length
{
Expand Down Expand Up @@ -181,7 +185,11 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
// Don't flag if the resulting expression would exceed the maximum line length.
let line_start = checker.locator().line_start(stmt.start());
if LineWidthBuilder::new(checker.settings.tab_size)
.add_str(&checker.locator().contents()[TextRange::new(line_start, stmt.start())])
.add_str(
checker
.locator()
.slice(TextRange::new(line_start, stmt.start())),
)
.add_str(&contents)
> checker.settings.line_length
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ fn fix_always_false_branch(
..
}) => {
debug_assert!(
&checker.locator().contents()[TextRange::at(range.start(), "elif".text_len())]
checker
.locator()
.slice(TextRange::at(range.start(), "elif".text_len()))
== "elif"
);
let end_location = range.start() + ("elif".text_len() - "if".text_len());
Expand Down

0 comments on commit 13748dd

Please sign in to comment.