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 a825cc4
Showing 1 changed file with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,29 @@ pub(crate) fn unnecessary_dunder_call(checker: &mut Checker, call: &ast::ExprCal

// 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(|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)
.find(|token| token.kind == SimpleTokenKind::Dot && token.start() < call.end())
.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 {
// 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 a825cc4

Please sign in to comment.