Skip to content

Commit

Permalink
add more things to take out of parens
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Mar 2, 2024
1 parent 0db7992 commit cd6c6a4
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub(crate) fn unnecessary_dunder_call(checker: &mut Checker, call: &ast::ExprCal
let value_slice = checker.locator().slice(value.as_ref());
let arg_slice = checker.locator().slice(arg);

if arg.is_attribute_expr() || arg.is_name_expr() || arg.is_literal_expr() {
if can_be_represented_without_parentheses(arg) {
// if it's something that can reasonably be removed from parentheses,
// we'll do that.
fixed = Some(format!("{value_slice} {replacement} {arg_slice}"));
Expand Down Expand Up @@ -174,7 +174,7 @@ pub(crate) fn unnecessary_dunder_call(checker: &mut Checker, call: &ast::ExprCal
let wrap_in_paren = checker
.semantic()
.current_expression_parent()
.is_some_and(|parent| !parent.is_call_expr());
.is_some_and(|parent| !can_be_represented_without_parentheses(parent));

if wrap_in_paren {
fixed = format!("({fixed})");
Expand Down Expand Up @@ -384,3 +384,24 @@ fn in_dunder_method_definition(semantic: &SemanticModel) -> bool {
func_def.name.starts_with("__") && func_def.name.ends_with("__")
})
}

/// Returns `true` if the [`Expr`] can be represented without parentheses.
fn can_be_represented_without_parentheses(expr: &Expr) -> bool {
expr.is_attribute_expr()
|| expr.is_name_expr()
|| expr.is_literal_expr()
|| expr.is_call_expr()
|| expr.is_lambda_expr()
|| expr.is_if_exp_expr()
|| expr.is_generator_exp_expr()
|| expr.is_subscript_expr()
|| expr.is_starred_expr()
|| expr.is_slice_expr()
|| expr.is_dict_expr()
|| expr.is_dict_comp_expr()
|| expr.is_list_expr()
|| expr.is_list_comp_expr()
|| expr.is_tuple_expr()
|| expr.is_set_comp_expr()
|| expr.is_set_expr()
}

0 comments on commit cd6c6a4

Please sign in to comment.