Skip to content

unused-variable

Reports a local variable or catch variable that is never used, or that is only assigned constants and never read. Variables captured by lambdas or local functions count as used. The fix removes the declaration. Off by default, because the compiler already warns.

A variable that is never read is a leftover of changed code, or a result that was meant to be used, so the logic that should have used it is missing.

To fix, remove the variable, or use its value.

Reported
function F {
$unused = 1
$read = 2
"$read"
$member = @{}
...
$script:t = $q
$splat = @{}
Get-Item @splat
$u = 1
}
csharp:
lint:
rules:
unused-variable: warn # off (default), warn, error
js:
lint:
rules:
unused-variable: warn # off (default), warn, error
powershell:
lint:
rules:
unused-variable: warn # off (default), warn, error
python:
lint:
rules:
unused-variable: warn # off (default), warn, error
shell:
lint:
rules:
unused-variable: warn # off (default), warn, error

Enabled by the presets: js/eslint-recommended, js/typescript-eslint-recommended, js/biome-recommended, js/oxlint, powershell/psscriptanalyzer, python/ruff, shell/shellcheck.

Suppress one line with sudsy-disable-next-line unused-variable in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
eslint no-unused-vars partial the options keep their defaults; /*exported*/ and /*global*/ comments are not read; a JSX tag counts as a use
psscriptanalyzer PSUseDeclaredVarsMoreThanAssignments partial The shared scope model, where a name is looked up through the blocks around it, so a read in an inner block of a name the inner block assigns itself counts for the outer one; the module counts by name inside each block. A scope that reads $PSBoundParameters is not checked (a documented difference).
ruff F841 full
shellcheck SC2034 partial A name the whole file assigns and never reads; the file is one scope, local is not told apart.
typescript-eslint no-unused-vars partial the options keep their defaults; .d.ts files and declare module blocks are not treated as used