Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a regression with reporting unused parameters in potential predicates #58514

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11461,7 +11461,7 @@ export function createNameResolver({
}
break;
}
if (isSelfReferenceLocation(location)) {
if (isSelfReferenceLocation(location, lastLocation)) {
lastSelfReferenceLocation = location;
}
lastLocation = location;
Expand Down Expand Up @@ -11595,15 +11595,18 @@ export function createNameResolver({
}

type SelfReferenceLocation =
| ParameterDeclaration
| FunctionDeclaration
| ClassDeclaration
| InterfaceDeclaration
| EnumDeclaration
| TypeAliasDeclaration
| ModuleDeclaration;

function isSelfReferenceLocation(node: Node): node is SelfReferenceLocation {
function isSelfReferenceLocation(node: Node, lastLocation: Node | undefined): node is SelfReferenceLocation {
switch (node.kind) {
case SyntaxKind.Parameter:
return !!lastLocation && lastLocation === (node as ParameterDeclaration).name;
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
noUnusedLocals_potentialPredicateUnusedParam.ts(1,40): error TS6133: 'a' is declared but its value is never read.


==== noUnusedLocals_potentialPredicateUnusedParam.ts (1 errors) ====
function potentialPredicateUnusedParam(a: unknown) {
~
!!! error TS6133: 'a' is declared but its value is never read.
return !!Math.random();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts] ////

=== noUnusedLocals_potentialPredicateUnusedParam.ts ===
function potentialPredicateUnusedParam(a: unknown) {
>potentialPredicateUnusedParam : Symbol(potentialPredicateUnusedParam, Decl(noUnusedLocals_potentialPredicateUnusedParam.ts, 0, 0))
>a : Symbol(a, Decl(noUnusedLocals_potentialPredicateUnusedParam.ts, 0, 39))

return !!Math.random();
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts] ////

=== noUnusedLocals_potentialPredicateUnusedParam.ts ===
function potentialPredicateUnusedParam(a: unknown) {
>potentialPredicateUnusedParam : (a: unknown) => boolean
> : ^ ^^ ^^^^^^^^^^^^
>a : unknown
> : ^^^^^^^

return !!Math.random();
>!!Math.random() : boolean
> : ^^^^^^^
>!Math.random() : boolean
> : ^^^^^^^
>Math.random() : number
> : ^^^^^^
>Math.random : () => number
> : ^^^^^^
>Math : Math
> : ^^^^
>random : () => number
> : ^^^^^^
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @strict: true
// @noEmit: true
// @noUnusedLocals: true
// @noUnusedParameters: true

function potentialPredicateUnusedParam(a: unknown) {
return !!Math.random();
}