Skip to content

Commit

Permalink
Add parenthesized to ExprGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Feb 26, 2024
1 parent 07a842f commit cd91a92
Show file tree
Hide file tree
Showing 62 changed files with 156 additions and 180 deletions.
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
elts,
ctx,
range: _,
is_parenthesized: _,
parenthesized: _,
})
| Expr::List(ast::ExprList {
elts,
Expand Down Expand Up @@ -1452,6 +1452,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
generators,
elt: _,
range: _,
parenthesized: _,
},
) => {
if checker.enabled(Rule::UnnecessaryListIndexLookup) {
Expand Down
3 changes: 2 additions & 1 deletion 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,7 +1328,7 @@ where
elts,
ctx,
range: _,
is_parenthesized: _,
parenthesized: _,
}) = slice.as_ref()
{
let mut iter = elts.iter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn type_pattern(elts: Vec<&Expr>) -> Expr {
elts: elts.into_iter().cloned().collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
}
.into()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub(crate) fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) {
.collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
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,7 +72,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp
elts,
range: _,
ctx: _,
is_parenthesized: _,
parenthesized: _,
}) = slice.as_ref()
{
for expr in elts {
Expand Down Expand Up @@ -124,7 +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,
is_parenthesized: true,
parenthesized: true,
})),
range: TextRange::default(),
ctx: ExprContext::Load,
Expand All @@ -150,7 +150,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp
elts,
range: TextRange::default(),
ctx: ExprContext::Load,
is_parenthesized: true,
parenthesized: true,
})),
range: TextRange::default(),
ctx: ExprContext::Load,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
.collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
})),
ctx: ExprContext::Load,
range: TextRange::default(),
Expand All @@ -152,7 +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(),
is_parenthesized: true,
parenthesized: true,
})),
ctx: ExprContext::Load,
range: TextRange::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
.collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
});
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
format!("({})", checker.generator().expr(&node)),
Expand Down Expand Up @@ -445,7 +445,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
elts: elts.clone(),
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
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,7 +428,7 @@ pub(crate) fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) {
.collect(),
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
};
let node1 = ast::ExprName {
id: "isinstance".into(),
Expand Down Expand Up @@ -544,7 +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(),
is_parenthesized: true,
parenthesized: true,
};
let node1 = ast::ExprName {
id: id.into(),
Expand Down Expand Up @@ -720,7 +720,7 @@ fn get_short_circuit_edit(
generator.expr(expr)
};
Edit::range_replacement(
if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _, is_parenthesized: _}) 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,7 +305,7 @@ fn assignment_targets_from_expr<'a>(
ctx: ExprContext::Store,
elts,
range: _,
is_parenthesized: _,
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,7 +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,
is_parenthesized: true,
parenthesized: true,
})]),
range: bool_op.range(),
})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E
elts: remaining,
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
};
format!("({})", checker.generator().expr(&node.into()))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E
elts: remaining,
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
};
format!("({})", checker.generator().expr(&node.into()))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +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,
is_parenthesized: true,
parenthesized: true,
})))
}
None => None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ fn make_suggestion(group: &AppendGroup, generator: Generator) -> String {
elts,
ctx: ast::ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
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,7 +162,7 @@ fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> {
elts: new_elts,
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
}
.into(),
};
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/ruff/rules/never_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) {
elts,
ctx: _,
range: _,
is_parenthesized: _,
parenthesized: _,
}) = slice.as_ref()
else {
return;
Expand Down Expand Up @@ -158,7 +158,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) {
elts: rest,
ctx: ast::ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
})),
ctx: ast::ExprContext::Load,
range: TextRange::default(),
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ impl SequenceKind<'_> {
Self::List => ("[", "]"),
Self::Set => ("{", "}"),
Self::Tuple(ast::ExprTuple {
is_parenthesized, ..
parenthesized: is_parenthesized,
..
}) => {
if *is_parenthesized {
("(", ")")
Expand Down
3 changes: 2 additions & 1 deletion 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,7 +1073,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
elts,
ctx: _,
range: _,
is_parenthesized: _,
parenthesized: _,
}) => Self::Tuple(ExprTuple {
elts: elts.iter().map(Into::into).collect(),
}),
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ pub fn any_over_expr(expr: &Expr, func: &dyn Fn(&Expr) -> bool) -> bool {
elt,
generators,
range: _,
parenthesized: _,
}) => {
any_over_expr(elt, func)
|| generators.iter().any(|generator| {
Expand Down Expand Up @@ -1423,7 +1424,7 @@ pub fn pep_604_union(elts: &[Expr]) -> Expr {
elts: vec![],
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
}),
[Expr::Tuple(ast::ExprTuple { elts, .. })] => pep_604_union(elts),
[elt] => elt.clone(),
Expand Down Expand Up @@ -1458,7 +1459,7 @@ pub fn typing_union(elts: &[Expr], binding: String) -> Expr {
elts: vec![],
ctx: ExprContext::Load,
range: TextRange::default(),
is_parenthesized: true,
parenthesized: true,
}),
[Expr::Tuple(ast::ExprTuple { elts, .. })] => typing_union(elts, binding),
[elt] => elt.clone(),
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_python_ast/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,7 @@ impl AstNode for ast::ExprGeneratorExp {
elt,
generators,
range: _,
parenthesized: _,
} = self;
visitor.visit_expr(elt);
for comprehension in generators {
Expand Down Expand Up @@ -3256,7 +3257,7 @@ impl AstNode for ast::ExprTuple {
elts,
ctx: _,
range: _,
is_parenthesized: _,
parenthesized: _,
} = self;

for expr in elts {
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ pub struct ExprGeneratorExp {
pub range: TextRange,
pub elt: Box<Expr>,
pub generators: Vec<Comprehension>,
pub parenthesized: bool,
}

impl From<ExprGeneratorExp> for Expr {
Expand Down Expand Up @@ -1797,7 +1798,7 @@ pub struct ExprTuple {
pub ctx: ExprContext,

/// Whether the tuple is parenthesized in the source code.
pub is_parenthesized: bool,
pub parenthesized: bool,
}

impl From<ExprTuple> for Expr {
Expand Down Expand Up @@ -3882,7 +3883,7 @@ mod tests {
assert_eq!(std::mem::size_of::<ExprDictComp>(), 48);
assert_eq!(std::mem::size_of::<ExprEllipsisLiteral>(), 8);
assert_eq!(std::mem::size_of::<ExprFString>(), 48);
assert_eq!(std::mem::size_of::<ExprGeneratorExp>(), 40);
assert_eq!(std::mem::size_of::<ExprGeneratorExp>(), 48);
assert_eq!(std::mem::size_of::<ExprIfExp>(), 32);
assert_eq!(std::mem::size_of::<ExprIpyEscapeCommand>(), 32);
assert_eq!(std::mem::size_of::<ExprLambda>(), 24);
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_python_ast/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
elt,
generators,
range: _,
parenthesized: _,
}) => {
for comprehension in generators {
visitor.visit_comprehension(comprehension);
Expand Down Expand Up @@ -539,7 +540,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
elts,
ctx,
range: _,
is_parenthesized: _,
parenthesized: _,
}) => {
for expr in elts {
visitor.visit_expr(expr);
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_python_ast/src/visitor/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ pub fn walk_expr<V: Transformer + ?Sized>(visitor: &V, expr: &mut Expr) {
elt,
generators,
range: _,
parenthesized: _,
}) => {
for comprehension in generators {
visitor.visit_comprehension(comprehension);
Expand Down Expand Up @@ -528,7 +529,7 @@ pub fn walk_expr<V: Transformer + ?Sized>(visitor: &V, expr: &mut Expr) {
elts,
ctx,
range: _,
is_parenthesized: _,
parenthesized: _,
}) => {
for expr in elts {
visitor.visit_expr(expr);
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_python_codegen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ impl<'a> Generator<'a> {
Expr::GeneratorExp(ast::ExprGeneratorExp {
elt,
generators,
parenthesized: _,
range: _,
}) => {
self.p("(");
Expand Down Expand Up @@ -1037,6 +1038,7 @@ impl<'a> Generator<'a> {
elt,
generators,
range: _,
parenthesized: _,
})],
[],
) = (arguments.args.as_ref(), arguments.keywords.as_ref())
Expand Down
7 changes: 2 additions & 5 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};

use crate::comments::visitor::{CommentPlacement, DecoratedComment};
use crate::expression::expr_generator_exp::is_generator_parenthesized;
use crate::expression::expr_slice::{assign_comment_in_slice, ExprSliceCommentSection};
use crate::other::parameters::{
assign_argument_separator_comment_placement, find_parameter_separators,
Expand Down Expand Up @@ -316,12 +315,10 @@ fn handle_enclosed_comment<'a>(
| AnyNodeRef::ExprListComp(_)
| AnyNodeRef::ExprSetComp(_) => handle_bracketed_end_of_line_comment(comment, locator),
AnyNodeRef::ExprTuple(ast::ExprTuple {
is_parenthesized: true,
parenthesized: true,
..
}) => handle_bracketed_end_of_line_comment(comment, locator),
AnyNodeRef::ExprGeneratorExp(generator)
if is_generator_parenthesized(generator, locator.contents()) =>
{
AnyNodeRef::ExprGeneratorExp(generator) if generator.parenthesized => {
handle_bracketed_end_of_line_comment(comment, locator)
}
_ => CommentPlacement::Default(comment),
Expand Down
Loading

0 comments on commit cd91a92

Please sign in to comment.