Skip to content

Commit

Permalink
cleanup/tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Jan 26, 2024
1 parent c1782ce commit c49171c
Showing 1 changed file with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,34 +163,35 @@ pub(crate) fn unnecessary_dunder_call(checker: &mut Checker, call: &ast::ExprCal
.current_expression_parent()
.is_some_and(|parent| matches!(parent, Expr::UnaryOp(ast::ExprUnaryOp { .. })));

// find the first ")" before our dunder method
let rparen = SimpleTokenizer::starts_at(value.as_ref().end(), checker.locator().contents())
.find(|token| token.kind == SimpleTokenKind::RParen);

// find the "." before our dunder method
let dot = SimpleTokenizer::starts_at(value.as_ref().end(), checker.locator().contents())
.find(|token| token.kind == SimpleTokenKind::Dot)
.unwrap();

if is_in_unary {
if rparen.is_some() {
// if we're within parentheses with a unary, we're going to take
// the value operand, and insert the fix in its place within its
// existing parentheses.
// for example, `-(-a).__sub__(1)` -> `-(-a - 1)`

diagnostic.set_fix(Fix::safe_edits(
Edit::range_replacement(fixed, value.as_ref().range()),
[Edit::deletion(dot.start(), call.end())],
));
} else {
// find the first ")" before our dunder method
let rparen =
SimpleTokenizer::starts_at(value.as_ref().end(), checker.locator().contents())
.find(|token| {
token.kind == SimpleTokenKind::RParen && token.start() < call.end()
});

// find the "." before our dunder method
let dot =
SimpleTokenizer::starts_at(value.as_ref().end(), checker.locator().contents())
.find(|token| token.kind == SimpleTokenKind::Dot && token.start() < call.end())
.unwrap();

// if we're within parentheses with a unary, we're going to take
// the value operand, and insert the fix in its place within its
// existing parentheses. the existing "fixed" value is what we want.
// for example, `-(-a).__sub__(1)` -> `-(-a - 1)`
if rparen.is_none() {
// otherwise, we're going to wrap the fix in parentheses
// to maintain semantic integrity.
// `-a.__sub__(1)` -> `-(a - 1)`
fixed = format!("({fixed})");
diagnostic.set_fix(Fix::safe_edits(
Edit::range_replacement(fixed, value.as_ref().range()),
[Edit::deletion(dot.start(), call.end())],
));
}

diagnostic.set_fix(Fix::safe_edits(
Edit::range_replacement(fixed, value.as_ref().range()),
[Edit::deletion(dot.start(), call.end())],
));
} else {
// `(3).__add__(1)` -> `3 + 1`
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(fixed, call.range())));
Expand Down

0 comments on commit c49171c

Please sign in to comment.