Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,20 @@
"abc"
"def"
) + "ghi"


# https://github.com/astral-sh/ruff/issues/26541
x = (
"A" # Complains about this: +
+ " broken"
+ " line"
+ " string."
)

foo(
bar="A" # Complains about this: +
+ " broken"
+ " line"
+ " string."
)

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::token::parenthesized_range;
use ruff_python_ast::token::{TokenKind, parenthesized_range};
use ruff_python_ast::{self as ast, Expr, Operator};
use ruff_python_trivia::is_python_whitespace;
use ruff_source_file::LineRanges;
Expand Down Expand Up @@ -116,7 +116,21 @@ fn generate_fix(checker: &Checker, expr_bin_op: &ast::ExprBinOp) -> Fix {

let between_operands_range = TextRange::new(left.end(), right.start());
let between_operands = checker.locator().slice(between_operands_range);
let (before_plus, after_plus) = between_operands.split_once('+').unwrap();

let plus_token = checker
.tokens()
.in_range(between_operands_range)
.iter()
.find(|token| token.kind() == TokenKind::Plus);

let (before_plus, after_plus) = if let Some(plus_token) = plus_token {
let plus_relative_start = plus_token.start() - left.end();
let (before, after) = between_operands.split_at(plus_relative_start.to_usize());
(before, &after[1..])
} else {
// Fallback in case of unexpected token structure
between_operands.split_once('+').unwrap()
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this fallback. It's as likely to panic as if we can't find the token in the token stream.


let linebreak_before_operator =
before_plus.contains_line_break(TextRange::at(TextSize::new(0), before_plus.text_len()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,41 @@ ISC003 Explicitly concatenated string should be implicitly concatenated
| |_________^
|
help: Remove redundant '+' operator to implicitly concatenate

ISC003 [*] Explicitly concatenated string should be implicitly concatenated
--> ISC.py:229:5
|
227 | # https://github.com/astral-sh/ruff/issues/26541
228 | x = (
229 | / "A" # Complains about this: +
230 | | + " broken"
| |_______________^
231 | + " line"
232 | + " string."
|
help: Remove redundant '+' operator to implicitly concatenate
|
229 | "A" # Complains about this: +
- + " broken"
230 + " broken"
231 | + " line"
|

ISC003 [*] Explicitly concatenated string should be implicitly concatenated
--> ISC.py:236:9
|
235 | foo(
236 | bar="A" # Complains about this: +
| _________^
237 | | + " broken"
| |_______________^
238 | + " line"
239 | + " string."
|
help: Remove redundant '+' operator to implicitly concatenate
|
236 | bar="A" # Complains about this: +
- + " broken"
237 + " broken"
238 | + " line"
|
Loading