Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore explicit-string-concatenation on single line #6028

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Changes from 4 commits
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
@@ -50,3 +50,12 @@
_ = 'a' "b"

_ = rf"a" rf"b"

# Single-line explicit concatenation should be ignored.
_ = "abc" + "def" + "ghi"
_ = foo + "abc" + "def"
_ = "abc" + foo + "def"
_ = "abc" + "def" + foo
_ = foo + bar + "abc"
_ = "abc" + foo + bar
_ = foo + "abc" + bar
4 changes: 3 additions & 1 deletion crates/ruff/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
@@ -1056,7 +1056,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
op: Operator::Add, ..
}) => {
if checker.enabled(Rule::ExplicitStringConcatenation) {
if let Some(diagnostic) = flake8_implicit_str_concat::rules::explicit(expr) {
if let Some(diagnostic) =
flake8_implicit_str_concat::rules::explicit(expr, checker.locator)
{
checker.diagnostics.push(diagnostic);
}
}
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Operator, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::source_code::Locator;

/// ## What it does
/// Checks for string literals that are explicitly concatenated (using the
@@ -38,15 +39,15 @@ impl Violation for ExplicitStringConcatenation {
}

/// ISC003
pub(crate) fn explicit(expr: &Expr) -> Option<Diagnostic> {
pub(crate) fn explicit(expr: &Expr, locator: &Locator) -> Option<Diagnostic> {
if let Expr::BinOp(ast::ExprBinOp {
left,
op,
right,
range: _,
range,
}) = expr
{
if matches!(op, Operator::Add) {
if locator.contains_line_break(*range) && matches!(op, Operator::Add) {
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
if matches!(
left.as_ref(),
Expr::JoinedStr(_)
Original file line number Diff line number Diff line change
@@ -138,6 +138,8 @@ ISC.py:52:5: ISC001 [*] Implicitly concatenated string literals on one line
51 |
52 | _ = rf"a" rf"b"
| ^^^^^^^^^^^ ISC001
53 |
54 | # Single-line explicit concatenation should be ignored.
|
= help: Combine string literals

@@ -147,5 +149,8 @@ ISC.py:52:5: ISC001 [*] Implicitly concatenated string literals on one line
51 51 |
52 |-_ = rf"a" rf"b"
52 |+_ = rf"ab"
53 53 |
54 54 | # Single-line explicit concatenation should be ignored.
55 55 | _ = "abc" + "def" + "ghi"


Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
---
source: crates/ruff/src/rules/flake8_implicit_str_concat/mod.rs
---
ISC.py:3:5: ISC003 Explicitly concatenated string should be implicitly concatenated
|
1 | _ = "a" "b" "c"
2 |
3 | _ = "abc" + "def"
| ^^^^^^^^^^^^^ ISC003
4 |
5 | _ = "abc" \
|

ISC.py:9:3: ISC003 Explicitly concatenated string should be implicitly concatenated
|
8 | _ = (
Original file line number Diff line number Diff line change
@@ -138,6 +138,8 @@ ISC.py:52:5: ISC001 [*] Implicitly concatenated string literals on one line
51 |
52 | _ = rf"a" rf"b"
| ^^^^^^^^^^^ ISC001
53 |
54 | # Single-line explicit concatenation should be ignored.
|
= help: Combine string literals

@@ -147,5 +149,8 @@ ISC.py:52:5: ISC001 [*] Implicitly concatenated string literals on one line
51 51 |
52 |-_ = rf"a" rf"b"
52 |+_ = rf"ab"
53 53 |
54 54 | # Single-line explicit concatenation should be ignored.
55 55 | _ = "abc" + "def" + "ghi"


Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
---
source: crates/ruff/src/rules/flake8_implicit_str_concat/mod.rs
---
ISC.py:3:5: ISC003 Explicitly concatenated string should be implicitly concatenated
|
1 | _ = "a" "b" "c"
2 |
3 | _ = "abc" + "def"
| ^^^^^^^^^^^^^ ISC003
4 |
5 | _ = "abc" \
|

ISC.py:9:3: ISC003 Explicitly concatenated string should be implicitly concatenated
|
8 | _ = (