Skip to content

Commit

Permalink
Add parenthesized flag to ExprTuple and ExprGenerator (#9614)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Feb 26, 2024
1 parent ab4bd71 commit 77c5561
Show file tree
Hide file tree
Showing 65 changed files with 391 additions and 139 deletions.
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
elts,
ctx,
range: _,
parenthesized: _,
})
| Expr::List(ast::ExprList {
elts,
Expand Down Expand Up @@ -1451,6 +1452,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
generators,
elt: _,
range: _,
parenthesized: _,
},
) => {
if checker.enabled(Rule::UnnecessaryListIndexLookup) {
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ where
elt,
generators,
range: _,
parenthesized: _,
}) => {
self.visit_generators(generators);
self.visit_expr(elt);
Expand Down Expand Up @@ -1327,6 +1328,7 @@ where
elts,
ctx,
range: _,
parenthesized: _,
}) = slice.as_ref()
{
let mut iter = elts.iter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ fn type_pattern(elts: Vec<&Expr>) -> Expr {
elts: elts.into_iter().cloned().collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
}
.into()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub(crate) fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) {
.collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
});
let node1 = Expr::Name(ast::ExprName {
id: arg_name.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp
elts,
range: _,
ctx: _,
parenthesized: _,
}) = slice.as_ref()
{
for expr in elts {
Expand Down Expand Up @@ -123,6 +124,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp
elts: literal_exprs.into_iter().cloned().collect(),
range: TextRange::default(),
ctx: ExprContext::Load,
parenthesized: true,
})),
range: TextRange::default(),
ctx: ExprContext::Load,
Expand All @@ -148,6 +150,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp
elts,
range: TextRange::default(),
ctx: ExprContext::Load,
parenthesized: true,
})),
range: TextRange::default(),
ctx: ExprContext::Load,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
.collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
})),
ctx: ExprContext::Load,
range: TextRange::default(),
Expand All @@ -151,6 +152,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
elts: exprs.into_iter().cloned().collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
})),
ctx: ExprContext::Load,
range: TextRange::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
.collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
});
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
format!("({})", checker.generator().expr(&node)),
Expand Down Expand Up @@ -444,6 +445,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
elts: elts.clone(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
});
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
format!("({})", checker.generator().expr(&node)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ pub(crate) fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) {
.collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
};
let node1 = ast::ExprName {
id: "isinstance".into(),
Expand Down Expand Up @@ -543,6 +544,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
elts: comparators.into_iter().map(Clone::clone).collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
};
let node1 = ast::ExprName {
id: id.into(),
Expand Down Expand Up @@ -718,7 +720,7 @@ fn get_short_circuit_edit(
generator.expr(expr)
};
Edit::range_replacement(
if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _}) if !elts.is_empty())
if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _, parenthesized: _}) if !elts.is_empty())
{
format!("({content})")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ fn return_stmt(id: &str, test: &Expr, target: &Expr, iter: &Expr, generator: Gen
range: TextRange::default(),
}],
range: TextRange::default(),
parenthesized: false,
};
let node1 = ast::ExprName {
id: id.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ fn assignment_targets_from_expr<'a>(
ctx: ExprContext::Store,
elts,
range: _,
parenthesized: _,
}) => Box::new(
elts.iter()
.flat_map(|elt| assignment_targets_from_expr(elt, dummy_variable_rgx)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast:
elts: comparators.iter().copied().cloned().collect(),
range: TextRange::default(),
ctx: ExprContext::Load,
parenthesized: true,
})]),
range: bool_op.range(),
})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E
elts: remaining,
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
};
format!("({})", checker.generator().expr(&node.into()))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E
elts: remaining,
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
};
format!("({})", checker.generator().expr(&node.into()))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub(crate) fn non_pep695_type_alias(checker: &mut Checker, stmt: &StmtAnnAssign)
range: TextRange::default(),
elts: constraints.into_iter().cloned().collect(),
ctx: ast::ExprContext::Load,
parenthesized: true,
})))
}
None => None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ fn make_suggestion(group: &AppendGroup, generator: Generator) -> String {
elts,
ctx: ast::ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
};
// Make `var.extend`.
// NOTE: receiver is the same for all appends and that's why we can take the first.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> {
elts: new_elts,
ctx: ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
}
.into(),
};
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/ruff/rules/never_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) {
elts,
ctx: _,
range: _,
parenthesized: _,
}) = slice.as_ref()
else {
return;
Expand Down Expand Up @@ -157,6 +158,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) {
elts: rest,
ctx: ast::ExprContext::Load,
range: TextRange::default(),
parenthesized: true,
})),
ctx: ast::ExprContext::Load,
range: TextRange::default(),
Expand Down
30 changes: 15 additions & 15 deletions crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,23 @@ impl InferredMemberType {
/// single-line tuple literals *can* be unparenthesized.
/// We keep the original AST node around for the
/// Tuple variant so that this can be queried later.
#[derive(Debug)]
pub(super) enum SequenceKind<'a> {
#[derive(Copy, Clone, Debug)]
pub(super) enum SequenceKind {
List,
Set,
Tuple(&'a ast::ExprTuple),
Tuple { parenthesized: bool },
}

impl SequenceKind<'_> {
impl SequenceKind {
// N.B. We only need the source code for the Tuple variant here,
// but if you already have a `Locator` instance handy,
// getting the source code is very cheap.
fn surrounding_brackets(&self, source: &str) -> (&'static str, &'static str) {
fn surrounding_brackets(self) -> (&'static str, &'static str) {
match self {
Self::List => ("[", "]"),
Self::Set => ("{", "}"),
Self::Tuple(ast_node) => {
if ast_node.is_parenthesized(source) {
Self::Tuple { parenthesized } => {
if parenthesized {
("(", ")")
} else {
("", "")
Expand All @@ -159,19 +159,19 @@ impl SequenceKind<'_> {
}
}

const fn opening_token_for_multiline_definition(&self) -> TokenKind {
const fn opening_token_for_multiline_definition(self) -> TokenKind {
match self {
Self::List => TokenKind::Lsqb,
Self::Set => TokenKind::Lbrace,
Self::Tuple(_) => TokenKind::Lpar,
Self::Tuple { .. } => TokenKind::Lpar,
}
}

const fn closing_token_for_multiline_definition(&self) -> TokenKind {
const fn closing_token_for_multiline_definition(self) -> TokenKind {
match self {
Self::List => TokenKind::Rsqb,
Self::Set => TokenKind::Rbrace,
Self::Tuple(_) => TokenKind::Rpar,
Self::Tuple { .. } => TokenKind::Rpar,
}
}
}
Expand Down Expand Up @@ -217,15 +217,15 @@ impl<'a> SequenceElements<'a> {
/// that can be inserted into the
/// source code as a `range_replacement` autofix.
pub(super) fn sort_single_line_elements_sequence(
kind: &SequenceKind,
kind: SequenceKind,
elts: &[ast::Expr],
elements: &[&str],
locator: &Locator,
sorting_style: SortingStyle,
) -> String {
let element_pairs = SequenceElements::new(elements, elts);
let last_item_index = element_pairs.last_item_index();
let (opening_paren, closing_paren) = kind.surrounding_brackets(locator.contents());
let (opening_paren, closing_paren) = kind.surrounding_brackets();
let mut result = String::from(opening_paren);
// We grab the original source-code ranges using `locator.slice()`
// rather than using the expression generator, as this approach allows
Expand Down Expand Up @@ -334,7 +334,7 @@ impl MultilineStringSequenceValue {
/// Return `None` if the analysis fails for whatever reason.
pub(super) fn from_source_range(
range: TextRange,
kind: &SequenceKind,
kind: SequenceKind,
locator: &Locator,
) -> Option<MultilineStringSequenceValue> {
// Parse the multiline string sequence using the raw tokens.
Expand Down Expand Up @@ -486,7 +486,7 @@ impl Ranged for MultilineStringSequenceValue {
/// in the original source code.
fn collect_string_sequence_lines(
range: TextRange,
kind: &SequenceKind,
kind: SequenceKind,
locator: &Locator,
) -> Option<(Vec<StringSequenceLine>, bool)> {
// These first two variables are used for keeping track of state
Expand Down
14 changes: 9 additions & 5 deletions crates/ruff_linter/src/rules/ruff/rules/sort_dunder_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,13 @@ fn sort_dunder_all(checker: &mut Checker, target: &ast::Expr, node: &ast::Expr)

let (elts, range, kind) = match node {
ast::Expr::List(ast::ExprList { elts, range, .. }) => (elts, *range, SequenceKind::List),
ast::Expr::Tuple(tuple_node @ ast::ExprTuple { elts, range, .. }) => {
(elts, *range, SequenceKind::Tuple(tuple_node))
}
ast::Expr::Tuple(tuple_node @ ast::ExprTuple { elts, range, .. }) => (
elts,
*range,
SequenceKind::Tuple {
parenthesized: tuple_node.parenthesized,
},
),
_ => return,
};

Expand All @@ -166,7 +170,7 @@ fn sort_dunder_all(checker: &mut Checker, target: &ast::Expr, node: &ast::Expr)
let mut diagnostic = Diagnostic::new(UnsortedDunderAll, range);

if let SortClassification::UnsortedAndMaybeFixable { items } = elts_analysis {
if let Some(fix) = create_fix(range, elts, &items, &kind, checker) {
if let Some(fix) = create_fix(range, elts, &items, kind, checker) {
diagnostic.set_fix(fix);
}
}
Expand All @@ -187,7 +191,7 @@ fn create_fix(
range: TextRange,
elts: &[ast::Expr],
string_items: &[&str],
kind: &SequenceKind,
kind: SequenceKind,
checker: &Checker,
) -> Option<Fix> {
let locator = checker.locator();
Expand Down
10 changes: 6 additions & 4 deletions crates/ruff_linter/src/rules/ruff/rules/sort_dunder_slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ impl<'a> StringLiteralDisplay<'a> {
}
}
ast::Expr::Tuple(tuple_node @ ast::ExprTuple { elts, range, .. }) => {
let display_kind = DisplayKind::Sequence(SequenceKind::Tuple(tuple_node));
let display_kind = DisplayKind::Sequence(SequenceKind::Tuple {
parenthesized: tuple_node.parenthesized,
});
Self {
elts: Cow::Borrowed(elts),
range: *range,
Expand Down Expand Up @@ -211,7 +213,7 @@ impl<'a> StringLiteralDisplay<'a> {
(DisplayKind::Sequence(sequence_kind), true) => {
let analyzed_sequence = MultilineStringSequenceValue::from_source_range(
self.range(),
sequence_kind,
*sequence_kind,
locator,
)?;
assert_eq!(analyzed_sequence.len(), self.elts.len());
Expand All @@ -220,7 +222,7 @@ impl<'a> StringLiteralDisplay<'a> {
// Sorting multiline dicts is unsupported
(DisplayKind::Dict { .. }, true) => return None,
(DisplayKind::Sequence(sequence_kind), false) => sort_single_line_elements_sequence(
sequence_kind,
*sequence_kind,
&self.elts,
items,
locator,
Expand All @@ -242,7 +244,7 @@ impl<'a> StringLiteralDisplay<'a> {
/// Python provides for builtin containers.
#[derive(Debug)]
enum DisplayKind<'a> {
Sequence(SequenceKind<'a>),
Sequence(SequenceKind),
Dict { values: &'a [ast::Expr] },
}

Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
elt,
generators,
range: _,
parenthesized: _,
}) => Self::GeneratorExp(ExprGeneratorExp {
elt: elt.into(),
generators: generators.iter().map(Into::into).collect(),
Expand Down Expand Up @@ -1072,6 +1073,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
elts,
ctx: _,
range: _,
parenthesized: _,
}) => Self::Tuple(ExprTuple {
elts: elts.iter().map(Into::into).collect(),
}),
Expand Down

0 comments on commit 77c5561

Please sign in to comment.