Skip to content

Commit

Permalink
Ignore explicit-string-concatenation on single line (#6028)
Browse files Browse the repository at this point in the history
## Summary

Ignore `explicit-string-concatenation` on single line.

Closes #5332.

## Test Plan

`cargo test`
  • Loading branch information
tjkuson authored Jul 25, 2023
1 parent 8c80bfa commit da33c26
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,12 +39,12 @@ 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) {
Expand All @@ -61,7 +62,8 @@ pub(crate) fn explicit(expr: &Expr) -> Option<Diagnostic> {
value: Constant::Str(..) | Constant::Bytes(..),
..
})
) {
) && locator.contains_line_break(*range)
{
return Some(Diagnostic::new(ExplicitStringConcatenation, expr.range()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 | _ = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 | _ = (
Expand Down

0 comments on commit da33c26

Please sign in to comment.