Skip to content

Commit

Permalink
Skip fixing unused variable when they have references
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 14, 2023
1 parent cf88f73 commit 7161458
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
7 changes: 7 additions & 0 deletions selene-lib/src/lints/unused_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ impl Lint for UnusedVariableLint {
fixed_code = None;
}

// Applying fix would cause references to reference the old variable. It's possible to also rename those
// references as well, but we'd need to check each of their scopes for potentially colliding variables.
// Cargo just doesn't apply a fix in these cases.
if !variable.references.is_empty() {
fixed_code = None;
}

let write_only = !analyzed_references.is_empty();

diagnostics.push(Diagnostic::new_complete(
Expand Down
9 changes: 3 additions & 6 deletions selene-lib/tests/lints/unused_variable/blocks.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
-- Functions
-local function unusedFunction()
- local unusedVariableA = 1
- local unusedVariableB = 1
+local function _unusedFunction()
+ local _unusedVariableA = 1
+ local _unusedVariableB = 1
local unusedVariableA = 1
local unusedVariableB = 1
end

print(unusedVariableB)
-local overidden = true
+local _overidden = true
local overidden = true

local function overridesIt()
local overidden = false
Expand Down
6 changes: 2 additions & 4 deletions selene-lib/tests/lints/unused_variable/edge_cases.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local foo = 1
-local foo = foo + 1
-local bar = bar
+local _foo = foo + 1
+local _bar = bar
local foo = foo + 1
local bar = bar

-- The variables inside of a repeat...until are accessible from the until statement
repeat
Expand Down
6 changes: 2 additions & 4 deletions selene-lib/tests/lints/unused_variable/locals.fixed.diff
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
print(localC)

-- Mutated, but never read
-local localD = 1
+local _localD = 1
local localD = 1
localD = 2
localD = 3

Expand All @@ -29,5 +28,4 @@

-- Put into a table
local localH = 1
-local localI = { localH }
+local _localI = { localH }
local localI = { localH }
9 changes: 3 additions & 6 deletions selene-lib/tests/lints/unused_variable/observes.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
-local writtenOnlyA = {}
+local _writtenOnlyA = {}
local writtenOnlyA = {}
table.insert(writtenOnlyA, 3)

local readA = {}
print(table.insert(readA, 3))

-- Make sure doing it inside other statements doesn't trigger false negatives
print(function()
- local writtenOnlyB = {}
+ local _writtenOnlyB = {}
local writtenOnlyB = {}
table.insert(writtenOnlyB, 1)

local readB = {}
return table.insert(readB, 1)
end)

if true then
- local writtenOnlyC = {}
+ local _writtenOnlyC = {}
local writtenOnlyC = {}
table.insert(writtenOnlyC, 1)

local readC = {}
Expand Down
3 changes: 1 addition & 2 deletions selene-lib/tests/lints/unused_variable/write_only.fixed.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-local bad1 = {}
+local _bad1 = {}
local bad1 = {}
bad1.x = 1
bad1.y = 2
bad1["z"] = 1
Expand Down

0 comments on commit 7161458

Please sign in to comment.