mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-26 22:43:47 +00:00
Mark RUF005 as fixable
This commit is contained in:
parent
125615af12
commit
ff3665a24b
3 changed files with 42 additions and 12 deletions
|
@ -1422,7 +1422,7 @@ For more, see [flake8-self](https://pypi.org/project/flake8-self/) on PyPI.
|
||||||
| RUF002 | ambiguous-unicode-character-docstring | Docstring contains ambiguous unicode character '{confusable}' (did you mean '{representant}'?) | 🛠 |
|
| RUF002 | ambiguous-unicode-character-docstring | Docstring contains ambiguous unicode character '{confusable}' (did you mean '{representant}'?) | 🛠 |
|
||||||
| RUF003 | ambiguous-unicode-character-comment | Comment contains ambiguous unicode character '{confusable}' (did you mean '{representant}'?) | 🛠 |
|
| RUF003 | ambiguous-unicode-character-comment | Comment contains ambiguous unicode character '{confusable}' (did you mean '{representant}'?) | 🛠 |
|
||||||
| RUF004 | keyword-argument-before-star-argument | Keyword argument `{name}` must come after starred arguments | |
|
| RUF004 | keyword-argument-before-star-argument | Keyword argument `{name}` must come after starred arguments | |
|
||||||
| RUF005 | unpack-instead-of-concatenating-to-collection-literal | Consider `{expr}` instead of concatenation | |
|
| RUF005 | unpack-instead-of-concatenating-to-collection-literal | Consider `{expr}` instead of concatenation | 🛠 |
|
||||||
| RUF100 | unused-noqa | Unused blanket `noqa` directive | 🛠 |
|
| RUF100 | unused-noqa | Unused blanket `noqa` directive | 🛠 |
|
||||||
|
|
||||||
<!-- End auto-generated sections. -->
|
<!-- End auto-generated sections. -->
|
||||||
|
|
|
@ -6,19 +6,36 @@ use crate::ast::types::Range;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::violation::Violation;
|
use crate::violation::{Availability, Violation};
|
||||||
|
use crate::AutofixKind;
|
||||||
|
|
||||||
define_violation!(
|
define_violation!(
|
||||||
pub struct UnpackInsteadOfConcatenatingToCollectionLiteral {
|
pub struct UnpackInsteadOfConcatenatingToCollectionLiteral {
|
||||||
pub expr: String,
|
pub expr: String,
|
||||||
|
pub fixable: bool,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
impl Violation for UnpackInsteadOfConcatenatingToCollectionLiteral {
|
impl Violation for UnpackInsteadOfConcatenatingToCollectionLiteral {
|
||||||
|
const AUTOFIX: Option<AutofixKind> = Some(AutofixKind::new(Availability::Sometimes));
|
||||||
|
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
let UnpackInsteadOfConcatenatingToCollectionLiteral { expr } = self;
|
let UnpackInsteadOfConcatenatingToCollectionLiteral { expr, .. } = self;
|
||||||
format!("Consider `{expr}` instead of concatenation")
|
format!("Consider `{expr}` instead of concatenation")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
|
||||||
|
let UnpackInsteadOfConcatenatingToCollectionLiteral { fixable, .. } = self;
|
||||||
|
if *fixable {
|
||||||
|
Some(
|
||||||
|
|UnpackInsteadOfConcatenatingToCollectionLiteral { expr, .. }| {
|
||||||
|
format!("Replace with `{expr}`")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_splat_elts(
|
fn make_splat_elts(
|
||||||
|
@ -86,24 +103,24 @@ pub fn unpack_instead_of_concatenating_to_collection_literal(checker: &mut Check
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut new_expr_string = unparse_expr(&new_expr, checker.stylist);
|
let contents = match kind {
|
||||||
|
|
||||||
new_expr_string = match kind {
|
|
||||||
// Wrap the new expression in parentheses if it was a tuple
|
// Wrap the new expression in parentheses if it was a tuple
|
||||||
Kind::Tuple => format!("({new_expr_string})"),
|
Kind::Tuple => format!("({})", unparse_expr(&new_expr, checker.stylist)),
|
||||||
Kind::List => new_expr_string,
|
Kind::List => unparse_expr(&new_expr, checker.stylist),
|
||||||
};
|
};
|
||||||
|
let fixable = !has_comments(expr, checker.locator);
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral {
|
UnpackInsteadOfConcatenatingToCollectionLiteral {
|
||||||
expr: new_expr_string.clone(),
|
expr: contents.clone(),
|
||||||
|
fixable,
|
||||||
},
|
},
|
||||||
Range::from_located(expr),
|
Range::from_located(expr),
|
||||||
);
|
);
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
if !has_comments(expr, checker.locator) {
|
if fixable {
|
||||||
diagnostic.amend(Fix::replacement(
|
diagnostic.amend(Fix::replacement(
|
||||||
new_expr_string,
|
contents,
|
||||||
expr.location,
|
expr.location,
|
||||||
expr.end_location.unwrap(),
|
expr.end_location.unwrap(),
|
||||||
));
|
));
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
---
|
---
|
||||||
source: src/rules/ruff/mod.rs
|
source: crates/ruff/src/rules/ruff/mod.rs
|
||||||
expression: diagnostics
|
expression: diagnostics
|
||||||
---
|
---
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "[1, 2, 3, *foo]"
|
expr: "[1, 2, 3, *foo]"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 10
|
row: 10
|
||||||
column: 6
|
column: 6
|
||||||
|
@ -24,6 +25,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "(7, 8, 9, *zoob)"
|
expr: "(7, 8, 9, *zoob)"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 12
|
row: 12
|
||||||
column: 7
|
column: 7
|
||||||
|
@ -43,6 +45,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "(*quux, 10, 11, 12)"
|
expr: "(*quux, 10, 11, 12)"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 13
|
row: 13
|
||||||
column: 7
|
column: 7
|
||||||
|
@ -62,6 +65,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "[*spom, 13, 14, 15]"
|
expr: "[*spom, 13, 14, 15]"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 15
|
row: 15
|
||||||
column: 7
|
column: 7
|
||||||
|
@ -81,6 +85,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "(\"we all say\", *yay())"
|
expr: "(\"we all say\", *yay())"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 16
|
row: 16
|
||||||
column: 12
|
column: 12
|
||||||
|
@ -100,6 +105,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "(\"we all think\", *Fun().yay())"
|
expr: "(\"we all think\", *Fun().yay())"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 17
|
row: 17
|
||||||
column: 13
|
column: 13
|
||||||
|
@ -119,6 +125,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "(\"we all feel\", *Fun.words)"
|
expr: "(\"we all feel\", *Fun.words)"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 18
|
row: 18
|
||||||
column: 15
|
column: 15
|
||||||
|
@ -138,6 +145,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "[\"a\", \"b\", \"c\", *eggs]"
|
expr: "[\"a\", \"b\", \"c\", *eggs]"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 20
|
row: 20
|
||||||
column: 8
|
column: 8
|
||||||
|
@ -157,6 +165,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "(\"yes\", \"no\", \"pants\", *zoob)"
|
expr: "(\"yes\", \"no\", \"pants\", *zoob)"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 20
|
row: 20
|
||||||
column: 38
|
column: 38
|
||||||
|
@ -176,6 +185,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "(*zoob,)"
|
expr: "(*zoob,)"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 22
|
row: 22
|
||||||
column: 6
|
column: 6
|
||||||
|
@ -195,6 +205,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "[*first, 4, 5, 6]"
|
expr: "[*first, 4, 5, 6]"
|
||||||
|
fixable: false
|
||||||
location:
|
location:
|
||||||
row: 32
|
row: 32
|
||||||
column: 9
|
column: 9
|
||||||
|
@ -206,6 +217,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "[*foo]"
|
expr: "[*foo]"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 41
|
row: 41
|
||||||
column: 0
|
column: 0
|
||||||
|
@ -225,6 +237,7 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
UnpackInsteadOfConcatenatingToCollectionLiteral:
|
||||||
expr: "[*foo]"
|
expr: "[*foo]"
|
||||||
|
fixable: true
|
||||||
location:
|
location:
|
||||||
row: 44
|
row: 44
|
||||||
column: 0
|
column: 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue