diff --git a/selene-lib/src/lints/unused_variable.rs b/selene-lib/src/lints/unused_variable.rs index 71a0e613..2f5da454 100644 --- a/selene-lib/src/lints/unused_variable.rs +++ b/selene-lib/src/lints/unused_variable.rs @@ -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( diff --git a/selene-lib/tests/lints/unused_variable/blocks.fixed.diff b/selene-lib/tests/lints/unused_variable/blocks.fixed.diff index 5308ebc1..402db837 100644 --- a/selene-lib/tests/lints/unused_variable/blocks.fixed.diff +++ b/selene-lib/tests/lints/unused_variable/blocks.fixed.diff @@ -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 diff --git a/selene-lib/tests/lints/unused_variable/edge_cases.fixed.diff b/selene-lib/tests/lints/unused_variable/edge_cases.fixed.diff index 414306c0..8565d0c6 100644 --- a/selene-lib/tests/lints/unused_variable/edge_cases.fixed.diff +++ b/selene-lib/tests/lints/unused_variable/edge_cases.fixed.diff @@ -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 diff --git a/selene-lib/tests/lints/unused_variable/locals.fixed.diff b/selene-lib/tests/lints/unused_variable/locals.fixed.diff index ea6cec85..6bcc77e7 100644 --- a/selene-lib/tests/lints/unused_variable/locals.fixed.diff +++ b/selene-lib/tests/lints/unused_variable/locals.fixed.diff @@ -7,8 +7,7 @@ print(localC) -- Mutated, but never read --local localD = 1 -+local _localD = 1 + local localD = 1 localD = 2 localD = 3 @@ -29,5 +28,4 @@ -- Put into a table local localH = 1 --local localI = { localH } -+local _localI = { localH } + local localI = { localH } diff --git a/selene-lib/tests/lints/unused_variable/observes.fixed.diff b/selene-lib/tests/lints/unused_variable/observes.fixed.diff index 64c364f0..e62cc6fa 100644 --- a/selene-lib/tests/lints/unused_variable/observes.fixed.diff +++ b/selene-lib/tests/lints/unused_variable/observes.fixed.diff @@ -1,5 +1,4 @@ --local writtenOnlyA = {} -+local _writtenOnlyA = {} + local writtenOnlyA = {} table.insert(writtenOnlyA, 3) local readA = {} @@ -7,8 +6,7 @@ -- 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 = {} @@ -16,8 +14,7 @@ end) if true then -- local writtenOnlyC = {} -+ local _writtenOnlyC = {} + local writtenOnlyC = {} table.insert(writtenOnlyC, 1) local readC = {} diff --git a/selene-lib/tests/lints/unused_variable/write_only.fixed.diff b/selene-lib/tests/lints/unused_variable/write_only.fixed.diff index f0652f88..47262952 100644 --- a/selene-lib/tests/lints/unused_variable/write_only.fixed.diff +++ b/selene-lib/tests/lints/unused_variable/write_only.fixed.diff @@ -1,5 +1,4 @@ --local bad1 = {} -+local _bad1 = {} + local bad1 = {} bad1.x = 1 bad1.y = 2 bad1["z"] = 1