Skip to content

feat(linter): add no-unused-types rule to warn on unused type declarations#20035

Open
myaschmitz wants to merge 6 commits into
mainfrom
myaschmitz/no-unused-types-linter-rule
Open

feat(linter): add no-unused-types rule to warn on unused type declarations#20035
myaschmitz wants to merge 6 commits into
mainfrom
myaschmitz/no-unused-types-linter-rule

Conversation

@myaschmitz

@myaschmitz myaschmitz commented Jul 9, 2026

Copy link
Copy Markdown

Description

Adds a new linter rule no-unused-types that emits a warning when a user-defined type is declared but never referenced, mirroring the existing no-unused-vars and no-unused-params rules. Types marked with @export() are excluded, and a Remove unused type quick-fix is provided.

Fixes #11876

Example Usage

Given the following Bicep file:

type usedType = {
  name: string
}

type unusedType = string   // warning: Type "unusedType" is declared but never used.

param person usedType
output personName string = person.name

The linter reports:

main.bicep(5,6) : Warning no-unused-types: Type "unusedType" is declared but never used. [https://aka.ms/bicep/linter-diagnostics#no-unused-types]

The rule defaults to warning level and can be configured in bicepconfig.json.

Changes

  • NoUnusedTypesRule.cs - new rule (auto-discovered via the linter rule source generator).
  • CoreResources.resx / .Designer.cs - description and message resource strings.
  • bicepconfig.schema.json - schema entry so the rule is configurable with IntelliSense.
  • NoUnusedTypesRuleTests.cs - unit tests (used/unused/exported/type-alias-chain/syntax-error cases).

Note: Documentation lives in the separate Microsoft Learn docs repo (the aka.ms/bicep/linter-diagnostics#no-unused-types anchor). Companion docs PR (internal): MicrosoftDocs/azure-docs-pr#318158 - adds the linter-rule-no-unused-types.md page, the linter.md table row, and the toc.yml entry.

Checklist

Microsoft Reviewers: Open in CodeFlow

…tions

Adds a new linter rule `no-unused-types` that emits a warning when a
user-defined type is declared but never referenced, mirroring the existing
`no-unused-vars` and `no-unused-params` rules. Types marked with @export()
are excluded. Includes a `Remove unused type` quick-fix, resource strings,
bicepconfig schema entry, and unit tests.

Fixes #11876

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Test this change out locally with the following install scripts (Action run 29538039388)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 29538039388
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 29538039388"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 29538039388
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 29538039388"

…types

- Regenerate 5 diagnostics baselines to include new no-unused-types warnings
- Disable no-unused-types in TypeStringifierTests.Compile since those tests
  compile standalone, intentionally-unused type declarations

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Exclude linter diagnostics from error-only/no-diagnostic assertions and add
the expected no-unused-types warnings to tests that assert linter output.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@myaschmitz
myaschmitz marked this pull request as ready for review July 14, 2026 21:45
{
CompileAndTest(text, new(OnCompileErrors.Ignore), unusedTypes);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test to verify the code fix works?

See

[TestMethod]
public void Codefix_removes_name_property() => AssertCodeFix("""
module foo 'br/public:avm/res/network/virtual-network:0.1.8' = {
na|me: 'foo'
params: {}
}
""", """
module foo 'br/public:avm/res/network/virtual-network:0.1.8' = {
params: {}
}
""");
for an example of how to do this.

myaschmitz and others added 2 commits July 15, 2026 15:52
Adds two code-fix tests for the no-unused-types rule: one verifying the quick fix removes a lone unused type, and one verifying it removes only the unused type while leaving referenced types intact.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Explain why exported types are skipped (external references aren't visible to single-file analysis) and why a self-binding declaration counts as unused.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn on unused type declarations

2 participants