Fix unused multi-assignments in a single pass (#2829)

This commit is contained in:
Charlie Marsh 2023-02-12 17:28:03 -05:00 committed by GitHub
parent 8b35b052b8
commit 1abaece9ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 58 deletions

View File

@ -201,31 +201,7 @@ fn remove_unused_variable(
range.location == target.location && range.end_location == target.end_location.unwrap() range.location == target.location && range.end_location == target.end_location.unwrap()
}) { }) {
if matches!(target.node, ExprKind::Name { .. }) { if matches!(target.node, ExprKind::Name { .. }) {
return if targets.len() > 1 { return if targets.len() > 1 || contains_effect(checker, value) {
// Construct a deletion by concatenating everything before the target to
// everything after it. This ensures that our edit spans the entire statement,
// which in turn ensures that we only apply one edit per pass.
Some((
DeletionKind::Partial,
Fix::replacement(
format!(
"{}{}",
checker.locator.slice_source_code_range(&Range::new(
stmt.location,
target.location
)),
checker.locator.slice_source_code_range(&Range::new(
match_token_after(target, checker.locator, |tok| tok
== Tok::Equal)
.location,
stmt.end_location.unwrap()
))
),
stmt.location,
stmt.end_location.unwrap(),
),
))
} else if contains_effect(checker, value) {
// If the expression is complex (`x = foo()`), remove the assignment, // If the expression is complex (`x = foo()`), remove the assignment,
// but preserve the right-hand side. // but preserve the right-hand side.
Some(( Some((

View File

@ -92,13 +92,13 @@ expression: diagnostics
column: 16 column: 16
fix: fix:
content: content:
- "(x, y) = bar" - ""
location: location:
row: 26 row: 26
column: 4 column: 13
end_location: end_location:
row: 26 row: 26
column: 22 column: 19
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:

View File

@ -35,13 +35,13 @@ expression: diagnostics
column: 19 column: 19
fix: fix:
content: content:
- "(x, y) = 1, 2" - ""
location: location:
row: 16 row: 16
column: 4 column: 13
end_location: end_location:
row: 16 row: 16
column: 26 column: 22
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -54,13 +54,13 @@ expression: diagnostics
column: 10 column: 10
fix: fix:
content: content:
- "(x, y) = 1, 2" - ""
location: location:
row: 20 row: 20
column: 4 column: 4
end_location: end_location:
row: 20 row: 20
column: 26 column: 13
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:

View File

@ -187,13 +187,13 @@ expression: diagnostics
column: 22 column: 22
fix: fix:
content: content:
- "(x2, y2) = (1, 2)" - ""
location: location:
row: 33 row: 33
column: 4 column: 15
end_location: end_location:
row: 33 row: 33
column: 31 column: 25
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -206,13 +206,13 @@ expression: diagnostics
column: 11 column: 11
fix: fix:
content: content:
- "(x3, y3) = (1, 2)" - ""
location: location:
row: 34 row: 34
column: 4 column: 4
end_location: end_location:
row: 34 row: 34
column: 31 column: 14
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -377,13 +377,13 @@ expression: diagnostics
column: 12 column: 12
fix: fix:
content: content:
- tt = lexer.get_token() - ""
location: location:
row: 87 row: 87
column: 4 column: 4
end_location: end_location:
row: 87 row: 87
column: 37 column: 15
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -396,13 +396,13 @@ expression: diagnostics
column: 12 column: 12
fix: fix:
content: content:
- tt = lexer.get_token() - ""
location: location:
row: 93 row: 93
column: 4 column: 4
end_location: end_location:
row: 93 row: 93
column: 37 column: 15
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -415,13 +415,13 @@ expression: diagnostics
column: 17 column: 17
fix: fix:
content: content:
- toplevel = lexer.get_token() - ""
location: location:
row: 93 row: 93
column: 4 column: 15
end_location: end_location:
row: 93 row: 93
column: 37 column: 20
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -434,13 +434,13 @@ expression: diagnostics
column: 12 column: 12
fix: fix:
content: content:
- "(a, b) = lexer.get_token()" - ""
location: location:
row: 97 row: 97
column: 4 column: 4
end_location: end_location:
row: 97 row: 97
column: 41 column: 15
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -453,13 +453,13 @@ expression: diagnostics
column: 21 column: 21
fix: fix:
content: content:
- "(a, b) = lexer.get_token()" - ""
location: location:
row: 101 row: 101
column: 4 column: 13
end_location: end_location:
row: 101 row: 101
column: 41 column: 24
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -472,13 +472,13 @@ expression: diagnostics
column: 12 column: 12
fix: fix:
content: content:
- tt = 1 - ""
location: location:
row: 105 row: 105
column: 4 column: 4
end_location: end_location:
row: 105 row: 105
column: 21 column: 15
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable:
@ -491,12 +491,12 @@ expression: diagnostics
column: 17 column: 17
fix: fix:
content: content:
- toplevel = 1 - ""
location: location:
row: 105 row: 105
column: 4 column: 15
end_location: end_location:
row: 105 row: 105
column: 21 column: 20
parent: ~ parent: ~

View File

@ -73,13 +73,13 @@ expression: diagnostics
column: 16 column: 16
fix: fix:
content: content:
- "(x, y) = bar" - ""
location: location:
row: 26 row: 26
column: 4 column: 13
end_location: end_location:
row: 26 row: 26
column: 22 column: 19
parent: ~ parent: ~
- kind: - kind:
UnusedVariable: UnusedVariable: