mirror of
https://github.com/astral-sh/ruff.git
synced 2025-12-04 01:36:46 +00:00
Ignore explicit-string-concatenation on single line (#6028)
## Summary Ignore `explicit-string-concatenation` on single line. Closes #5332. ## Test Plan `cargo test`
This commit is contained in:
parent
8c80bfa7da
commit
da33c26238
7 changed files with 27 additions and 24 deletions
|
|
@ -50,3 +50,12 @@ _ = """a""" "b"
|
|||
_ = '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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,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) {
|
||||
|
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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 | _ = (
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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 | _ = (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue