Skip to content

unused type lint - #2552

Open
nnullcolumn wants to merge 4 commits into
luau-lang:masterfrom
nnullcolumn:lint-unused-type-alias
Open

unused type lint#2552
nnullcolumn wants to merge 4 commits into
luau-lang:masterfrom
nnullcolumn:lint-unused-type-alias

Conversation

@nnullcolumn

@nnullcolumn nnullcolumn commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

unused type alias / type function lint pass. does not trigger on export, references to type functions from within that type function's body don't count. tested failure cases pretty extensively, pr/code/tests are 100% human authored.

silencing path is to prefix the type with _ for now. Although you cannot declare multiple type aliases with the name _, unused types like that are niche, and users can always --!nolint TypeUnused.

some people are saying its even better than unused local

@nnullcolumn nnullcolumn left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

noticed currently this makes the lint span the entire function's body, will update code/tests sometime soon to only show up on the name's location

edit: also needs some general cleanup.. code is a bit scarier than what it needs to be

@nnullcolumn nnullcolumn left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

on mobile rn; left some small comments for myself to work on later

Comment thread Analysis/src/Linter.cpp Outdated
Comment thread tests/Linter.test.cpp Outdated
Comment thread tests/Linter.test.cpp Outdated
Comment thread tests/Linter.test.cpp
CHECK_EQ(result.warnings[0].location, Location(Position(1, 0), Position(3, 3)));
}

TEST_CASE_FIXTURE(Fixture, "unused_type_unrelated_global_reference")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

testing for global access seems a little sparse, i should probably improve this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree

@nnullcolumn nnullcolumn Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree

next time please quote reply; i was very confused to see an "i agree" on this line until i visited the diff page :P will do when i get the chance though

EDIT: github mobile ui was bugged. classic.

EDIT 2: oh, no. it just didnt show the convo because it was displaying the comment in the review section haha

Comment thread Analysis/src/Linter.cpp Outdated
Comment thread Analysis/src/Linter.cpp
Comment on lines +3746 to +3748
if (context.warningEnabled(LintWarning::Code_TypeUnused))
LintUnusedType::process(context);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please flag this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

whoops sorry will do

Comment thread tests/Linter.test.cpp
CHECK_EQ(result.warnings[0].location, Location(Position(1, 0), Position(3, 3)));
}

TEST_CASE_FIXTURE(Fixture, "unused_type_unrelated_global_reference")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree

Comment thread Analysis/src/Linter.cpp

void report()
{
for (auto& pair : refs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor nit: if you are going to iterate over it, prefer some sort of ordered data structure. You can compose a DenseHashSet with a std::vector, for example.

Comment thread Analysis/src/Linter.cpp
Comment on lines +3578 to +3582
void scopePush(Alias& alias)
{
alias.scopeDepth++;
typesEnvScopeStack.push_back(&alias);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: just have this take a NotNull<Alias> instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

n-not.. null? but i'm nnullcolumn..

sounds good though, will do when i get back to this PR (probably not today)

Comment thread Analysis/src/Linter.cpp
Comment on lines +3601 to +3604
Alias& alias = refs[node->name];
alias.declaration = node;
alias.nameLocation = node->nameLocation;
alias.exported = node->exported;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit, I think house style prefers just using emplace_back or try_insert.

Comment thread Analysis/src/Linter.cpp
Comment on lines +3610 to +3614
Alias& alias = refs[node->name];
alias.declaration = node;
alias.nameLocation = node->nameLocation;
alias.exported = node->exported;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto re: try_insert

Comment thread Analysis/src/Linter.cpp
AstStat* declaration = nullptr;
unsigned int scopeDepth = 0;
bool referenced = false;
bool softReferenced = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto as the other PR re "softReferenced"

Comment thread Analysis/src/Linter.cpp
unsigned int scopeDepth = 0;
bool referenced = false;
bool softReferenced = false;
bool exported = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not rename referenced to referencedOrExported?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i agree; wasn't super satisfied with that naming anyways

@hgoldstein hgoldstein self-assigned this Aug 18, 2026
@nnullcolumn

Copy link
Copy Markdown
Contributor Author

hopefully going to have time to come back to this soon. just thought of another test case, i should definitely be looking out for recursive type aliases type Json = { [string]: Json | string | number }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce a lint for "TypeUnused"

2 participants