This commit is contained in:
hauntsaninja 2025-12-12 17:18:15 -08:00
parent 148831199c
commit 46ce1efff3
3 changed files with 135 additions and 5 deletions

View File

@ -26,6 +26,24 @@ facts = {
), ),
} }
facts = (
"Octopuses have three hearts."
# Missing comma here.
"Honey never spoils.",
)
facts = [
"Octopuses have three hearts."
# Missing comma here.
"Honey never spoils.",
]
facts = {
"Octopuses have three hearts."
# Missing comma here.
"Honey never spoils.",
}
facts = ( facts = (
( (
"Clarinets are made almost entirely out of wood from the mpingo tree." "Clarinets are made almost entirely out of wood from the mpingo tree."

View File

@ -4,7 +4,7 @@ use ruff_python_ast::{Expr, StringLike};
use ruff_text_size::Ranged; use ruff_text_size::Ranged;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::Violation; use crate::{Edit, Fix, FixAvailability, Violation};
/// ## What it does /// ## What it does
/// Checks for implicitly concatenated strings inside list, tuple, and set literals. /// Checks for implicitly concatenated strings inside list, tuple, and set literals.
@ -50,10 +50,16 @@ use crate::Violation;
pub(crate) struct ImplicitStringConcatenationInCollectionLiteral; pub(crate) struct ImplicitStringConcatenationInCollectionLiteral;
impl Violation for ImplicitStringConcatenationInCollectionLiteral { impl Violation for ImplicitStringConcatenationInCollectionLiteral {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Always;
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
"Implicit string concatenation in collection literal; did you forget a comma?".to_string() "Implicit string concatenation in collection literal; did you forget a comma?".to_string()
} }
fn fix_title(&self) -> Option<String> {
Some("Wrap implicitly concatenated strings in parentheses".to_string())
}
} }
/// ISC004 /// ISC004
@ -75,9 +81,13 @@ pub(crate) fn implicit_string_concatenation_in_collection_literal(
continue; continue;
} }
checker.report_diagnostic( let mut diagnostic = checker.report_diagnostic(
ImplicitStringConcatenationInCollectionLiteral, ImplicitStringConcatenationInCollectionLiteral,
string_like.range(), string_like.range(),
); );
diagnostic.set_fix(Fix::safe_edits(
Edit::insertion("(".to_string(), string_like.range().start()),
[Edit::insertion(")".to_string(), string_like.range().end())],
));
} }
} }

View File

@ -1,7 +1,7 @@
--- ---
source: crates/ruff_linter/src/rules/flake8_implicit_str_concat/mod.rs source: crates/ruff_linter/src/rules/flake8_implicit_str_concat/mod.rs
--- ---
ISC004 Implicit string concatenation in collection literal; did you forget a comma? ISC004 [*] Implicit string concatenation in collection literal; did you forget a comma?
--> ISC004.py:4:5 --> ISC004.py:4:5
| |
2 | "Lobsters have blue blood.", 2 | "Lobsters have blue blood.",
@ -11,8 +11,19 @@ ISC004 Implicit string concatenation in collection literal; did you forget a com
| |______________________________________________________________^ | |______________________________________________________________^
6 | ) 6 | )
| |
help: Wrap implicitly concatenated strings in parentheses
1 | facts = (
2 | "Lobsters have blue blood.",
3 | "The liver is the only human organ that can fully regenerate itself.",
- "Clarinets are made almost entirely out of wood from the mpingo tree."
- "In 1971, astronaut Alan Shepard played golf on the moon.",
4 + ("Clarinets are made almost entirely out of wood from the mpingo tree."
5 + "In 1971, astronaut Alan Shepard played golf on the moon."),
6 | )
7 |
8 | facts = [
ISC004 Implicit string concatenation in collection literal; did you forget a comma? ISC004 [*] Implicit string concatenation in collection literal; did you forget a comma?
--> ISC004.py:11:5 --> ISC004.py:11:5
| |
9 | "Lobsters have blue blood.", 9 | "Lobsters have blue blood.",
@ -22,8 +33,19 @@ ISC004 Implicit string concatenation in collection literal; did you forget a com
| |______________________________________________________________^ | |______________________________________________________________^
13 | ] 13 | ]
| |
help: Wrap implicitly concatenated strings in parentheses
8 | facts = [
9 | "Lobsters have blue blood.",
10 | "The liver is the only human organ that can fully regenerate itself.",
- "Clarinets are made almost entirely out of wood from the mpingo tree."
- "In 1971, astronaut Alan Shepard played golf on the moon.",
11 + ("Clarinets are made almost entirely out of wood from the mpingo tree."
12 + "In 1971, astronaut Alan Shepard played golf on the moon."),
13 | ]
14 |
15 | facts = {
ISC004 Implicit string concatenation in collection literal; did you forget a comma? ISC004 [*] Implicit string concatenation in collection literal; did you forget a comma?
--> ISC004.py:18:5 --> ISC004.py:18:5
| |
16 | "Lobsters have blue blood.", 16 | "Lobsters have blue blood.",
@ -33,3 +55,83 @@ ISC004 Implicit string concatenation in collection literal; did you forget a com
| |______________________________________________________________^ | |______________________________________________________________^
20 | } 20 | }
| |
help: Wrap implicitly concatenated strings in parentheses
15 | facts = {
16 | "Lobsters have blue blood.",
17 | "The liver is the only human organ that can fully regenerate itself.",
- "Clarinets are made almost entirely out of wood from the mpingo tree."
- "In 1971, astronaut Alan Shepard played golf on the moon.",
18 + ("Clarinets are made almost entirely out of wood from the mpingo tree."
19 + "In 1971, astronaut Alan Shepard played golf on the moon."),
20 | }
21 |
22 | facts = {
ISC004 [*] Implicit string concatenation in collection literal; did you forget a comma?
--> ISC004.py:30:5
|
29 | facts = (
30 | / "Octopuses have three hearts."
31 | | # Missing comma here.
32 | | "Honey never spoils.",
| |_________________________^
33 | )
|
help: Wrap implicitly concatenated strings in parentheses
27 | }
28 |
29 | facts = (
- "Octopuses have three hearts."
30 + ("Octopuses have three hearts."
31 | # Missing comma here.
- "Honey never spoils.",
32 + "Honey never spoils."),
33 | )
34 |
35 | facts = [
ISC004 [*] Implicit string concatenation in collection literal; did you forget a comma?
--> ISC004.py:36:5
|
35 | facts = [
36 | / "Octopuses have three hearts."
37 | | # Missing comma here.
38 | | "Honey never spoils.",
| |_________________________^
39 | ]
|
help: Wrap implicitly concatenated strings in parentheses
33 | )
34 |
35 | facts = [
- "Octopuses have three hearts."
36 + ("Octopuses have three hearts."
37 | # Missing comma here.
- "Honey never spoils.",
38 + "Honey never spoils."),
39 | ]
40 |
41 | facts = {
ISC004 [*] Implicit string concatenation in collection literal; did you forget a comma?
--> ISC004.py:42:5
|
41 | facts = {
42 | / "Octopuses have three hearts."
43 | | # Missing comma here.
44 | | "Honey never spoils.",
| |_________________________^
45 | }
|
help: Wrap implicitly concatenated strings in parentheses
39 | ]
40 |
41 | facts = {
- "Octopuses have three hearts."
42 + ("Octopuses have three hearts."
43 | # Missing comma here.
- "Honey never spoils.",
44 + "Honey never spoils."),
45 | }
46 |
47 | facts = (